11package io .netty .loom .benchmark ;
22
33import io .netty .channel .nio .NioIoHandler ;
4- import io .netty .loom .LoomSupport ;
54import io .netty .loom .VirtualMultithreadIoEventLoopGroup ;
65import org .openjdk .jmh .annotations .Benchmark ;
76import org .openjdk .jmh .annotations .BenchmarkMode ;
1514import org .openjdk .jmh .annotations .State ;
1615import org .openjdk .jmh .annotations .TearDown ;
1716import org .openjdk .jmh .annotations .Warmup ;
17+ import org .openjdk .jmh .infra .BenchmarkParams ;
1818
1919import java .util .concurrent .CountDownLatch ;
2020import java .util .concurrent .ExecutionException ;
2525@ OutputTimeUnit (TimeUnit .MILLISECONDS )
2626@ Warmup (iterations = 3 , time = 1 )
2727@ Measurement (iterations = 5 , time = 1 )
28- @ Fork (value = 2 , jvmArgs = {"--add-opens=java.base/java.lang=ALL-UNNAMED" , "-XX:+UnlockExperimentalVMOptions" ,
29- "-XX:-DoJVMTIVirtualThreadTransitions" , "-Djdk.trackAllThreads=false" ,
30- "-Djdk.virtualThreadScheduler.implClass=io.netty.loom.NettyScheduler" })
3128@ State (Scope .Thread )
3229public class NettySchedulerBenchmark {
3330
@@ -39,13 +36,18 @@ public class NettySchedulerBenchmark {
3936 private ThreadFactory vtFactory ;
4037
4138 @ Setup
42- public void setup () throws ExecutionException , InterruptedException {
43- executorGroup = new VirtualMultithreadIoEventLoopGroup (1 , NioIoHandler .newFactory ());
44- vtFactory = executorGroup .submit (executorGroup ::vThreadFactory ).get ();
39+ public void setup (BenchmarkParams params ) throws ExecutionException , InterruptedException {
40+ if (params .getBenchmark ().contains ("Netty" )) {
41+ executorGroup = new VirtualMultithreadIoEventLoopGroup (1 , NioIoHandler .newFactory ());
42+ vtFactory = executorGroup .submit (executorGroup ::vThreadFactory ).get ();
43+ }
4544 }
4645
4746 @ Benchmark
48- public void global () {
47+ @ Fork (value = 2 , jvmArgs = {"--add-opens=java.base/java.lang=ALL-UNNAMED" , "-XX:+UnlockExperimentalVMOptions" ,
48+ "-XX:-DoJVMTIVirtualThreadTransitions" , "-Djdk.trackAllThreads=false" ,
49+ "-Djdk.virtualThreadScheduler.implClass=io.netty.loom.NettyScheduler" , "-Djdk.pollerMode=3" })
50+ public void scheduleToFjFromNetty () {
4951 CountDownLatch countDown = new CountDownLatch (tasks );
5052 vtFactory .newThread (() -> {
5153 for (int i = 0 ; i < tasks ; i ++) {
@@ -60,15 +62,49 @@ public void global() {
6062 }
6163
6264 @ Benchmark
63- public void inheritFromParent () {
65+ @ Fork (value = 2 , jvmArgs = {"--add-opens=java.base/java.lang=ALL-UNNAMED" , "-XX:+UnlockExperimentalVMOptions" ,
66+ "-XX:-DoJVMTIVirtualThreadTransitions" , "-Djdk.trackAllThreads=false" })
67+ public void scheduleToFjFromFjWithBuiltInScheduler () {
68+ CountDownLatch countDown = new CountDownLatch (tasks );
69+ Thread .startVirtualThread (() -> {
70+ for (int i = 0 ; i < tasks ; i ++) {
71+ Thread .startVirtualThread (countDown ::countDown );
72+ }
73+ }).start ();
74+ try {
75+ countDown .await ();
76+ } catch (InterruptedException e ) {
77+ throw new RuntimeException (e );
78+ }
79+ }
80+
81+ @ Benchmark
82+ @ Fork (value = 2 , jvmArgs = {"--add-opens=java.base/java.lang=ALL-UNNAMED" , "-XX:+UnlockExperimentalVMOptions" ,
83+ "-XX:-DoJVMTIVirtualThreadTransitions" , "-Djdk.trackAllThreads=false" ,
84+ "-Djdk.virtualThreadScheduler.implClass=io.netty.loom.NettyScheduler" , "-Djdk.pollerMode=3" })
85+ public void scheduleToFjFromFjWithCustomScheduler () {
86+ CountDownLatch countDown = new CountDownLatch (tasks );
87+ Thread .startVirtualThread (() -> {
88+ for (int i = 0 ; i < tasks ; i ++) {
89+ Thread .startVirtualThread (countDown ::countDown );
90+ }
91+ }).start ();
92+ try {
93+ countDown .await ();
94+ } catch (InterruptedException e ) {
95+ throw new RuntimeException (e );
96+ }
97+ }
98+
99+ @ Benchmark
100+ @ Fork (value = 2 , jvmArgs = {"--add-opens=java.base/java.lang=ALL-UNNAMED" , "-XX:+UnlockExperimentalVMOptions" ,
101+ "-XX:-DoJVMTIVirtualThreadTransitions" , "-Djdk.trackAllThreads=false" ,
102+ "-Djdk.virtualThreadScheduler.implClass=io.netty.loom.NettyScheduler" , "-Djdk.pollerMode=3" })
103+ public void scheduleToNettyFromNetty () {
64104 CountDownLatch countDown = new CountDownLatch (tasks );
65105 vtFactory .newThread (() -> {
66- Thread .VirtualThreadScheduler parentScheduler = LoomSupport .getScheduler (Thread .currentThread ());
67- // Simulate the behavior of the previous version
68- // get the scheduler from the parent thread before starting, instead of
69- // pre-initializing the `vtFactory`.
70106 for (int i = 0 ; i < tasks ; i ++) {
71- Thread . ofVirtual (). scheduler ( parentScheduler ). start ( countDown ::countDown );
107+ vtFactory . newThread ( countDown ::countDown ). start ( );
72108 }
73109 }).start ();
74110 try {
@@ -80,6 +116,8 @@ public void inheritFromParent() {
80116
81117 @ TearDown
82118 public void tearDown () {
83- executorGroup .shutdownGracefully ();
119+ if (executorGroup != null ) {
120+ executorGroup .shutdownGracefully ();
121+ }
84122 }
85123}
0 commit comments