Skip to content

Commit

Permalink
AverageTime BenchmarkMode instead of single-shot
Browse files Browse the repository at this point in the history
Using the invocation-level setup method, we can use average-time rather than single shot.
  • Loading branch information
shakuzen committed Oct 16, 2024
1 parent a279c1a commit fe05dbe
Showing 1 changed file with 8 additions and 24 deletions.
Original file line number Diff line number Diff line change
@@ -32,6 +32,9 @@
@State(Scope.Benchmark)
@OutputTimeUnit(TimeUnit.MICROSECONDS)
@Fork(1)
@BenchmarkMode(Mode.AverageTime)
@Warmup(iterations = 3)
@Measurement(iterations = 3)
public class DefaultLongTaskTimerBenchmark {

public static void main(String[] args) throws RunnerException {
@@ -40,10 +43,11 @@ public static void main(String[] args) throws RunnerException {
new Runner(opt).run();
}

private static final int sampleCount = 10_000;

private static final Random random = new Random();

@Param({ "10000", "100000" })
int activeSampleCount;

private MockClock clock;

private DefaultLongTaskTimer longTaskTimer;
@@ -57,9 +61,9 @@ 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);
int randomIndex = random.nextInt(activeSampleCount);
// start some samples for benchmarking start/stop with active samples
IntStream.range(0, sampleCount).forEach(offset -> {
IntStream.range(0, activeSampleCount).forEach(offset -> {
clock.add(offset, TimeUnit.MILLISECONDS);
LongTaskTimer.Sample sample = longTaskTimer.start();
if (offset == randomIndex)
@@ -68,34 +72,14 @@ public void setup() {
clock.add(1, TimeUnit.MILLISECONDS);
}

@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 stopRandom() {
return randomSample.stop();
}

@Benchmark
@Warmup(iterations = 20)
@Measurement(iterations = 200)
@BenchmarkMode(Mode.SingleShotTime)
public long startAndStop() {
start();
return stopRandom();
}

}

0 comments on commit fe05dbe

Please sign in to comment.