From 650ca9a98604f79a48835f13d25b9ba0ec3aef35 Mon Sep 17 00:00:00 2001 From: Brad Baker Date: Fri, 23 Feb 2024 13:23:27 +1100 Subject: [PATCH] Added Async benchmark for future changes --- src/test/java/benchmark/AsyncBenchmark.java | 78 +++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 src/test/java/benchmark/AsyncBenchmark.java diff --git a/src/test/java/benchmark/AsyncBenchmark.java b/src/test/java/benchmark/AsyncBenchmark.java new file mode 100644 index 0000000000..84d0ee523a --- /dev/null +++ b/src/test/java/benchmark/AsyncBenchmark.java @@ -0,0 +1,78 @@ +package benchmark; + +import graphql.execution.Async; +import org.openjdk.jmh.annotations.Benchmark; +import org.openjdk.jmh.annotations.BenchmarkMode; +import org.openjdk.jmh.annotations.Level; +import org.openjdk.jmh.annotations.Measurement; +import org.openjdk.jmh.annotations.Mode; +import org.openjdk.jmh.annotations.Param; +import org.openjdk.jmh.annotations.Scope; +import org.openjdk.jmh.annotations.Setup; +import org.openjdk.jmh.annotations.State; +import org.openjdk.jmh.annotations.Warmup; +import org.openjdk.jmh.runner.Runner; +import org.openjdk.jmh.runner.options.Options; +import org.openjdk.jmh.runner.options.OptionsBuilder; + +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeUnit; + +@State(Scope.Benchmark) +@BenchmarkMode(Mode.Throughput) +@Warmup(iterations = 2) +@Measurement(iterations = 2, timeUnit = TimeUnit.NANOSECONDS) +public class AsyncBenchmark { + + @Param({"0", "1", "10"}) + public int num; + + List> futures; + + @Setup(Level.Trial) + public void setUp() throws ExecutionException, InterruptedException { + futures = new ArrayList<>(); + for (int i = 0; i < num; i++) { + futures.add(mkFuture(i)); + } + + } + + private CompletableFuture mkFuture(int i) { + // half will take some time + if (i % 2 == 0) { + return CompletableFuture.supplyAsync(() -> sleep(i)); + } else { + return CompletableFuture.completedFuture(i); + } + } + + private Object sleep(int i) { + try { + Thread.sleep(i * 1000L); + } catch (InterruptedException e) { + throw new RuntimeException(e); + } + return i; + } + + @Benchmark + public List benchmarkAsync() { + Async.CombinedBuilder builder = Async.ofExpectedSize(futures.size()); + futures.forEach(builder::add); + return builder.await().join(); + } + + public static void main(String[] args) throws Exception { + Options opt = new OptionsBuilder() + .include("benchmark.AsyncBenchmark") + .forks(5) + .build(); + + new Runner(opt).run(); + } + +}