forked from micrometer-metrics/micrometer
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add benchmarks for DefaultLTT start/stop
Start and stop are called on the critical path. We should have benchmarks for them to evaluate changes that may affect their performance.
- Loading branch information
Showing
1 changed file
with
96 additions
and
0 deletions.
There are no files selected for viewing
96 changes: 96 additions & 0 deletions
96
...chmarks-core/src/jmh/java/io/micrometer/benchmark/core/DefaultLongTaskTimerBenchmark.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
/* | ||
* Copyright 2024 VMware, Inc. | ||
* | ||
* Licensed 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 | ||
* | ||
* https://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 io.micrometer.benchmark.core; | ||
|
||
import io.micrometer.core.instrument.*; | ||
import io.micrometer.core.instrument.distribution.DistributionStatisticConfig; | ||
import io.micrometer.core.instrument.internal.DefaultLongTaskTimer; | ||
import org.openjdk.jmh.annotations.*; | ||
import org.openjdk.jmh.annotations.Measurement; | ||
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; | ||
|
||
import java.util.concurrent.TimeUnit; | ||
import java.util.stream.IntStream; | ||
|
||
@State(Scope.Benchmark) | ||
@OutputTimeUnit(TimeUnit.MICROSECONDS) | ||
@Fork(1) | ||
public class DefaultLongTaskTimerBenchmark { | ||
|
||
public static void main(String[] args) throws RunnerException { | ||
Options opt = new OptionsBuilder().include(DefaultLongTaskTimerBenchmark.class.getSimpleName()).build(); | ||
|
||
new Runner(opt).run(); | ||
} | ||
|
||
private static final int sampleCount = 10_000; | ||
|
||
private MockClock clock; | ||
|
||
private DefaultLongTaskTimer longTaskTimer; | ||
|
||
private LongTaskTimer.Sample middleSample; | ||
|
||
@Setup(Level.Invocation) | ||
public void setup() { | ||
clock = new MockClock(); | ||
longTaskTimer = new DefaultLongTaskTimer( | ||
new Meter.Id("ltt", Tags.empty(), TimeUnit.MILLISECONDS.toString().toLowerCase(), null, | ||
Meter.Type.LONG_TASK_TIMER), | ||
clock, TimeUnit.MILLISECONDS, DistributionStatisticConfig.DEFAULT, false); | ||
// start some samples for benchmarking start/stop with active samples | ||
IntStream.range(0, sampleCount).forEach(offset -> { | ||
clock.add(offset, TimeUnit.MILLISECONDS); | ||
LongTaskTimer.Sample sample = longTaskTimer.start(); | ||
if (offset == sampleCount / 2) | ||
middleSample = sample; | ||
}); | ||
} | ||
|
||
@TearDown(Level.Invocation) | ||
public void tearDown() { | ||
longTaskTimer = null; | ||
} | ||
|
||
@Benchmark | ||
@Warmup(iterations = 20) | ||
@Measurement(iterations = 200) | ||
@BenchmarkMode(Mode.SingleShotTime) | ||
public LongTaskTimer.Sample start() { | ||
return longTaskTimer.start(); | ||
} | ||
|
||
@Benchmark | ||
@Warmup(iterations = 20) | ||
@Measurement(iterations = 200) | ||
@BenchmarkMode(Mode.SingleShotTime) | ||
public long stop() { | ||
return middleSample.stop(); | ||
} | ||
|
||
@Benchmark | ||
@Warmup(iterations = 20) | ||
@Measurement(iterations = 200) | ||
@BenchmarkMode(Mode.SingleShotTime) | ||
public long startAndStop() { | ||
start(); | ||
return stop(); | ||
} | ||
|
||
} |