Skip to content

Commit

Permalink
Stop a random sample instead of middle sample
Browse files Browse the repository at this point in the history
We should get a better idea of average performance with a random sample rather than a sample in a fixed position in the active tasks collection.
  • Loading branch information
shakuzen committed Oct 16, 2024
1 parent f9832ee commit a279c1a
Showing 1 changed file with 11 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.openjdk.jmh.runner.options.Options;
import org.openjdk.jmh.runner.options.OptionsBuilder;

import java.util.Random;
import java.util.concurrent.TimeUnit;
import java.util.stream.IntStream;

Expand All @@ -41,11 +42,13 @@ public static void main(String[] args) throws RunnerException {

private static final int sampleCount = 10_000;

private static final Random random = new Random();

private MockClock clock;

private DefaultLongTaskTimer longTaskTimer;

private LongTaskTimer.Sample middleSample;
private LongTaskTimer.Sample randomSample;

@Setup(Level.Invocation)
public void setup() {
Expand All @@ -54,13 +57,15 @@ public void setup() {
new Meter.Id("ltt", Tags.empty(), TimeUnit.MILLISECONDS.toString().toLowerCase(), null,
Meter.Type.LONG_TASK_TIMER),
clock, TimeUnit.MILLISECONDS, DistributionStatisticConfig.DEFAULT, false);
int randomIndex = random.nextInt(sampleCount);
// 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;
if (offset == randomIndex)
randomSample = sample;
});
clock.add(1, TimeUnit.MILLISECONDS);
}

@TearDown(Level.Invocation)
Expand All @@ -80,8 +85,8 @@ public LongTaskTimer.Sample start() {
@Warmup(iterations = 20)
@Measurement(iterations = 200)
@BenchmarkMode(Mode.SingleShotTime)
public long stop() {
return middleSample.stop();
public long stopRandom() {
return randomSample.stop();
}

@Benchmark
Expand All @@ -90,7 +95,7 @@ public long stop() {
@BenchmarkMode(Mode.SingleShotTime)
public long startAndStop() {
start();
return stop();
return stopRandom();
}

}

0 comments on commit a279c1a

Please sign in to comment.