/** *Somesimplemicro-benchmarkstohelpdeterminebestapproachforthreadsafetyinvalves,particularlythe *{@linkAccessLogValve}.ImplementedasJUnitteststomakethesimpletoexecutebutdoesnotusedTest*astheclass *nametoavoidbeingincludedintheautomatedunittests.
*/ publicclass Benchmarks {
@Test publicvoid testAccessLogGetDate() throws Exception { // Is it better to use a sync or a thread local here?
BenchmarkTest benchmark = new BenchmarkTest();
Runnable[] tests = new Runnable[] { new GetDateBenchmarkTest_Sync(), new GetDateBenchmarkTest_Local(), new GetDateBenchmarkTest_LocalMutableLong(), new GetDateBenchmarkTest_LocalStruct() };
benchmark.doTest(5, tests);
}
public Date getCurrentDate() {
Struct struct = currentStruct.get(); long systime = System.currentTimeMillis(); if ((systime - struct.currentMillis) > 1000) {
struct.currentDate = new Date(systime);
struct.currentMillis = systime;
} return struct.currentDate;
}
}
@Test publicvoid testAccessLogTimeDateElement() throws Exception { // Is it better to use a sync or a thread local here?
BenchmarkTest benchmark = new BenchmarkTest();
Runnable[] tests = new Runnable[] { new TimeDateElementBenchmarkTest_Sync(), new TimeDateElementBenchmarkTest_Local(), new TimeDateElementBenchmarkTest_LocalStruct(), new TimeDateElementBenchmarkTest_LocalStruct_SBuilder() };
benchmark.doTest(5, tests);
}
@Override public String toString() { return"single ThreadLocal";
}
privatestaticclass Struct { public String currentDateString; public Date currentDate = new Date(); public SimpleDateFormat dayFormatter = new SimpleDateFormat("dd"); public SimpleDateFormat monthFormatter = new SimpleDateFormat("MM"); public SimpleDateFormat yearFormatter = new SimpleDateFormat("yyyy"); public SimpleDateFormat timeFormatter = new SimpleDateFormat("hh:mm:ss");
}
@Override public String toString() { return"single ThreadLocal, with StringBuilder";
}
privatestaticclass Struct { public String currentDateString; public Date currentDate = new Date(); public SimpleDateFormat dayFormatter = new SimpleDateFormat("dd"); public SimpleDateFormat monthFormatter = new SimpleDateFormat("MM"); public SimpleDateFormat yearFormatter = new SimpleDateFormat("yyyy"); public SimpleDateFormat timeFormatter = new SimpleDateFormat("hh:mm:ss");
}
privatevoid doTestInternal(int threadCount, int iterations, Runnable test) throws Exception { long start = System.currentTimeMillis(); Thread[] threads = newThread[threadCount]; for (int i = 0; i < threadCount; i++) {
threads[i] = newThread(new TestThread(iterations, test));
} for (int i = 0; i < threadCount; i++) {
threads[i].start();
} for (int i = 0; i < threadCount; i++) {
threads[i].join();
} long end = System.currentTimeMillis();
System.out.println(test.getClass().getSimpleName() + ": " + threadCount + " threads and " + iterations + " iterations using " + test + " took " + (end - start) + "ms");
}
}
@Override publicvoid run() { for (int i = 0; i < count; i++) {
test.run();
}
}
}
}
Messung V0.5 in Prozent
¤ Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.0.13Bemerkung:
(vorverarbeitet am 2026-10-11)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.