From 1b6241a8795011ef2ef4b2c1c2c1f86a936b8202 Mon Sep 17 00:00:00 2001 From: markt Date: Fri, 19 Jun 2009 17:13:00 +0000 Subject: [PATCH] As per Filip's suggestions: - reduce object creation - better use of ThreadLocals git-svn-id: https://svn.apache.org/repos/asf/tomcat/trunk@786585 13f79535-47bb-0310-9956-ffa450edef68 --- test/org/apache/catalina/valves/Benchmarks.java | 45 +++++++++++-------------- 1 file changed, 19 insertions(+), 26 deletions(-) diff --git a/test/org/apache/catalina/valves/Benchmarks.java b/test/org/apache/catalina/valves/Benchmarks.java index 61966ca26..2710b1af3 100644 --- a/test/org/apache/catalina/valves/Benchmarks.java +++ b/test/org/apache/catalina/valves/Benchmarks.java @@ -191,8 +191,7 @@ public class Benchmarks extends TestCase { return "Syncs"; } - private volatile long currentMillis = 0; - private volatile Date currentDate = null; + private volatile Date currentDate = new Date(); private String currentDateString = null; private SimpleDateFormat dayFormatter = new SimpleDateFormat("dd"); private SimpleDateFormat monthFormatter = new SimpleDateFormat("MM"); @@ -231,11 +230,10 @@ public class Benchmarks extends TestCase { private Date getDateSync() { long systime = System.currentTimeMillis(); - if ((systime - currentMillis) > 1000) { + if ((systime - currentDate.getTime()) > 1000) { synchronized (this) { - if ((systime - currentMillis) > 1000) { - currentDate = new Date(systime); - currentMillis = systime; + if ((systime - currentDate.getTime()) > 1000) { + currentDate.setTime(systime); } } } @@ -250,15 +248,13 @@ public class Benchmarks extends TestCase { return "ThreadLocals"; } - private volatile Date currentDate = null; - private String currentDateString = null; + private ThreadLocal currentDateStringLocal = new ThreadLocal(); - private ThreadLocal currentMillisLocal = new ThreadLocal() { - protected Long initialValue() { - return Long.valueOf(0); + private ThreadLocal currentDateLocal = new ThreadLocal() { + protected Date initialValue() { + return new Date(); } }; - private ThreadLocal currentDateLocal = new ThreadLocal(); private ThreadLocal dayFormatterLocal = new ThreadLocal() { protected SimpleDateFormat initialValue() { return new SimpleDateFormat("dd"); @@ -285,31 +281,28 @@ public class Benchmarks extends TestCase { } public String printDate() { - StringBuffer buf = new StringBuffer(); - Date date = getDateLocal(); - if (currentDate != date) { + getDateLocal(); + if (currentDateStringLocal.get() == null) { StringBuffer current = new StringBuffer(32); current.append('['); - current.append(dayFormatterLocal.get().format(date)); // Day + current.append(dayFormatterLocal.get().format(currentDateLocal.get())); // Day current.append('/'); - current.append(lookup(monthFormatterLocal.get().format(date))); // Month + current.append(lookup(monthFormatterLocal.get().format(currentDateLocal.get()))); // Month current.append('/'); - current.append(yearFormatterLocal.get().format(date)); // Year + current.append(yearFormatterLocal.get().format(currentDateLocal.get())); // Year current.append(':'); - current.append(timeFormatterLocal.get().format(date)); // Time + current.append(timeFormatterLocal.get().format(currentDateLocal.get())); // Time current.append(']'); - currentDateString = current.toString(); - currentDate = date; + currentDateStringLocal.set(current.toString()); } - buf.append(currentDateString); - return buf.toString(); + return currentDateStringLocal.get(); } private Date getDateLocal() { long systime = System.currentTimeMillis(); - if ((systime - currentMillisLocal.get().longValue()) > 1000) { - currentDateLocal.set(new Date(systime)); - currentMillisLocal.set(Long.valueOf(systime)); + if ((systime - currentDateLocal.get().getTime()) > 1000) { + currentDateLocal.get().setTime(systime); + currentDateStringLocal.set(null); } return currentDateLocal.get(); } -- 2.11.0