|
| 1 | +package works.bosk; |
| 2 | + |
| 3 | +import java.util.concurrent.ExecutionException; |
| 4 | +import java.util.concurrent.ExecutorService; |
| 5 | +import java.util.concurrent.Executors; |
| 6 | +import java.util.concurrent.FutureTask; |
| 7 | +import org.openjdk.jmh.annotations.Benchmark; |
| 8 | +import org.openjdk.jmh.annotations.BenchmarkMode; |
| 9 | +import org.openjdk.jmh.annotations.Fork; |
| 10 | +import org.openjdk.jmh.annotations.Measurement; |
| 11 | +import org.openjdk.jmh.annotations.Scope; |
| 12 | +import org.openjdk.jmh.annotations.State; |
| 13 | +import org.openjdk.jmh.annotations.TearDown; |
| 14 | +import org.openjdk.jmh.annotations.Warmup; |
| 15 | + |
| 16 | +import static org.openjdk.jmh.annotations.Mode.Throughput; |
| 17 | + |
| 18 | +@Fork(1) |
| 19 | +@Warmup(iterations = 5, time = 1) |
| 20 | +@Measurement(iterations = 10, time = 1) |
| 21 | +@BenchmarkMode(Throughput) |
| 22 | +public class VirtualThreadBenchmark { |
| 23 | + |
| 24 | + @State(Scope.Benchmark) |
| 25 | + public static class BenchmarkState { |
| 26 | + final ExecutorService virtualThreads = Executors.newVirtualThreadPerTaskExecutor(); |
| 27 | + final ExecutorService realThreads = Executors.newThreadPerTaskExecutor(Thread::new); |
| 28 | + final ExecutorService pooledThreads = Executors.newFixedThreadPool(1); |
| 29 | + String result; |
| 30 | + |
| 31 | + @TearDown |
| 32 | + public void tearDown() { |
| 33 | + virtualThreads.close(); |
| 34 | + realThreads.close(); |
| 35 | + pooledThreads.close(); |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + @Benchmark |
| 40 | + public String baseline(BenchmarkState state) throws ExecutionException, InterruptedException { |
| 41 | + FutureTask<String> stringFutureTask = new FutureTask<>(() -> "hello"); |
| 42 | + stringFutureTask.run(); |
| 43 | + return stringFutureTask.get(); |
| 44 | + } |
| 45 | + |
| 46 | + @Benchmark |
| 47 | + public Object virtualThreadPool(BenchmarkState state) throws ExecutionException, InterruptedException { |
| 48 | + return state.virtualThreads.submit(()->"hello").get(); |
| 49 | + } |
| 50 | + |
| 51 | + @Benchmark |
| 52 | + public Object virtualThread(BenchmarkState state) throws InterruptedException { |
| 53 | + Thread.startVirtualThread(() -> state.result = "hello").join(); |
| 54 | + return state.result; |
| 55 | + } |
| 56 | + |
| 57 | + @Benchmark |
| 58 | + public Object platformThreadPool(BenchmarkState state) throws ExecutionException, InterruptedException { |
| 59 | + return state.realThreads.submit(()->"hello").get(); |
| 60 | + } |
| 61 | + |
| 62 | + @Benchmark |
| 63 | + public Object platformThread(BenchmarkState state) throws ExecutionException, InterruptedException { |
| 64 | + var thread = new Thread(() -> state.result = "hello"); |
| 65 | + thread.start(); |
| 66 | + thread.join(); |
| 67 | + return state.result; |
| 68 | + } |
| 69 | + |
| 70 | + @Benchmark |
| 71 | + public Object singlePooledThread(BenchmarkState state) throws ExecutionException, InterruptedException { |
| 72 | + return state.pooledThreads.submit(()->"hello").get(); |
| 73 | + } |
| 74 | + |
| 75 | +} |
0 commit comments