3434import com .rabbitmq .client .*;
3535import java .util .*;
3636
37- public class ChannelCreationPerformance {
37+ class ChannelCreationPerformance {
3838 static Connection connect () throws Exception {
3939 return new ConnectionFactory (new ConnectionParameters (){{
4040 setRequestedChannelMax (CHANNEL_MAX );
@@ -45,53 +45,67 @@ static Connection connect() throws Exception{
4545 static int STEP = 1000 ;
4646 static int START = STEP ;
4747
48- public static void main (String [] args ) throws Exception {
49- System .out .println ("Sequential creation, no close:" );
50- for (int i = START ; i <= CHANNEL_MAX ; i += STEP ){
51- Connection c = connect ();
52- long start = System .currentTimeMillis ();
53- for (int j = 1 ; j <= i ; j ++){
54- c .createChannel ();
55- }
56- System .out .println (i + "\t " + (System .currentTimeMillis () - start ));
57- c .close ();
58- }
48+ abstract static class PerformanceTest {
49+ String name ;
50+ Connection c ;
51+ int i ;
5952
60- System .out .println ("Sequential creation followed by close:" );
61- for (int i = START ; i <= CHANNEL_MAX ; i += STEP ){
62- Connection c = connect ();
63- long start = System .currentTimeMillis ();
64- for (int j = 1 ; j <= i ; j ++){
65- c .createChannel ().close ();
66- }
67- System .out .println (i + "\t " + (System .currentTimeMillis () - start ));
68- c .close ();
53+ PerformanceTest (String name ){
54+ this .name = name ;
6955 }
56+
57+ void run () throws Exception {
58+ System .out .println (name );
59+ for (i = START ; i <= CHANNEL_MAX ; i += STEP ){
60+ c = connect ();
61+ long start = System .currentTimeMillis ();
62+ body ();
63+ long time = System .currentTimeMillis () - start ;
64+ System .out .println (i + "\t " + time + " (" + (1000 * i / ((double )time )) + " channels/s)" );
65+ c .close ();
66+ }
67+ }
7068
71- System .out .println ("Sequential creation then bulk close:" );
72- for (int i = START ; i <= CHANNEL_MAX ; i += STEP ){
73- Connection c = connect ();
74- long start = System .currentTimeMillis ();
75- ArrayList <Channel > channels = new ArrayList <Channel >();
76- for (int j = 1 ; j <= i ; j ++){
77- channels .add (c .createChannel ());
78- }
79- for (Channel chan : channels ) chan .close ();
80- System .out .println (i + "\t " + (System .currentTimeMillis () - start ));
81- c .close ();
82- }
83- System .out .println ("Sequential creation then out of order bulk close:" );
84- for (int i = START ; i <= CHANNEL_MAX ; i += STEP ){
85- Connection c = connect ();
86- long start = System .currentTimeMillis ();
87- ArrayList <Channel > channels = new ArrayList <Channel >();
88- for (int j = 1 ; j <= i ; j ++){
89- channels .add (c .createChannel ());
90- }
91- Collections .shuffle (channels );
92- for (Channel chan : channels ) chan .close ();
93- System .out .println (i + "\t " + (System .currentTimeMillis () - start ));
94- c .close ();
95- }
69+ abstract void body () throws Exception ;
70+
71+ }
72+
73+ public static void main (String [] args ) throws Exception {
74+ new PerformanceTest ("Sequential creation, no close:" ){
75+ void body () throws Exception {
76+ for (int j = 1 ; j <= i ; j ++){
77+ c .createChannel ();
78+ }
79+ }
80+ }.run ();
81+
82+ new PerformanceTest ("Sequential creation followed by close:" ){
83+ void body () throws Exception {
84+ for (int j = 1 ; j <= i ; j ++){
85+ c .createChannel ().close ();
86+ }
87+ }
88+ }.run ();
89+
90+ new PerformanceTest ("Sequential creation then bulk close:" ){
91+ void body () throws Exception {
92+ ArrayList <Channel > channels = new ArrayList <Channel >();
93+ for (int j = 1 ; j <= i ; j ++){
94+ channels .add (c .createChannel ());
95+ }
96+ for (Channel chan : channels ) chan .close ();
97+ }
98+ }.run ();
99+
100+ new PerformanceTest ("Sequential creation then out of order bulk close:" ){
101+ void body () throws Exception {
102+ ArrayList <Channel > channels = new ArrayList <Channel >();
103+ for (int j = 1 ; j <= i ; j ++){
104+ channels .add (c .createChannel ());
105+ }
106+ Collections .shuffle (channels );
107+ for (Channel chan : channels ) chan .close ();
108+ }
109+ }.run ();
96110 }
97111}
0 commit comments