You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@logging.apache.org by rp...@apache.org on 2015/08/17 18:14:16 UTC
logging-log4j2 git commit: changed TimeFormatBenchmark to properly
handle multi-threaded tests
Repository: logging-log4j2
Updated Branches:
refs/heads/master f672e2834 -> 79158906a
changed TimeFormatBenchmark to properly handle multi-threaded tests
Project: http://git-wip-us.apache.org/repos/asf/logging-log4j2/repo
Commit: http://git-wip-us.apache.org/repos/asf/logging-log4j2/commit/79158906
Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/79158906
Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/79158906
Branch: refs/heads/master
Commit: 79158906a56a2548d2791c5c8d15785b8dab0f44
Parents: f672e28
Author: rpopma <rp...@apache.org>
Authored: Tue Aug 18 01:14:23 2015 +0900
Committer: rpopma <rp...@apache.org>
Committed: Tue Aug 18 01:14:23 2015 +0900
----------------------------------------------------------------------
.../log4j/perf/jmh/TimeFormatBenchmark.java | 86 ++++++++++++++++++--
1 file changed, 77 insertions(+), 9 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/79158906/log4j-perf/src/main/java/org/apache/logging/log4j/perf/jmh/TimeFormatBenchmark.java
----------------------------------------------------------------------
diff --git a/log4j-perf/src/main/java/org/apache/logging/log4j/perf/jmh/TimeFormatBenchmark.java b/log4j-perf/src/main/java/org/apache/logging/log4j/perf/jmh/TimeFormatBenchmark.java
index bbdee4d..6813ca1 100644
--- a/log4j-perf/src/main/java/org/apache/logging/log4j/perf/jmh/TimeFormatBenchmark.java
+++ b/log4j-perf/src/main/java/org/apache/logging/log4j/perf/jmh/TimeFormatBenchmark.java
@@ -46,17 +46,24 @@ import org.openjdk.jmh.annotations.State;
// Usage help:
// java -jar log4j-perf/target/benchmarks.jar -help
//
-@State(Scope.Thread)
+@State(Scope.Benchmark)
public class TimeFormatBenchmark {
- SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm:ss.SSS");
+ ThreadLocal<SimpleDateFormat> threadLocalSimpleDateFormat = new ThreadLocal<SimpleDateFormat>() {
+ @Override
+ protected SimpleDateFormat initialValue() {
+ return new SimpleDateFormat("HH:mm:ss.SSS");
+ }
+ };
+ // SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm:ss.SSS");
FastDateFormat fastDateFormat = FastDateFormat.getInstance("HH:mm:ss.SSS");
- long midnightToday = 0;
- long midnightTomorrow = 0;
+ volatile long midnightToday = 0;
+ volatile long midnightTomorrow = 0;
@State(Scope.Thread)
public static class BufferState {
ByteBuffer buffer = ByteBuffer.allocate(12);
+ StringBuilder stringBuilder = new StringBuilder(12);
}
private long millisSinceMidnight(final long now) {
@@ -93,14 +100,14 @@ public class TimeFormatBenchmark {
@BenchmarkMode(Mode.SampleTime)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
public String simpleDateFormatString() {
- return simpleDateFormat.format(new Date());
+ return threadLocalSimpleDateFormat.get().format(new Date());
}
@Benchmark
@BenchmarkMode(Mode.SampleTime)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
public int simpleDateFormatBytes(final BufferState state) {
- final String str = simpleDateFormat.format(new Date());
+ final String str = threadLocalSimpleDateFormat.get().format(new Date());
final byte[] bytes = str.getBytes(StandardCharsets.UTF_8);
state.buffer.clear();
state.buffer.put(bytes);
@@ -147,9 +154,18 @@ public class TimeFormatBenchmark {
@BenchmarkMode(Mode.SampleTime)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
public String customFormatString(final BufferState state) {
- state.buffer.clear();
- format(System.currentTimeMillis(), state.buffer);
- return new String(state.buffer.array(), 0, state.buffer.position(), StandardCharsets.UTF_8);
+ state.stringBuilder.setLength(0);
+ formatText(System.currentTimeMillis(), state.stringBuilder);
+ return new String(state.stringBuilder);
+ }
+
+ @Benchmark
+ @BenchmarkMode(Mode.SampleTime)
+ @OutputTimeUnit(TimeUnit.NANOSECONDS)
+ public int customFormatStringBuilder(final BufferState state) {
+ state.stringBuilder.setLength(0);
+ formatText(System.currentTimeMillis(), state.stringBuilder);
+ return state.stringBuilder.length();
}
@Benchmark
@@ -219,6 +235,58 @@ public class TimeFormatBenchmark {
return buffer;
}
+ public StringBuilder formatText(final long time, final StringBuilder buffer) {
+ // Calculate values by getting the ms values first and do then
+ // calculate the hour minute and second values divisions.
+
+ // Get daytime in ms which does fit into an int
+ // int ms = (int) (time % 86400000);
+ int ms = (int) (millisSinceMidnight(time));
+
+ final int hours = ms / 3600000;
+ ms -= 3600000 * hours;
+
+ final int minutes = ms / 60000;
+ ms -= 60000 * minutes;
+
+ final int seconds = ms / 1000;
+ ms -= 1000 * seconds;
+
+ // Hour
+ int temp = hours / 10;
+ buffer.append((char) (temp + '0'));
+
+ // Do subtract to get remainder instead of doing % 10
+ buffer.append((char) (hours - 10 * temp + '0'));
+ buffer.append((char) ':');
+
+ // Minute
+ temp = minutes / 10;
+ buffer.append((char) (temp + '0'));
+
+ // Do subtract to get remainder instead of doing % 10
+ buffer.append((char) (minutes - 10 * temp + '0'));
+ buffer.append((char) ':');
+
+ // Second
+ temp = seconds / 10;
+ buffer.append((char) (temp + '0'));
+ buffer.append((char) (seconds - 10 * temp + '0'));
+ buffer.append((char) '.');
+
+ // Millisecond
+ temp = ms / 100;
+ buffer.append((char) (temp + '0'));
+
+ ms -= 100 * temp;
+ temp = ms / 10;
+ buffer.append((char) (temp + '0'));
+
+ ms -= 10 * temp;
+ buffer.append((char) (ms + '0'));
+ return buffer;
+ }
+
public ByteBuffer format(final long time, final ByteBuffer buffer) {
// Calculate values by getting the ms values first and do then
// calculate the hour minute and second values divisions.