How to benchmark asynchronous operations with JMH

Viewed 803

I'm trying to benchmark an asynchronous operation that has the form:

void op(Callback cb);

I could benchmark using:

@Benchmark
void issueOp(BenchState state) {
    state.svc.op(nullCb);
}

but that only measures the time to issue the operation, not the completion time.

@Benchmark
void issueOp(BenchState state) {
   final CountDownLatch latch = new CountDownLatch(1);
   Callback signalCb = new Callback() {
       public void complete() {
           latch.countDown();
       }
   };
   state.svc.op(signalCb);
   signalCb.await();
}

does measure the completion time, but with only one op queued up at a time.

I would really like something like:

@AsyncBenchmark
void issueOp(BenchState state, final BenchmarkCompletion bc) {
    Callback cb = new Callback() {
        void complete() {
            bc.complete();
        }
    };
    state.svc.op(cb);
}

where BenchmarkCompletion is something provided by JMH to indicate that the operation is complete.

Is there anything like this in JMH?

1 Answers

You can write something like this:

  final private val OperationsPerInvocation = 10000
  @Benchmark
  @OperationsPerInvocation(OperationsPerInvocation)
  def schedulePeriodicallyBenchmark(): Unit = {
    val latch = new CountDownLatch(OperationsPerInvocation)
    timeServiceScheduler.schedulePeriodically(Duration.ofMillis(100)) {
      latch.countDown()
    }
    latch.await(20, TimeUnit.MINUTES)
  }
Related