From f87711b2daf083e42708d6dd1143cbb72616e8a0 Mon Sep 17 00:00:00 2001 From: Joakim Erdfelt Date: Thu, 16 Jul 2020 13:30:47 -0500 Subject: [PATCH] Adding jmh test to compare String.substring() vs String.repeat() Signed-off-by: Joakim Erdfelt --- .../jetty/logging/jmh/IndentBenchmark.java | 86 +++++++++++++++++++ 1 file changed, 86 insertions(+) create mode 100644 tests/jetty-jmh/src/main/java/org/eclipse/jetty/logging/jmh/IndentBenchmark.java diff --git a/tests/jetty-jmh/src/main/java/org/eclipse/jetty/logging/jmh/IndentBenchmark.java b/tests/jetty-jmh/src/main/java/org/eclipse/jetty/logging/jmh/IndentBenchmark.java new file mode 100644 index 00000000000..913716707c4 --- /dev/null +++ b/tests/jetty-jmh/src/main/java/org/eclipse/jetty/logging/jmh/IndentBenchmark.java @@ -0,0 +1,86 @@ +// +// ======================================================================== +// Copyright (c) 1995-2020 Mort Bay Consulting Pty Ltd and others. +// +// This program and the accompanying materials are made available under +// the terms of the Eclipse Public License 2.0 which is available at +// https://www.eclipse.org/legal/epl-2.0 +// +// This Source Code may also be made available under the following +// Secondary Licenses when the conditions for such availability set +// forth in the Eclipse Public License, v. 2.0 are satisfied: +// the Apache License v2.0 which is available at +// https://www.apache.org/licenses/LICENSE-2.0 +// +// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 +// ======================================================================== +// + +package org.eclipse.jetty.logging.jmh; + +import java.util.concurrent.TimeUnit; + +import org.openjdk.jmh.annotations.Benchmark; +import org.openjdk.jmh.annotations.BenchmarkMode; +import org.openjdk.jmh.annotations.Measurement; +import org.openjdk.jmh.annotations.Mode; +import org.openjdk.jmh.annotations.Scope; +import org.openjdk.jmh.annotations.State; +import org.openjdk.jmh.annotations.Threads; +import org.openjdk.jmh.annotations.Warmup; +import org.openjdk.jmh.infra.Blackhole; +import org.openjdk.jmh.profile.GCProfiler; +import org.openjdk.jmh.runner.Runner; +import org.openjdk.jmh.runner.RunnerException; +import org.openjdk.jmh.runner.options.Options; +import org.openjdk.jmh.runner.options.OptionsBuilder; + +@State(Scope.Benchmark) +@Threads(4) +@Warmup(iterations = 7, time = 500, timeUnit = TimeUnit.MILLISECONDS) +@Measurement(iterations = 7, time = 500, timeUnit = TimeUnit.MILLISECONDS) +public class IndentBenchmark +{ + private static final int SMALL = 13; + private static final int LARGE = 43; + private String bigstring = " "; + + @Benchmark + @BenchmarkMode(Mode.Throughput) + public void testStringSubStringSmall(Blackhole blackhole) + { + blackhole.consume(bigstring.substring(0, SMALL)); + } + + @Benchmark + @BenchmarkMode(Mode.Throughput) + public void testStringSubStringLarge(Blackhole blackhole) + { + blackhole.consume(bigstring.substring(0, LARGE)); + } + + @Benchmark + @BenchmarkMode(Mode.Throughput) + public void testStringRepeatSmall(Blackhole blackhole) + { + blackhole.consume(" " .repeat(SMALL)); + } + + @Benchmark + @BenchmarkMode(Mode.Throughput) + public void testStringRepeatLarge(Blackhole blackhole) + { + blackhole.consume(" " .repeat(LARGE)); + } + + public static void main(String[] args) throws RunnerException + { + Options opt = new OptionsBuilder() + .include(IndentBenchmark.class.getSimpleName()) + .addProfiler(GCProfiler.class) + .forks(1) + .build(); + + new Runner(opt).run(); + } +}