Skip to content

Commit c0f8106

Browse files
author
Simon MacMullen
committed
Read performance test specifications from a JSON file. We lose the ability to run multiple brokers, but that wasn't very well done anyway. We can come back to it.
1 parent 42a7281 commit c0f8106

File tree

5 files changed

+155
-183
lines changed

5 files changed

+155
-183
lines changed

test/src/com/rabbitmq/examples/perf/MulticastValue.java

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,6 @@
1616

1717
package com.rabbitmq.examples.perf;
1818

19-
import java.beans.IntrospectionException;
20-
import java.beans.Introspector;
21-
import java.beans.PropertyDescriptor;
22-
import java.lang.reflect.InvocationTargetException;
23-
2419
class MulticastValue implements VariableValue {
2520
private final String name;
2621
private final Object value;
@@ -32,32 +27,13 @@ class MulticastValue implements VariableValue {
3227

3328
@Override
3429
public void setup(MulticastParams params) {
35-
setValue(params, name, value);
30+
PerfUtil.setValue(params, name, value);
3631
}
3732

3833
@Override
3934
public void teardown(MulticastParams params) {
4035
}
4136

42-
private static void setValue(Object obj, String name, Object value) {
43-
try {
44-
PropertyDescriptor[] props = Introspector.getBeanInfo(obj.getClass()).getPropertyDescriptors();
45-
for (PropertyDescriptor prop : props) {
46-
if (prop.getName().equals(name)) {
47-
prop.getWriteMethod().invoke(obj, value);
48-
return;
49-
}
50-
}
51-
throw new RuntimeException("Could not find property " + name + " in " + obj.getClass());
52-
} catch (IntrospectionException e) {
53-
throw new RuntimeException(e);
54-
} catch (InvocationTargetException e) {
55-
throw new RuntimeException(e);
56-
} catch (IllegalAccessException e) {
57-
throw new RuntimeException(e);
58-
}
59-
}
60-
6137
@Override
6238
public String getName() {
6339
return name;
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.rabbitmq.examples.perf;
2+
3+
import java.beans.IntrospectionException;
4+
import java.beans.Introspector;
5+
import java.beans.PropertyDescriptor;
6+
import java.lang.reflect.InvocationTargetException;
7+
8+
public class PerfUtil {
9+
public static void setValue(Object obj, Object name, Object value) {
10+
try {
11+
PropertyDescriptor[] props = Introspector.getBeanInfo(obj.getClass()).getPropertyDescriptors();
12+
for (PropertyDescriptor prop : props) {
13+
if (prop.getName().equals(name)) {
14+
prop.getWriteMethod().invoke(obj, value);
15+
return;
16+
}
17+
}
18+
throw new RuntimeException("Could not find property " + name + " in " + obj.getClass());
19+
} catch (IntrospectionException e) {
20+
throw new RuntimeException(e);
21+
} catch (InvocationTargetException e) {
22+
throw new RuntimeException(e);
23+
} catch (IllegalAccessException e) {
24+
throw new RuntimeException(e);
25+
}
26+
}
27+
}

test/src/com/rabbitmq/examples/perf/PerformanceMain.java

Lines changed: 32 additions & 158 deletions
Original file line numberDiff line numberDiff line change
@@ -17,46 +17,62 @@
1717
package com.rabbitmq.examples.perf;
1818

1919
import com.rabbitmq.client.ConnectionFactory;
20+
import com.rabbitmq.tools.json.JSONReader;
2021
import com.rabbitmq.tools.json.JSONWriter;
2122

23+
import java.io.FileInputStream;
2224
import java.io.FileWriter;
2325
import java.io.IOException;
26+
import java.io.InputStreamReader;
2427
import java.io.PrintWriter;
25-
import java.util.Arrays;
28+
import java.io.Reader;
2629
import java.util.HashMap;
2730
import java.util.List;
2831
import java.util.Map;
2932

3033
public 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
}
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
package com.rabbitmq.examples.perf;
2+
3+
import com.rabbitmq.client.ConnectionFactory;
4+
5+
import java.util.List;
6+
import java.util.Map;
7+
8+
public class ScenarioFactory {
9+
public static Scenario fromJSON(Map json, ConnectionFactory factory) {
10+
String type = read("type", json, String.class);
11+
String name = read("name", json, String.class);
12+
Integer interval = read("interval", json, Integer.class, 1000);
13+
List paramsJSON = read("params", json, List.class);
14+
15+
MulticastParams[] params = new MulticastParams[paramsJSON.size()];
16+
for (int i = 0; i < paramsJSON.size(); i++) {
17+
params[i] = paramsFromJSON((Map) paramsJSON.get(i));
18+
}
19+
20+
if (type.equals("simple")) {
21+
return new SimpleScenario(name, factory, interval, params);
22+
}
23+
else if (type.equals("rate-vs-latency")) {
24+
return new RateVsLatencyScenario(name, factory, params[0]); // TODO
25+
}
26+
else if (type.equals("varying")) {
27+
List variablesJSON = read("variables", json, List.class);
28+
Variable[] variables = new Variable[variablesJSON.size()];
29+
for (int i = 0; i < variablesJSON.size(); i++) {
30+
variables[i] = variableFromJSON((Map) variablesJSON.get(i));
31+
}
32+
33+
return new VaryingScenario(name, factory, params, variables);
34+
}
35+
36+
throw new RuntimeException("Type " + type + " was not simple or varying.");
37+
}
38+
39+
private static<T> T read(String key, Map map, Class<T> clazz) {
40+
if (map.containsKey(key)) {
41+
return read0(key, map, clazz);
42+
}
43+
else {
44+
throw new RuntimeException("Key " + key + " not found.");
45+
}
46+
}
47+
48+
private static<T> T read(String key, Map map, Class<T> clazz, T def) {
49+
if (map.containsKey(key)) {
50+
return read0(key, map, clazz);
51+
}
52+
else {
53+
return def;
54+
}
55+
}
56+
57+
private static <T> T read0(String key, Map map, Class<T> clazz) {
58+
Object o = map.get(key);
59+
if (clazz.isAssignableFrom(o.getClass())) {
60+
return (T) o;
61+
}
62+
else {
63+
throw new RuntimeException("Object under key " + key + " was a " + o.getClass() + ", not a " + clazz + ".");
64+
}
65+
}
66+
67+
private static MulticastParams paramsFromJSON(Map json) {
68+
MulticastParams params = new MulticastParams();
69+
for (Object key : json.keySet()) {
70+
PerfUtil.setValue(params, hyphensToCamel((String)key), json.get(key));
71+
}
72+
return params;
73+
}
74+
75+
private static Variable variableFromJSON(Map json) {
76+
String type = read("type", json, String.class, "multicast");
77+
String name = read("name", json, String.class);
78+
Object[] values = read("values", json, List.class).toArray();
79+
80+
if (type.equals("multicast")) {
81+
return new MulticastVariable(hyphensToCamel(name), values);
82+
}
83+
84+
throw new RuntimeException("Type " + type + " was not multicast");
85+
}
86+
87+
private static String hyphensToCamel(String name) {
88+
String out = "";
89+
for (String part : name.split("-")) {
90+
out += part.substring(0, 1).toUpperCase() + part.substring(1);
91+
}
92+
return out.substring(0, 1).toLowerCase() + out.substring(1);
93+
}
94+
}

0 commit comments

Comments
 (0)