48
48
import java .util .Collections ;
49
49
import java .util .Comparator ;
50
50
import java .util .HashMap ;
51
+ import java .util .Iterator ;
51
52
import java .util .List ;
52
53
import java .util .Locale ;
53
54
import java .util .Map ;
@@ -308,6 +309,116 @@ static int downSamplingDivisor(int rate) {
308
309
return divisor ;
309
310
}
310
311
312
+ static void declareSuperStreamExchangeAndBindings (
313
+ Channel channel , String superStream , List <String > streams ) throws Exception {
314
+ channel .exchangeDeclare (
315
+ superStream ,
316
+ BuiltinExchangeType .DIRECT ,
317
+ true ,
318
+ false ,
319
+ Collections .singletonMap ("x-super-stream" , true ));
320
+
321
+ for (int i = 0 ; i < streams .size (); i ++) {
322
+ channel .queueBind (
323
+ streams .get (i ),
324
+ superStream ,
325
+ String .valueOf (i ),
326
+ Collections .singletonMap ("x-stream-partition-order" , i ));
327
+ }
328
+ }
329
+
330
+ static void deleteSuperStreamExchange (Channel channel , String superStream ) throws Exception {
331
+ channel .exchangeDelete (superStream );
332
+ }
333
+
334
+ static List <String > superStreamPartitions (String superStream , int partitionCount ) {
335
+ int digits = String .valueOf (partitionCount - 1 ).length ();
336
+ String format = superStream + "-%0" + digits + "d" ;
337
+ return IntStream .range (0 , partitionCount )
338
+ .mapToObj (i -> String .format (format , i ))
339
+ .collect (Collectors .toList ());
340
+ }
341
+
342
+ static Connection amqpConnection (
343
+ String amqpUri , List <String > streamUris , boolean isTls , List <SNIServerName > sniServerNames )
344
+ throws Exception {
345
+ ConnectionFactory connectionFactory = new ConnectionFactory ();
346
+ if (amqpUri == null || amqpUri .trim ().isEmpty ()) {
347
+ String streamUriString = streamUris .get (0 );
348
+ if (isTls ) {
349
+ streamUriString = streamUriString .replaceFirst ("rabbitmq-stream\\ +tls" , "amqps" );
350
+ } else {
351
+ streamUriString = streamUriString .replaceFirst ("rabbitmq-stream" , "amqp" );
352
+ }
353
+ URI streamUri = new URI (streamUriString );
354
+ int streamPort = streamUri .getPort ();
355
+ if (streamPort != -1 ) {
356
+ int defaultAmqpPort =
357
+ isTls
358
+ ? ConnectionFactory .DEFAULT_AMQP_OVER_SSL_PORT
359
+ : ConnectionFactory .DEFAULT_AMQP_PORT ;
360
+ streamUriString = streamUriString .replaceFirst (":" + streamPort , ":" + defaultAmqpPort );
361
+ }
362
+ connectionFactory .setUri (streamUriString );
363
+ } else {
364
+ connectionFactory .setUri (amqpUri );
365
+ }
366
+ if (isTls ) {
367
+ SSLContext sslContext = SSLContext .getInstance ("TLS" );
368
+ sslContext .init (
369
+ new KeyManager [] {},
370
+ new TrustManager [] {TRUST_EVERYTHING_TRUST_MANAGER },
371
+ new SecureRandom ());
372
+ connectionFactory .useSslProtocol (sslContext );
373
+ if (!sniServerNames .isEmpty ()) {
374
+ SocketConfigurator socketConfigurator =
375
+ socket -> {
376
+ if (socket instanceof SSLSocket ) {
377
+ SSLSocket sslSocket = (SSLSocket ) socket ;
378
+ SSLParameters sslParameters =
379
+ sslSocket .getSSLParameters () == null
380
+ ? new SSLParameters ()
381
+ : sslSocket .getSSLParameters ();
382
+ sslParameters .setServerNames (sniServerNames );
383
+ sslSocket .setSSLParameters (sslParameters );
384
+ } else {
385
+ LOGGER .warn ("SNI parameter set on a non-TLS connection" );
386
+ }
387
+ };
388
+ connectionFactory .setSocketConfigurator (
389
+ SocketConfigurators .defaultConfigurator ().andThen (socketConfigurator ));
390
+ }
391
+ }
392
+ return connectionFactory .newConnection ("stream-perf-test-amqp-connection" );
393
+ }
394
+
395
+ static String commandLineMetrics (String [] args ) {
396
+ Map <String , Boolean > filteredOptions = new HashMap <>();
397
+ filteredOptions .put ("--uris" , true );
398
+ filteredOptions .put ("-u" , true );
399
+ filteredOptions .put ("--prometheus" , false );
400
+ filteredOptions .put ("--amqp-uri" , true );
401
+ filteredOptions .put ("--au" , true );
402
+ filteredOptions .put ("--metrics-command-line-arguments" , false );
403
+ filteredOptions .put ("-mcla" , false );
404
+ filteredOptions .put ("--metrics-tags" , true );
405
+ filteredOptions .put ("-mt" , true );
406
+
407
+ Collection <String > filtered = new ArrayList <>();
408
+ Iterator <String > iterator = Arrays .stream (args ).iterator ();
409
+ while (iterator .hasNext ()) {
410
+ String option = iterator .next ();
411
+ if (filteredOptions .containsKey (option )) {
412
+ if (filteredOptions .get (option )) {
413
+ iterator .next ();
414
+ }
415
+ } else {
416
+ filtered .add (option );
417
+ }
418
+ }
419
+ return String .join (" " , filtered );
420
+ }
421
+
311
422
static class ByteCapacityTypeConverter implements CommandLine .ITypeConverter <ByteCapacity > {
312
423
313
424
@ Override
@@ -324,7 +435,7 @@ public ByteCapacity convert(String value) {
324
435
static class MetricsTagsTypeConverter implements CommandLine .ITypeConverter <Collection <Tag >> {
325
436
326
437
@ Override
327
- public Collection <Tag > convert (String value ) throws Exception {
438
+ public Collection <Tag > convert (String value ) {
328
439
if (value == null || value .trim ().isEmpty ()) {
329
440
return Collections .emptyList ();
330
441
} else {
@@ -359,7 +470,7 @@ public BiFunction<String, Integer, String> convert(String input) {
359
470
static class SniServerNamesConverter implements ITypeConverter <List <SNIServerName >> {
360
471
361
472
@ Override
362
- public List <SNIServerName > convert (String value ) throws Exception {
473
+ public List <SNIServerName > convert (String value ) {
363
474
if (value == null || value .trim ().isEmpty ()) {
364
475
return Collections .emptyList ();
365
476
} else {
@@ -626,6 +737,7 @@ public Thread newThread(Runnable r) {
626
737
}
627
738
628
739
private static class TrustEverythingTrustManager implements X509TrustManager {
740
+
629
741
@ Override
630
742
public void checkClientTrusted (X509Certificate [] chain , String authType ) {}
631
743
@@ -652,89 +764,6 @@ public String apply(String stream, Integer index) {
652
764
}
653
765
}
654
766
655
- static void declareSuperStreamExchangeAndBindings (
656
- Channel channel , String superStream , List <String > streams ) throws Exception {
657
- channel .exchangeDeclare (
658
- superStream ,
659
- BuiltinExchangeType .DIRECT ,
660
- true ,
661
- false ,
662
- Collections .singletonMap ("x-super-stream" , true ));
663
-
664
- for (int i = 0 ; i < streams .size (); i ++) {
665
- channel .queueBind (
666
- streams .get (i ),
667
- superStream ,
668
- String .valueOf (i ),
669
- Collections .singletonMap ("x-stream-partition-order" , i ));
670
- }
671
- }
672
-
673
- static void deleteSuperStreamExchange (Channel channel , String superStream ) throws Exception {
674
- channel .exchangeDelete (superStream );
675
- }
676
-
677
- static List <String > superStreamPartitions (String superStream , int partitionCount ) {
678
- int digits = String .valueOf (partitionCount - 1 ).length ();
679
- String format = superStream + "-%0" + digits + "d" ;
680
- return IntStream .range (0 , partitionCount )
681
- .mapToObj (i -> String .format (format , i ))
682
- .collect (Collectors .toList ());
683
- }
684
-
685
- static Connection amqpConnection (
686
- String amqpUri , List <String > streamUris , boolean isTls , List <SNIServerName > sniServerNames )
687
- throws Exception {
688
- ConnectionFactory connectionFactory = new ConnectionFactory ();
689
- if (amqpUri == null || amqpUri .trim ().isEmpty ()) {
690
- String streamUriString = streamUris .get (0 );
691
- if (isTls ) {
692
- streamUriString = streamUriString .replaceFirst ("rabbitmq-stream\\ +tls" , "amqps" );
693
- } else {
694
- streamUriString = streamUriString .replaceFirst ("rabbitmq-stream" , "amqp" );
695
- }
696
- URI streamUri = new URI (streamUriString );
697
- int streamPort = streamUri .getPort ();
698
- if (streamPort != -1 ) {
699
- int defaultAmqpPort =
700
- isTls
701
- ? ConnectionFactory .DEFAULT_AMQP_OVER_SSL_PORT
702
- : ConnectionFactory .DEFAULT_AMQP_PORT ;
703
- streamUriString = streamUriString .replaceFirst (":" + streamPort , ":" + defaultAmqpPort );
704
- }
705
- connectionFactory .setUri (streamUriString );
706
- } else {
707
- connectionFactory .setUri (amqpUri );
708
- }
709
- if (isTls ) {
710
- SSLContext sslContext = SSLContext .getInstance ("TLS" );
711
- sslContext .init (
712
- new KeyManager [] {},
713
- new TrustManager [] {TRUST_EVERYTHING_TRUST_MANAGER },
714
- new SecureRandom ());
715
- connectionFactory .useSslProtocol (sslContext );
716
- if (!sniServerNames .isEmpty ()) {
717
- SocketConfigurator socketConfigurator =
718
- socket -> {
719
- if (socket instanceof SSLSocket ) {
720
- SSLSocket sslSocket = (SSLSocket ) socket ;
721
- SSLParameters sslParameters =
722
- sslSocket .getSSLParameters () == null
723
- ? new SSLParameters ()
724
- : sslSocket .getSSLParameters ();
725
- sslParameters .setServerNames (sniServerNames );
726
- sslSocket .setSSLParameters (sslParameters );
727
- } else {
728
- LOGGER .warn ("SNI parameter set on a non-TLS connection" );
729
- }
730
- };
731
- connectionFactory .setSocketConfigurator (
732
- SocketConfigurators .defaultConfigurator ().andThen (socketConfigurator ));
733
- }
734
- }
735
- return connectionFactory .newConnection ("stream-perf-test-amqp-connection" );
736
- }
737
-
738
767
static class PerformanceMicrometerMetricsCollector extends MicrometerMetricsCollector {
739
768
740
769
public PerformanceMicrometerMetricsCollector (MeterRegistry registry , String prefix ) {
0 commit comments