1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package jgroup.test.performance;
20
21 import java.rmi.RemoteException;
22 import java.util.Timer;
23 import java.util.TimerTask;
24
25 import jgroup.core.GroupUnreachableException;
26 import jgroup.core.registry.DependableRegistry;
27 import jgroup.core.registry.RegistryFactory;
28 import jgroup.util.Network;
29 import jgroup.util.log.Eventlogger;
30
31
32
33
34
35
36
37
38 public class PerformanceClient
39 {
40
41
42
43
44
45
46 private static final String DELAY_MEASUREMENT = "delay";
47 private static final String THROUGHPUT_MEASUREMENT = "throughput";
48
49
50
51
52
53
54 public static final String[] properties = new String[] {
55 "calibration.period",
56 "request.load.duration",
57 "measurement.type",
58 "requests.per.second",
59 "invocation.type",
60 "buffer.size",
61 };
62
63
64
65
66
67 private static final String[] supportedServers = new String[] {
68 "Jgroup/PerformanceServer",
69 "ARM/ReplicatedServer",
70 "Jgroup/SpeedTest",
71 "UpgradeServer"
72 };
73
74
75 private static final int TIME_UNIT = 1000;
76
77
78
79
80
81
82
83 private static SpeedTest test = null;
84
85
86 private static int requestPeriod;
87
88
89 private static String invocationType;
90
91
92 private static byte[] buffer;
93
94
95 private static volatile boolean trafficGenerationCompleted = false;
96
97
98
99
100
101 private static volatile boolean loggingCompleted = false;
102
103
104
105
106
107
108
109
110 private static volatile boolean logDelays = false;
111
112
113
114
115
116
117 public static void main(String[] argv)
118 throws Exception
119 {
120 String localHost = Network.getLocalMachineName();
121 System.out.println("Running PerformanceClient: " + localHost);
122
123
124
125
126 for (int i = 0; i < properties.length; i++) {
127 String prop = System.getProperty(properties[i]);
128 if (prop != null && prop.startsWith("${")) {
129 System.clearProperty(properties[i]);
130 }
131 }
132 DependableRegistry reg = RegistryFactory.getRegistry();
133 String[] servers = reg.list();
134 for (int i = 0; test == null && i < servers.length; i++) {
135 for (int j = 0; test == null && j < supportedServers.length; j++) {
136 if (servers[i].equals(supportedServers[j])) {
137 test = (SpeedTest) reg.lookup(servers[i]);
138 System.out.println("Found supported server: " + servers[i]);
139 }
140 }
141 }
142 if (test == null) {
143 System.out.println("No supported servers found");
144 System.exit(0);
145 }
146
147
148
149
150 int calibrationPeriod = Integer.getInteger("calibration.period", 20).intValue();
151 int requestDuration = Integer.getInteger("request.load.duration", 60).intValue();
152 String measurementType = System.getProperty("measurement.type", DELAY_MEASUREMENT);
153 if (measurementType.equalsIgnoreCase(DELAY_MEASUREMENT)) {
154 int requestRate = Integer.getInteger("requests.per.second", 2).intValue();
155 requestPeriod = TIME_UNIT / requestRate;
156 } else if (measurementType.equalsIgnoreCase(THROUGHPUT_MEASUREMENT)) {
157 requestPeriod = -1;
158 }
159 invocationType = System.getProperty("invocation.type", "anycast");
160 String invType = invocationType;
161 if (invocationType.endsWith("-buffer")) {
162 int bufSize = Integer.getInteger("buffer.size", 1000).intValue();
163 buffer = new byte[bufSize];
164 invType = invocationType + "(" + bufSize + ")";
165 }
166
167
168
169
170
171
172 System.out.println("Calibration: " + localHost);
173 generateTraffic(calibrationPeriod);
174
175 logDelays = true;
176 System.out.println("Begin logging: " + localHost);
177 Eventlogger.logEvent("BeginTrafficGeneration: " + invType);
178 generateTraffic(requestDuration);
179 Eventlogger.logEvent("EndTrafficGeneration: " + invType);
180 System.out.println("End logging: " + localHost);
181 logDelays = false;
182 Eventlogger.close();
183
184
185
186
187
188 generateTraffic(5);
189 System.out.println("Exiting: " + localHost);
190 }
191
192
193
194
195
196
197
198 public static void generateTraffic(final int duration)
199 {
200 final Timer timer = new Timer("GenerateTraffic", true);
201 if (requestPeriod > 0) {
202 TimerTask timerTask = getDelayTimerTask();
203 timer.scheduleAtFixedRate(timerTask, 0, requestPeriod);
204 } else {
205 TimerTask timerTask = getThroughputTimerTask();
206 timer.schedule(timerTask, 0);
207 }
208 new Thread("StopTrafficGeneration") {
209 public void run() {
210 try { sleep(duration*TIME_UNIT);
211 } catch (InterruptedException e) { }
212
213 trafficGenerationCompleted = true;
214 }
215 }.start();
216
217
218
219 while (!trafficGenerationCompleted || !loggingCompleted) {
220 Thread.yield();
221 }
222
223 timer.cancel();
224
225 trafficGenerationCompleted = false;
226 loggingCompleted = false;
227 }
228
229
230
231
232
233
234
235
236 public static TimerTask getThroughputTimerTask()
237 {
238 return new TimerTask() {
239 public void run() {
240 long invocationCounter = 0;
241 long start = System.currentTimeMillis();
242 try {
243 if (invocationType.equalsIgnoreCase("anycast")) {
244 while (!trafficGenerationCompleted) {
245 test.test();
246 invocationCounter++;
247 }
248 } else if (invocationType.equalsIgnoreCase("anycast-buffer")) {
249 while (!trafficGenerationCompleted) {
250 test.test(buffer);
251 invocationCounter++;
252 }
253 } else if (invocationType.equalsIgnoreCase("multicast")) {
254 while (!trafficGenerationCompleted) {
255 test.mtest();
256 invocationCounter++;
257 }
258 } else if (invocationType.equalsIgnoreCase("multicast-buffer")) {
259 while (!trafficGenerationCompleted) {
260 test.mtest(buffer);
261 invocationCounter++;
262 }
263 } else {
264 throw new IllegalStateException("Unknown invocation type: " + invocationType);
265 }
266 } catch (RemoteException e) {
267 trafficGenerationCompleted = true;
268 e.printStackTrace();
269 Eventlogger.logEvent(e.getMessage());
270 } catch (GroupUnreachableException e) {
271
272
273
274
275 trafficGenerationCompleted = true;
276 Eventlogger.logEvent("GroupUnreachable");
277 }
278 double invocDuration = (System.currentTimeMillis() - start) / TIME_UNIT;
279 double throughput = invocationCounter / invocDuration;
280
281 if (logDelays) {
282 Eventlogger.logEventNoSysTime(Double.toString(throughput));
283 }
284 loggingCompleted = true;
285 }
286 };
287 }
288
289
290
291
292
293
294
295 public static TimerTask getDelayTimerTask()
296 {
297 return new TimerTask() {
298 public void run() {
299 try {
300 long invocationDelay = 0;
301 if (invocationType.equalsIgnoreCase("anycast")) {
302 long start = System.nanoTime();
303 test.test();
304 invocationDelay = System.nanoTime() - start;
305 } else if (invocationType.equalsIgnoreCase("anycast-buffer")) {
306 long start = System.nanoTime();
307 test.test(buffer);
308 invocationDelay = System.nanoTime() - start;
309 } else if (invocationType.equalsIgnoreCase("multicast")) {
310 long start = System.nanoTime();
311 test.mtest();
312 invocationDelay = System.nanoTime() - start;
313 } else if (invocationType.equalsIgnoreCase("multicast-buffer")) {
314 long start = System.nanoTime();
315 test.mtest(buffer);
316 invocationDelay = System.nanoTime() - start;
317 } else {
318 throw new IllegalStateException("Unknown invocation type: " + invocationType);
319 }
320
321 if (logDelays) {
322 Eventlogger.logEvent(Long.toString(invocationDelay));
323 }
324 } catch (RemoteException e) {
325 trafficGenerationCompleted = true;
326 e.printStackTrace();
327 Eventlogger.logEvent(e.getMessage());
328 } catch (GroupUnreachableException e) {
329
330
331
332
333 trafficGenerationCompleted = true;
334 Eventlogger.logEvent("GroupUnreachable");
335 } finally {
336 loggingCompleted = true;
337 }
338 }
339 };
340 }
341
342 }