Repository: logging-log4j2
Updated Branches:
  refs/heads/master 72ef0eb4c -> b38f0cb56


LOG4J2-812 added JMH benchmark

Project: http://git-wip-us.apache.org/repos/asf/logging-log4j2/repo
Commit: http://git-wip-us.apache.org/repos/asf/logging-log4j2/commit/e5484796
Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/e5484796
Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/e5484796

Branch: refs/heads/master
Commit: e54847961bfc139abbe8bf86885fda6951b40f59
Parents: 8d4dc1f
Author: rpopma <[email protected]>
Authored: Sun Jul 5 16:29:05 2015 +0900
Committer: rpopma <[email protected]>
Committed: Sun Jul 5 16:29:05 2015 +0900

----------------------------------------------------------------------
 .../perf/jmh/SimpleDateFormatBenchmark.java     | 81 ++++++++++++++++++++
 1 file changed, 81 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/e5484796/log4j-perf/src/main/java/org/apache/logging/log4j/perf/jmh/SimpleDateFormatBenchmark.java
----------------------------------------------------------------------
diff --git 
a/log4j-perf/src/main/java/org/apache/logging/log4j/perf/jmh/SimpleDateFormatBenchmark.java
 
b/log4j-perf/src/main/java/org/apache/logging/log4j/perf/jmh/SimpleDateFormatBenchmark.java
new file mode 100644
index 0000000..2e6e620
--- /dev/null
+++ 
b/log4j-perf/src/main/java/org/apache/logging/log4j/perf/jmh/SimpleDateFormatBenchmark.java
@@ -0,0 +1,81 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache license, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the license for the specific language governing permissions and
+ * limitations under the license.
+ */
+
+package org.apache.logging.log4j.perf.jmh;
+
+import java.text.SimpleDateFormat;
+import java.util.Date;
+import java.util.concurrent.TimeUnit;
+
+import org.openjdk.jmh.annotations.Benchmark;
+import org.openjdk.jmh.annotations.BenchmarkMode;
+import org.openjdk.jmh.annotations.Mode;
+import org.openjdk.jmh.annotations.OutputTimeUnit;
+import org.openjdk.jmh.annotations.Scope;
+import org.openjdk.jmh.annotations.State;
+
+/**
+ * Tests performance of various ways to use SimpleDateFormat in a thread-safe 
manner.
+ */
+// ============================== HOW TO RUN THIS TEST: 
====================================
+//
+// single thread:
+// java -jar log4j-perf/target/benchmarks.jar ".*SimpleDateFormat.*" -f 1 -wi 
5 -i 5
+//
+// multiple threads (for example, 4 threads):
+// java -jar log4j-perf/target/benchmarks.jar ".*SimpleDateFormat.*" -f 1 -wi 
5 -i 5 -t 4 -si true
+//
+// Usage help:
+// java -jar log4j-perf/target/benchmarks.jar -help
+//
+@State(Scope.Benchmark)
+public class SimpleDateFormatBenchmark {
+
+    private final Date date = new Date();
+    private SimpleDateFormat simpleDateFormat = new 
SimpleDateFormat("HH:mm:ss.SSS");
+    private ThreadLocal<SimpleDateFormat> threadLocal = new 
ThreadLocal<SimpleDateFormat>() {
+        @Override
+        protected SimpleDateFormat initialValue() {
+            return new SimpleDateFormat("HH:mm:ss.SSS");
+        }
+    };
+
+    public static void main(final String[] args) {
+    }
+
+    @Benchmark
+    @BenchmarkMode(Mode.SampleTime)
+    @OutputTimeUnit(TimeUnit.NANOSECONDS)
+    public void baseline() {
+    }
+
+    @Benchmark
+    @BenchmarkMode(Mode.SampleTime)
+    @OutputTimeUnit(TimeUnit.NANOSECONDS)
+    public String synchronizedFormat() {
+        synchronized (simpleDateFormat) {
+            return simpleDateFormat.format(date);
+        }
+    }
+
+    @Benchmark
+    @BenchmarkMode(Mode.SampleTime)
+    @OutputTimeUnit(TimeUnit.NANOSECONDS)
+    public String threadLocalFormat() {
+        return threadLocal.get().format(date);
+    }
+}

Reply via email to