58
58
import io .netty .buffer .ByteBufAllocator ;
59
59
import io .netty .buffer .ByteBufAllocatorMetric ;
60
60
import io .netty .buffer .ByteBufAllocatorMetricProvider ;
61
+ import io .netty .channel .EventLoopGroup ;
62
+ import io .netty .channel .nio .NioEventLoopGroup ;
61
63
import io .netty .handler .ssl .SslContextBuilder ;
62
64
import io .netty .handler .ssl .SslHandler ;
63
65
import io .netty .util .internal .PlatformDependent ;
@@ -491,7 +493,7 @@ static int run(
491
493
return commandLine .execute (args );
492
494
}
493
495
494
- static void versionInformation (PrintStream out ) {
496
+ static void versionInformation (PrintWriter out ) {
495
497
String lineSeparator = System .getProperty ("line.separator" );
496
498
String version =
497
499
format (
@@ -661,13 +663,16 @@ public Integer call() throws Exception {
661
663
}
662
664
}
663
665
666
+ EventLoopGroup eventLoopGroup = new NioEventLoopGroup ();
667
+
664
668
EnvironmentBuilder environmentBuilder =
665
669
Environment .builder ()
666
670
.id ("stream-perf-test" )
667
671
.uris (this .uris )
668
672
.addressResolver (addrResolver )
669
673
.scheduledExecutorService (envExecutor )
670
674
.metricsCollector (metricsCollector )
675
+ .eventLoopGroup (eventLoopGroup )
671
676
.byteBufAllocator (byteBufAllocator )
672
677
.codec (codec )
673
678
.maxProducersByConnection (this .producersByConnection )
@@ -701,10 +706,20 @@ public Integer call() throws Exception {
701
706
}
702
707
703
708
Environment environment = environmentBuilder .channelCustomizer (channelCustomizer ).build ();
704
- shutdownService .wrap (closeStep ("Closing environment(s)" , () -> environment .close ()));
709
+ if (!isRunTimeLimited ()) {
710
+ shutdownService .wrap (
711
+ closeStep (
712
+ "Closing Netty event loop group" ,
713
+ () -> {
714
+ if (!eventLoopGroup .isShuttingDown () || !eventLoopGroup .isShutdown ()) {
715
+ eventLoopGroup .shutdownGracefully (0 , 0 , TimeUnit .SECONDS );
716
+ }
717
+ }));
718
+ shutdownService .wrap (closeStep ("Closing environment" , () -> environment .close ()));
719
+ }
705
720
706
721
MonitoringContext monitoringContext =
707
- new MonitoringContext (this .monitoringPort , meterRegistry , environment );
722
+ new MonitoringContext (this .monitoringPort , meterRegistry , environment , this . out );
708
723
this .monitorings .forEach (m -> m .configure (monitoringContext ));
709
724
monitoringContext .start ();
710
725
@@ -958,14 +973,16 @@ public Integer call() throws Exception {
958
973
})
959
974
.collect (Collectors .toList ()));
960
975
961
- shutdownService .wrap (
962
- closeStep (
963
- "Closing consumers" ,
964
- () -> {
965
- for (Consumer consumer : consumers ) {
966
- consumer .close ();
967
- }
968
- }));
976
+ if (!isRunTimeLimited ()) {
977
+ shutdownService .wrap (
978
+ closeStep (
979
+ "Closing consumers" ,
980
+ () -> {
981
+ for (Consumer consumer : consumers ) {
982
+ consumer .close ();
983
+ }
984
+ }));
985
+ }
969
986
970
987
ExecutorService executorService ;
971
988
if (this .producers > 0 ) {
@@ -980,23 +997,25 @@ public Integer call() throws Exception {
980
997
executorService = null ;
981
998
}
982
999
983
- shutdownService .wrap (
984
- closeStep (
985
- "Closing producers" ,
986
- () -> {
987
- for (Producer p : producers ) {
988
- p .close ();
989
- }
990
- }));
1000
+ if (!isRunTimeLimited ()) {
1001
+ shutdownService .wrap (
1002
+ closeStep (
1003
+ "Closing producers" ,
1004
+ () -> {
1005
+ for (Producer p : producers ) {
1006
+ p .close ();
1007
+ }
1008
+ }));
991
1009
992
- shutdownService .wrap (
993
- closeStep (
994
- "Closing producers executor service" ,
995
- () -> {
996
- if (executorService != null ) {
997
- executorService .shutdownNow ();
998
- }
999
- }));
1010
+ shutdownService .wrap (
1011
+ closeStep (
1012
+ "Closing producers executor service" ,
1013
+ () -> {
1014
+ if (executorService != null ) {
1015
+ executorService .shutdownNow ();
1016
+ }
1017
+ }));
1018
+ }
1000
1019
1001
1020
String metricsHeader = "Arguments: " + String .join (" " , arguments );
1002
1021
@@ -1008,7 +1027,7 @@ public Integer call() throws Exception {
1008
1027
Thread shutdownHook = new Thread (() -> latch .countDown ());
1009
1028
Runtime .getRuntime ().addShutdownHook (shutdownHook );
1010
1029
try {
1011
- if (this . time > 0 ) {
1030
+ if (isRunTimeLimited () ) {
1012
1031
latch .await (this .time , TimeUnit .SECONDS );
1013
1032
} else {
1014
1033
latch .await ();
@@ -1091,7 +1110,7 @@ private void maybeDisplayEnvironmentVariablesHelp() {
1091
1110
1092
1111
private void maybeDisplayVersion () {
1093
1112
if (this .version ) {
1094
- versionInformation (System .out );
1113
+ versionInformation (this .out );
1095
1114
System .exit (0 );
1096
1115
}
1097
1116
}
@@ -1112,6 +1131,10 @@ public String toString() {
1112
1131
};
1113
1132
}
1114
1133
1134
+ private boolean isRunTimeLimited () {
1135
+ return this .time > 0 ;
1136
+ }
1137
+
1115
1138
public void monitorings (List <Monitoring > monitorings ) {
1116
1139
this .monitorings = monitorings ;
1117
1140
}
0 commit comments