Repository: logging-log4j2 Updated Branches: refs/heads/release-2.x a9934b750 -> 641238d4e
Benchmarks for StringBuilders escapeJson/escapeXml Project: http://git-wip-us.apache.org/repos/asf/logging-log4j2/repo Commit: http://git-wip-us.apache.org/repos/asf/logging-log4j2/commit/641238d4 Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/641238d4 Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/641238d4 Branch: refs/heads/release-2.x Commit: 641238d4e69723d4a288be01c5d03e013cf72464 Parents: a9934b7 Author: Carter Kozak <[email protected]> Authored: Mon Jul 16 12:44:09 2018 -0400 Committer: Carter Kozak <[email protected]> Committed: Mon Jul 16 13:34:37 2018 -0400 ---------------------------------------------------------------------- .../perf/jmh/StringBuilderEscapeBenchmark.java | 78 ++++++++++++++++++++ 1 file changed, 78 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/641238d4/log4j-perf/src/main/java/org/apache/logging/log4j/perf/jmh/StringBuilderEscapeBenchmark.java ---------------------------------------------------------------------- diff --git a/log4j-perf/src/main/java/org/apache/logging/log4j/perf/jmh/StringBuilderEscapeBenchmark.java b/log4j-perf/src/main/java/org/apache/logging/log4j/perf/jmh/StringBuilderEscapeBenchmark.java new file mode 100644 index 0000000..865f925 --- /dev/null +++ b/log4j-perf/src/main/java/org/apache/logging/log4j/perf/jmh/StringBuilderEscapeBenchmark.java @@ -0,0 +1,78 @@ +/* + * 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 org.apache.logging.log4j.util.StringBuilders; +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; + +import java.util.concurrent.TimeUnit; + +/** + * This benchmark tests encoding implementations. + */ +// ============================== HOW TO RUN THIS TEST: ==================================== +// +// java -jar log4j-perf/target/benchmarks.jar ".*StringBuilderEscapeBenchmark.*" -f 1 -wi 5 -i 10 +// +// Usage help: +// java -jar log4j-perf/target/benchmarks.jar -help +// +@State(Scope.Benchmark) +public class StringBuilderEscapeBenchmark { + + private static final String EVERY_CHARACTER_MUST_BE_ESCAPED_JSON = repeat("\t\"", 1024); + private static final String EVERY_CHARACTER_MUST_BE_ESCAPED_XML = repeat("<\"&>", 512); + + @State(Scope.Thread) + public static class ThreadState { + StringBuilder buffer = new StringBuilder(1024 * 4); + } + + @Benchmark + @BenchmarkMode(Mode.SampleTime) + @OutputTimeUnit(TimeUnit.NANOSECONDS) + public int escapeJsonLargeString(final ThreadState state) { + state.buffer.setLength(0); + state.buffer.append(EVERY_CHARACTER_MUST_BE_ESCAPED_JSON); + StringBuilders.escapeJson(state.buffer, 0); + return state.buffer.length(); + } + + @Benchmark + @BenchmarkMode(Mode.SampleTime) + @OutputTimeUnit(TimeUnit.NANOSECONDS) + public int escapeXmlLargeString(final ThreadState state) { + state.buffer.setLength(0); + state.buffer.append(EVERY_CHARACTER_MUST_BE_ESCAPED_XML); + StringBuilders.escapeXml(state.buffer, 0); + return state.buffer.length(); + } + + private static String repeat(String str, int times) { + StringBuilder sb = new StringBuilder(str.length() * times); + for (int i = 0; i < times; i++) { + sb.append(str); + } + return sb.toString(); + } +}
