2525import java .io .IOException ;
2626import java .io .PrintWriter ;
2727import java .io .StringWriter ;
28+ import java .time .Duration ;
2829import java .util .Objects ;
2930import java .util .concurrent .CompletableFuture ;
3031import java .util .concurrent .ForkJoinPool ;
3536class VakoTest {
3637 @ ParameterizedTest
3738 @ CsvSource ({
38- "false, 30 " ,
39- "true, 30 "
39+ "false, 20 " ,
40+ "true, 20 "
4041 })
4142 void asyncCallbackLoop (final boolean optimized , final int iterations ) throws Exception {
4243 System .err .printf ("baselineStackDepth=%d%n" , Thread .currentThread ().getStackTrace ().length );
@@ -60,47 +61,62 @@ void asyncCallbackLoop(final boolean optimized, final int iterations) throws Exc
6061 join .get ();
6162 }
6263
63- @ ParameterizedTest
64+ @ ParameterizedTest ()
6465 @ CsvSource ({
65- "false, false, 30 " ,
66- "false, true, 30 " ,
67- "true, false, 30 " ,
68- "true, true, 30 "
66+ "false, false, 0, 20 " ,
67+ "false, true, 4, 20 " ,
68+ "true, false, 0, 20 " ,
69+ "true, true, 4, 20 "
6970 })
70- void testThenRunDoWhileLoop (final boolean optimized , final boolean separateThread , final int counterInitialValue ) throws Exception {
71+ void testThenRunDoWhileLoop (
72+ final boolean optimized ,
73+ final boolean separateThread ,
74+ final int sleepSeconds ,
75+ final int counterInitialValue ) throws Exception {
76+ Duration totalSleepDuration = Duration .ofSeconds (sleepSeconds );
7177 StartTime start = StartTime .now ();
7278 System .err .printf ("baselineStackDepth=%d%n" , Thread .currentThread ().getStackTrace ().length );
7379 CompletableFuture <Void > join = new CompletableFuture <>();
74- asyncMethod1 (optimized , separateThread , new Counter (counterInitialValue ), (r , t ) -> {
75- System .err .printf ("test callback completed callStackDepth=%s, r=%s, t=%s%n" ,
80+ asyncMethod1 (optimized , separateThread , totalSleepDuration , new Counter (counterInitialValue ), (r , t ) -> {
81+ System .err .printf ("TEST callback completed callStackDepth=%s, r=%s, t=%s%n" ,
7682 Thread .currentThread ().getStackTrace ().length , r , exceptionToString (t ));
7783 if (t != null ) {
7884 join .completeExceptionally (t );
7985 } else {
8086 join .complete (r );
8187 }
8288 });
83- System .err .printf ("asyncMethod1 executed in %s%n" , start .elapsed ());
89+ System .err .printf ("asyncMethod1 returned in %s%n" , start .elapsed ());
8490 join .get ();
91+ sleep (Duration .ofSeconds (1 ));
92+ System .err .printf ("%nDONE%n" );
8593 }
8694
87- private static void asyncMethod1 (final boolean optimized , final boolean separateThread ,
88- final Counter counter , final SingleResultCallback <Void > callback ) {
95+ private static void asyncMethod1 (
96+ final boolean optimized ,
97+ final boolean separateThread ,
98+ final Duration totalSleepDuration ,
99+ final Counter counter ,
100+ final SingleResultCallback <Void > callback ) {
89101 beginAsync ().thenRunDoWhileLoop (optimized , c -> {
90- asyncMethod2 (separateThread , counter , c );
102+ // sleep(totalSleepDuration.dividedBy(counter.initial()));
103+ StartTime start = StartTime .now ();
104+ asyncMethod2 (separateThread , totalSleepDuration , counter , c );
105+ System .err .printf ("asyncMethod2 returned in %s%n" , start .elapsed ());
91106 }, () -> !counter .done ()).finish (callback );
92107 }
93108
94- private static void asyncMethod2 (final boolean separateThread , final Counter counter , final SingleResultCallback <Void > callback ) {
109+ private static void asyncMethod2 (
110+ final boolean separateThread ,
111+ final Duration totalSleepDuration ,
112+ final Counter counter ,
113+ final SingleResultCallback <Void > callback ) {
95114 Runnable action = () -> {
96- try {
97- Thread .sleep (TimeUnit .SECONDS .toMillis (1 ) / counter .initial ());
98- } catch (InterruptedException e ) {
99- Thread .currentThread ().interrupt ();
100- throw new RuntimeException (e );
101- }
115+ sleep (totalSleepDuration .dividedBy (counter .initial ()));
102116 counter .countDown ();
117+ StartTime start = StartTime .now ();
103118 callback .complete (callback );
119+ System .err .printf ("asyncMethod2 callback.complete returned in %s%n" , start .elapsed ());
104120 };
105121 if (separateThread ) {
106122 ForkJoinPool .commonPool ().execute (action );
@@ -152,4 +168,21 @@ private static String exceptionToString(@Nullable final Throwable t) {
152168 throw new RuntimeException (e );
153169 }
154170 }
171+
172+ private static void sleep (Duration duration ) {
173+ if (duration .isZero ()) {
174+ return ;
175+ }
176+ try {
177+ long durationNsPart = duration .getNano ();
178+ long durationMsPartFromNsPart = TimeUnit .MILLISECONDS .convert (duration .getNano (), TimeUnit .NANOSECONDS );
179+ long sleepMs = TimeUnit .MILLISECONDS .convert (duration .getSeconds (), TimeUnit .SECONDS ) + durationMsPartFromNsPart ;
180+ int sleepNs = Math .toIntExact (durationNsPart - TimeUnit .NANOSECONDS .convert (durationMsPartFromNsPart , TimeUnit .MILLISECONDS ));
181+ System .err .printf ("sleeping for %d ms %d ns%n" , sleepMs , sleepNs );
182+ Thread .sleep (sleepMs , sleepNs );
183+ } catch (InterruptedException e ) {
184+ Thread .currentThread ().interrupt ();
185+ throw new RuntimeException (e );
186+ }
187+ }
155188}
0 commit comments