Adding jmh test to compare String.substring() vs String.repeat()

Signed-off-by: Joakim Erdfelt <joakim.erdfelt@gmail.com>
This commit is contained in:
Joakim Erdfelt 2020-07-16 13:30:47 -05:00
parent c564c9647e
commit f87711b2da
No known key found for this signature in database
GPG Key ID: 2D0E1FB8FE4B68B4
1 changed files with 86 additions and 0 deletions

View File

@ -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();
}
}