1717package com .rabbitmq .examples .perf ;
1818
1919import com .rabbitmq .client .ConnectionFactory ;
20+ import com .rabbitmq .tools .json .JSONReader ;
2021import com .rabbitmq .tools .json .JSONWriter ;
2122
23+ import java .io .FileInputStream ;
2224import java .io .FileWriter ;
2325import java .io .IOException ;
26+ import java .io .InputStreamReader ;
2427import java .io .PrintWriter ;
25- import java .util . Arrays ;
28+ import java .io . Reader ;
2629import java .util .HashMap ;
2730import java .util .List ;
2831import java .util .Map ;
2932
3033public class PerformanceMain {
3134 private static final ConnectionFactory factory = new ConnectionFactory ();
3235
33- //private static final List<?> NO_FLAGS = Arrays.asList();
34- private static final List <?> PERSISTENT = Arrays .asList ("persistent" );
35- private static final List <?> MANDATORY = Arrays .asList ("mandatory" );
36- private static final List <?> IMMEDIATE = Arrays .asList ("immediate" );
37-
3836 private static Map <String , Object > results = new HashMap <String , Object >();
3937
4038 public static void main (String [] args ) throws Exception {
41- runStaticBrokerTests ();
42- runTests (new Scenario []{message_size_broker_config ()});
43- writeJSON ();
39+ String inJSON = args [0 ];
40+ String outJSON = args [1 ];
41+ List <Map > scenariosJSON = (List <Map >) new JSONReader ().read (readFile (inJSON ));
42+ Scenario [] scenarios = new Scenario [scenariosJSON .size ()];
43+ for (int i = 0 ; i < scenariosJSON .size (); i ++) {
44+ scenarios [i ] = ScenarioFactory .fromJSON (scenariosJSON .get (i ), factory );
45+ }
46+ runStaticBrokerTests (scenarios );
47+ writeJSON (outJSON );
48+ }
49+
50+ private static String readFile (String path ) throws IOException {
51+ final char [] buf = new char [4096 ];
52+ StringBuilder out = new StringBuilder ();
53+ Reader in = new InputStreamReader (new FileInputStream (path ), "UTF-8" );
54+ try {
55+ int chars ;
56+ while ((chars = in .read (buf , 0 , buf .length )) > 0 ) {
57+ out .append (buf , 0 , chars );
58+ }
59+ } finally {
60+ in .close ();
61+ }
62+ return out .toString ();
4463 }
4564
46- private static void writeJSON () throws IOException {
47- FileWriter outFile = new FileWriter ("results.js" );
65+ private static void writeJSON (String outJSON ) throws IOException {
66+ FileWriter outFile = new FileWriter (outJSON );
4867 PrintWriter out = new PrintWriter (outFile );
4968 out .println (new JSONWriter (true ).write (results ));
5069 outFile .close ();
5170 }
5271
53- private static void runStaticBrokerTests () throws Exception {
72+ private static void runStaticBrokerTests (Scenario [] scenarios ) throws Exception {
5473 Broker broker = Broker .HIPE_COARSE ;
5574 broker .start ();
56- runTests (new Scenario []{no_ack_long (), no_consume (), no_ack (), no_ack_mandatory (), no_ack_immediate (), ack (),
57- ack_confirm (), ack_confirm_persist (), ack_persist (), fill_drain_queue ("small" , 500000 ),
58- fill_drain_queue ("large" , 2000000 ), consumers (), headline_publish (), headline_consume (),
59- message_sizes_small (), message_sizes_large (), message_size_vs_producers (), rate_vs_latency ()});
75+ runTests (scenarios );
6076 broker .stop ();
6177 }
6278
@@ -68,146 +84,4 @@ private static void runTests(Scenario[] scenarios) throws Exception {
6884 results .put (scenario .getName (), scenario .getStats ().results ());
6985 }
7086 }
71-
72- private static Scenario no_ack_long () throws IOException , InterruptedException {
73- MulticastParams params = params ();
74- params .setTimeLimit (500 );
75- return new SimpleScenario ("no-ack-long" , factory , 10000 , params );
76- }
77-
78- private static Scenario headline_publish () throws IOException , InterruptedException {
79- MulticastParams params = params ();
80- params .setProducerCount (10 );
81- params .setConsumerCount (0 );
82- return new SimpleScenario ("headline-publish" , factory , params );
83- }
84-
85- private static Scenario headline_consume () throws IOException , InterruptedException {
86- MulticastParams params = params ();
87- params .setProducerCount (1 );
88- params .setConsumerCount (20 );
89- return new SimpleScenario ("headline-consume" , factory , params );
90- }
91-
92- private static Scenario no_consume () throws IOException , InterruptedException {
93- MulticastParams params = params ();
94- params .setConsumerCount (0 );
95- return new SimpleScenario ("no-consume" , factory , params );
96- }
97-
98- private static Scenario no_ack () throws IOException , InterruptedException {
99- MulticastParams params = params ();
100- return new SimpleScenario ("no-ack" , factory , params );
101- }
102-
103- private static Scenario no_ack_mandatory () throws IOException , InterruptedException {
104- MulticastParams params = params ();
105- params .setFlags (MANDATORY );
106- return new SimpleScenario ("no-ack-mandatory" , factory , params );
107- }
108-
109- private static Scenario no_ack_immediate () throws IOException , InterruptedException {
110- MulticastParams params = params ();
111- params .setFlags (IMMEDIATE );
112- return new SimpleScenario ("no-ack-immediate" , factory , params );
113- }
114-
115- private static Scenario ack () throws IOException , InterruptedException {
116- MulticastParams params = params ();
117- params .setAutoAck (false );
118- return new SimpleScenario ("ack" , factory , params );
119- }
120-
121- private static Scenario ack_confirm () throws IOException , InterruptedException {
122- MulticastParams params = params ();
123- params .setAutoAck (false );
124- params .setConfirm (10000 );
125- return new SimpleScenario ("ack-confirm" , factory , params );
126- }
127-
128- private static Scenario ack_confirm_persist () throws IOException , InterruptedException {
129- MulticastParams params = params ();
130- params .setAutoAck (false );
131- params .setConfirm (10000 );
132- params .setFlags (PERSISTENT );
133- return new SimpleScenario ("ack-confirm-persist" , factory , params );
134- }
135-
136- private static Scenario ack_persist () throws IOException , InterruptedException {
137- MulticastParams params = params ();
138- params .setAutoAck (false );
139- params .setFlags (PERSISTENT );
140- return new SimpleScenario ("ack-persist" , factory , params );
141- }
142-
143- private static Scenario fill_drain_queue (String name , int count ) throws IOException , InterruptedException {
144- MulticastParams fill = fill_drain_params ();
145- MulticastParams drain = fill_drain_params ();
146-
147- fill .setConsumerCount (0 );
148- fill .setProducerMsgCount (count );
149- drain .setProducerCount (0 );
150- drain .setConsumerMsgCount (count );
151-
152- return new SimpleScenario ("fill-drain-" + name + "-queue" , factory , fill , drain );
153- }
154-
155- private static MulticastParams fill_drain_params () {
156- MulticastParams params = new MulticastParams ();
157- params .setQueueName ("test" );
158- params .setExclusive (false );
159- params .setAutoDelete (true );
160- return params ;
161- }
162-
163- private static Scenario message_sizes_small () throws IOException , InterruptedException {
164- MulticastParams params = params ();
165- return new VaryingScenario ("message-sizes-small" , factory , params ,
166- var ("minMsgSize" , 0 , 100 , 200 , 300 , 400 , 500 , 600 , 700 , 800 , 900 , 1000 ,
167- 1200 , 1400 , 1600 , 1800 , 2000 , 3000 , 4000 , 5000 ));
168- }
169-
170- private static Scenario message_sizes_large () throws IOException , InterruptedException {
171- MulticastParams params = params ();
172- return new VaryingScenario ("message-sizes-large" , factory , params ,
173- var ("minMsgSize" , 5000 , 10000 , 20000 , 50000 , 100000 , 500000 , 1000000 ));
174- }
175-
176- private static Scenario consumers () throws IOException , InterruptedException {
177- MulticastParams params = params ();
178- params .setAutoAck (false );
179- return new VaryingScenario ("consumers" , factory , params ,
180- var ("consumerCount" , 1 , 2 , 5 , 10 , 50 , 100 , 500 ),
181- var ("prefetchCount" , 1 , 2 , 5 , 10 , 20 , 50 , 10000 ));
182- }
183-
184- private static Scenario message_size_vs_producers () throws IOException , InterruptedException {
185- MulticastParams params = params ();
186- params .setConsumerCount (0 );
187- return new VaryingScenario ("message-sizes-and-producers" , factory , params ,
188- var ("minMsgSize" , 0 , 1000 , 10000 , 100000 ),
189- var ("producerCount" , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 ));
190- }
191-
192- private static Scenario message_size_broker_config () throws IOException , InterruptedException {
193- MulticastParams params = params ();
194- return new VaryingScenario ("message-sizes-and-broker-config" , factory , params ,
195- var ("minMsgSize" , 0 , 1000 , 2000 , 5000 ),
196- new BrokerVariable (Broker .DEFAULT , Broker .HIPE , Broker .COARSE , Broker .HIPE_COARSE ));
197- }
198-
199- private static Scenario rate_vs_latency () throws IOException , InterruptedException {
200- MulticastParams params = params ();
201- return new RateVsLatencyScenario ("rate-vs-latency" , factory , params );
202- }
203-
204- private static MulticastParams params () {
205- MulticastParams p = new MulticastParams ();
206- p .setTimeLimit (30 );
207- return p ;
208- }
209-
210- private static Variable var (String name , Object ... values ) {
211- return new MulticastVariable (name , values );
212- }
21387}
0 commit comments