From: kkolinko Date: Fri, 19 Jun 2009 12:51:11 +0000 (+0000) Subject: Added toString() call to the StringBuffer. The tests will run a bit slower because... X-Git-Url: https://git.internetallee.de/?a=commitdiff_plain;h=2c34b26a577ab405cea86095c4119732da8a066b;p=tomcat7.0 Added toString() call to the StringBuffer. The tests will run a bit slower because of that. Added two other implementation variants for the first test. git-svn-id: https://svn.apache.org/repos/asf/tomcat/trunk@786484 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/test/org/apache/catalina/valves/Benchmarks.java b/test/org/apache/catalina/valves/Benchmarks.java index 47c126542..61966ca26 100644 --- a/test/org/apache/catalina/valves/Benchmarks.java +++ b/test/org/apache/catalina/valves/Benchmarks.java @@ -33,7 +33,9 @@ public class Benchmarks extends TestCase { // 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_Local(), + new GetDateBenchmarkTest_LocalMutableLong(), + new GetDateBenchmarkTest_LocalStruct() }; benchmark.doTest(5, tests); } @@ -92,6 +94,72 @@ public class Benchmarks extends TestCase { } } + private static class GetDateBenchmarkTest_LocalMutableLong implements + Runnable { + + public String toString() { + return "ThreadLocals with a mutable Long"; + } + + private static class MutableLong { + long value = 0; + } + + private ThreadLocal currentMillisLocal = new ThreadLocal() { + protected MutableLong initialValue() { + return new MutableLong(); + } + }; + + private ThreadLocal currentDateLocal = new ThreadLocal(); + + public void run() { + getCurrentDate(); + } + + public Date getCurrentDate() { + long systime = System.currentTimeMillis(); + if ((systime - currentMillisLocal.get().value) > 1000) { + currentDateLocal.set(new Date(systime)); + currentMillisLocal.get().value = systime; + } + return currentDateLocal.get(); + } + } + + private static class GetDateBenchmarkTest_LocalStruct implements Runnable { + + public String toString() { + return "single ThreadLocal"; + } + + // note, that we can avoid (long -> Long) conversion + private static class Struct { + long currentMillis = 0; + Date currentDate; + } + + private ThreadLocal currentStruct = new ThreadLocal() { + protected Struct initialValue() { + return new Struct(); + } + }; + + public void run() { + getCurrentDate(); + } + + 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; + } + } + public void testAccessLogTimeDateElement() throws Exception { // Is it better to use a sync or a thread local here? BenchmarkTest benchmark = new BenchmarkTest(); @@ -136,7 +204,7 @@ public class Benchmarks extends TestCase { printDate(); } - public StringBuffer printDate() { + public String printDate() { StringBuffer buf = new StringBuffer(); Date date = getDateSync(); if (currentDate != date) { @@ -158,7 +226,7 @@ public class Benchmarks extends TestCase { } } buf.append(currentDateString); - return buf; + return buf.toString(); } private Date getDateSync() { @@ -216,7 +284,7 @@ public class Benchmarks extends TestCase { printDate(); } - public StringBuffer printDate() { + public String printDate() { StringBuffer buf = new StringBuffer(); Date date = getDateLocal(); if (currentDate != date) { @@ -234,7 +302,7 @@ public class Benchmarks extends TestCase { currentDate = date; } buf.append(currentDateString); - return buf; + return buf.toString(); } private Date getDateLocal() {