KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > nl > justobjects > pushlet > test > StressTester


1 // Copyright (c) 2000 Just Objects B.V. <just@justobjects.nl>
2
// Distributable under LGPL license. See terms of license at gnu.org.
3

4 package nl.justobjects.pushlet.test;
5
6 import nl.justobjects.pushlet.client.PushletClient;
7 import nl.justobjects.pushlet.client.PushletClientListener;
8 import nl.justobjects.pushlet.core.Event;
9 import nl.justobjects.pushlet.core.Protocol;
10 import nl.justobjects.pushlet.util.PushletException;
11 import nl.justobjects.pushlet.util.Rand;
12
13 import java.util.HashMap JavaDoc;
14 import java.util.Map JavaDoc;
15
16 /**
17  * Tester to demonstrate Pushlet use in Java applications.
18  *
19  * This program does two things:
20  * (1) it subscribes to the subject "test/ping"
21  * (2) it publishes an Event with subject "/test/ping" every few seconds.
22  *
23  * @version $Id: StressTester.java,v 1.1 2005/02/28 17:16:58 justb Exp $
24  * @author Just van den Broecke - Just Objects &copy;
25  **/

26 public class StressTester implements Protocol {
27     static private String JavaDoc host = "localhost";
28     static private int port = 8080;
29     static private int TESTER_COUNT = 10;
30     private static final String JavaDoc SUBJECT = "/test/ping";
31     private static final long MIN_PUBLISH_INTERVAL_MILLIS = 200;
32     private static final long MAX_PUBLISH_INTERVAL_MILLIS = 1000;
33     private static final long MIN_SUBSCRIBER_INTERVAL_MILLIS = 500;
34     private static final long MAX_SUBSCRIBER_INTERVAL_MILLIS = 1000;
35
36     public StressTester() {
37     }
38
39     public void run() {
40         new EventPublisher().start();
41         new EventSubscriber().start();
42     }
43
44     /** Generic print. */
45     public void err(String JavaDoc s) {
46         System.out.println("[StressTester] ERROR" + s);
47     }
48
49     /** Generic print. */
50     public void p(String JavaDoc s) {
51         System.out.println("[StressTester] " + s);
52     }
53
54     private class EventSubscriber extends Thread JavaDoc implements PushletClientListener {
55         private PushletClient pushletClient;
56
57         public void run() {
58             while (true) {
59                 // Create and start a Pushlet client; we receive callbacks
60
// through onHeartbeat() and onData().
61
try {
62                     pushletClient = new PushletClient(host, port);
63                     // pushletClient.setDebug(true);
64
pushletClient.join();
65                     pushletClient.listen(this, Protocol.MODE_STREAM);
66                     //p("listening");
67
// Test subscribe/unsubscribe
68
String JavaDoc subscriptionId = pushletClient.subscribe(SUBJECT);
69                     pushletClient.unsubscribe(subscriptionId);
70
71                     // The real subscribe
72
subscriptionId = pushletClient.subscribe(SUBJECT);
73                     //p("sleeping");
74
sleepRandom();
75                     //p("leaving");
76
pushletClient.unsubscribe(subscriptionId);
77                     pushletClient.leave();
78
79                 } catch (Throwable JavaDoc t) {
80                     err("Error in EventSubscriber t=" + t);
81                     return;
82                 }
83             }
84         }
85
86         /** Error occurred. */
87         public void onError(String JavaDoc message) {
88             // p(message);
89
}
90
91         /** Abort event from server. */
92         public void onAbort(Event theEvent) {
93             //p("onAbort received: " + theEvent);
94
}
95
96         /** Data event from server. */
97         public void onData(Event theEvent) {
98             // Calculate round trip delay
99
long then = Long.parseLong(theEvent.getField("time"));
100             long delay = System.currentTimeMillis() - then;
101             //p("onData: ping #" + theEvent.getField("seqNr") + " in " + delay + " ms");
102
}
103
104         /** Heartbeat event from server. */
105         public void onHeartbeat(Event theEvent) {
106             //p("onHeartbeat received: " + theEvent);
107
}
108
109         private void sleepRandom() throws InterruptedException JavaDoc {
110             Thread.sleep(Rand.randomLong(MIN_SUBSCRIBER_INTERVAL_MILLIS, MAX_SUBSCRIBER_INTERVAL_MILLIS));
111         }
112     }
113
114     private class EventPublisher extends Thread JavaDoc {
115         private PushletClient pushletClient;
116
117         public void run() {
118             // Create and start a Pushlet client; we receive callbacks
119
// through onHeartbeat() and onData().
120
try {
121                 pushletClient = new PushletClient(host, port);
122                 pushletClient.join();
123
124                 // p("pushletClient started");
125
} catch (PushletException pe) {
126                 err("Error in EventPublisher pe=" + pe);
127                 return;
128             }
129
130             // Publish an event to the server every N seconds.
131
Map JavaDoc eventData = new HashMap JavaDoc(2);
132             int seqNr = 1;
133             while (true) {
134                 try {
135                     // Create event data
136
eventData.put("seqNr", "" + seqNr++);
137                     eventData.put("time", "" + System.currentTimeMillis());
138
139                     // POST event to pushlet server
140
pushletClient.publish(SUBJECT, eventData);
141
142                     Thread.sleep(Rand.randomLong(MIN_PUBLISH_INTERVAL_MILLIS, MAX_PUBLISH_INTERVAL_MILLIS));
143                 } catch (Exception JavaDoc e) {
144                     p("EventPublisher exception: " + e);
145                     return;
146                 }
147             }
148         }
149
150     }
151
152     /** Main program. */
153     public static void main(String JavaDoc args[]) {
154         if (args.length > 0) {
155             TESTER_COUNT = Integer.parseInt(args[0]);
156         }
157         if (args.length == 3) {
158             host = args[1];
159             port = Integer.parseInt(args[2]);
160         }
161
162         for (int i = 0; i < TESTER_COUNT; i++) {
163             new StressTester().run();
164         }
165
166     }
167 }
168
169
170 /*
171  * $Log: StressTester.java,v $
172  * Revision 1.1 2005/02/28 17:16:58 justb
173  * simple stress tester
174  *
175  *
176  */

177
Popular Tags