added 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/59cc8e58 Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/59cc8e58 Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/59cc8e58
Branch: refs/heads/LOG4J2-1161 Commit: 59cc8e58fc91884fedb4795abe85eca0832b49f5 Parents: 3195cda Author: rpopma <rpo...@apache.org> Authored: Fri Oct 23 00:33:17 2015 +0900 Committer: rpopma <rpo...@apache.org> Committed: Fri Oct 23 00:33:17 2015 +0900 ---------------------------------------------------------------------- ...ThreadLocalVsConcurrentHashMapBenchmark.java | 94 ++++++++++++++++++++ 1 file changed, 94 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/59cc8e58/log4j-perf/src/main/java/org/apache/logging/log4j/perf/jmh/ThreadLocalVsConcurrentHashMapBenchmark.java ---------------------------------------------------------------------- diff --git a/log4j-perf/src/main/java/org/apache/logging/log4j/perf/jmh/ThreadLocalVsConcurrentHashMapBenchmark.java b/log4j-perf/src/main/java/org/apache/logging/log4j/perf/jmh/ThreadLocalVsConcurrentHashMapBenchmark.java new file mode 100644 index 0000000..7ba0ca4 --- /dev/null +++ b/log4j-perf/src/main/java/org/apache/logging/log4j/perf/jmh/ThreadLocalVsConcurrentHashMapBenchmark.java @@ -0,0 +1,94 @@ +/* + * 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.util.concurrent.ConcurrentHashMap; + +import org.openjdk.jmh.annotations.Benchmark; +import org.openjdk.jmh.annotations.Scope; +import org.openjdk.jmh.annotations.State; + +/** + * Compares performance of ThreadLocal, vs ConcurrentHashMap<Thread, Object>. + */ +// ============================== HOW TO RUN THIS TEST: ==================================== +// (Quick build: mvn -DskipTests=true clean package -pl log4j-perf -am ) +// +// single thread: +// java -jar log4j-perf/target/benchmarks.jar ".*ThreadLocalVsConcurrentHashMap.*" -f 1 -wi 10 -i 20 -tu ns -bm sample +// +// four threads: +// java -jar log4j-perf/target/benchmarks.jar ".*ThreadLocalVsConcurrentHashMap.*" -f 1 -wi 10 -i 20 -tu ns -bm sample +// -t 4 +// +// Usage help: +// java -jar log4j-perf/target/benchmarks.jar -help +// +@State(Scope.Benchmark) +public class ThreadLocalVsConcurrentHashMapBenchmark { + + private static final String VALUE = "value"; + private static ConcurrentHashMap<Thread, StringBuilder> map = new ConcurrentHashMap<Thread, StringBuilder>(); + private static ThreadLocal<StringBuilder> threadLocal = new ThreadLocal<>(); + + @Benchmark + public String newInstance() { + StringBuilder sb = getNew(); + sb.append(VALUE); + return sb.toString(); + } + + @Benchmark + public String threadLocal() { + StringBuilder sb = getThreadLocal(); + sb.append(VALUE); + return sb.toString(); + } + + @Benchmark + public String concurrentHashMap() { + StringBuilder sb = getConcurrentMap(); + sb.append(VALUE); + return sb.toString(); + } + + private StringBuilder getNew() { + final StringBuilder buf = new StringBuilder(); + return buf; + } + + private StringBuilder getThreadLocal() { + StringBuilder buf = threadLocal.get(); + if (buf == null) { + buf = new StringBuilder(); + threadLocal.set(buf); + } + buf.setLength(0); + return buf; + } + + private StringBuilder getConcurrentMap() { + StringBuilder buf = map.get(Thread.currentThread()); + if (buf == null) { + buf = new StringBuilder(); + map.put(Thread.currentThread(), buf); + } + buf.setLength(0); + return buf; + } +} \ No newline at end of file