KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ubermq > jms > client > test > StatisticsTest


1 package com.ubermq.jms.client.test;
2
3 import java.util.*;
4 import javax.jms.*;
5 import junit.framework.*;
6
7 import EDU.oswego.cs.dl.util.concurrent.ClockDaemon;
8 import com.ubermq.jms.client.UnicastConnectionFactory;
9 import com.ubermq.util.*;
10
11 /**
12  * A test case that outputs information to stdout indicating
13  * message receipt time, size and latency. Can be used to gather
14  * statistics about the throughput of various protocols and handlers.
15  */

16 public class StatisticsTest
17     implements MessageListener
18 {
19     private static final org.apache.log4j.Logger log = org.apache.log4j.Logger.getLogger(StatisticsTest.class);
20     
21     public static TestSuite suite() {
22         return new TestSuite(StatisticsTest.class);
23     }
24     
25     public StatisticsTest(String JavaDoc host,
26                           int port,
27                           boolean subscriber,
28                           int size)
29     {
30         this.host = host;
31         this.port = port;
32         this.subscriber = subscriber;
33         this.size = size;
34     }
35     
36     public void run() {
37         try
38         {
39             setUp();
40             if (subscriber) {
41                 testSubscribe();
42             } else {
43                 testPublish();
44             }
45         }
46         catch (Exception JavaDoc e) {
47             log.error("", e);;
48         }
49     }
50     
51     /**
52      *
53      */

54     public static void main(String JavaDoc[] args)
55     {
56         if (args.length < 4) {
57             System.out.println("usage: host port subscriber? msg-size");
58             return;
59         }
60         
61         new StatisticsTest(
62             args[0],
63             Integer.valueOf(args[1]).intValue(),
64             Boolean.valueOf(args[2]).booleanValue(),
65             Integer.valueOf(args[3]).intValue()).run();
66     }
67     
68     protected String JavaDoc host;
69     protected int port;
70     protected boolean subscriber = true;
71     protected int size = 1000;
72     
73     private TopicConnectionFactory f;
74     private TopicConnection tc1;
75     private TopicSession ts_auto;
76     private Topic theTopic, theTopic2;
77     private List points;
78     
79     // a topic used for general testing.
80
public static final String JavaDoc THE_TOPIC = "dsakjdsalkjdsakdjsalkdsajdlsaksajdlsakdkslad";
81     public static final String JavaDoc THE_TOPIC2 = "v[vopzxcpvioczxivsd98faknr3qnmdsancsax98ucasdm";
82     
83     private static ClockDaemon cd = new ClockDaemon();
84     
85     public void setUp()
86         throws Exception JavaDoc
87     {
88         // connect
89
f = getConnectionFactory();
90         tc1 = f.createTopicConnection();
91         ts_auto = tc1.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
92         
93         theTopic = ts_auto.createTopic(THE_TOPIC);
94         theTopic2 = ts_auto.createTopic(THE_TOPIC2);
95         
96         points = new ArrayList();
97     }
98     
99     public void testSubscribe()
100         throws Exception JavaDoc
101     {
102         TopicSubscriber ts = ts_auto.createSubscriber(theTopic);
103         ts.setMessageListener(this);
104         
105         tc1.start();
106     }
107     
108     private static class DataPoint
109     {
110         long time;
111         int size;
112         long latency;
113         
114         DataPoint(long now, int size) {
115             this.time = now;
116             this.size = size;
117         }
118         
119         DataPoint(long now, long timestamp, int size) {
120             this.time = now;
121             this.size = size;
122             this.latency = now - timestamp;
123         }
124     }
125     
126     private void output(DataPoint dp)
127     {
128         System.out.println("" + dp.time + ", " + dp.size + ", " + dp.latency);
129     }
130     
131     public synchronized void onMessage(Message p0)
132     {
133         try
134         {
135             BytesMessage bm = (BytesMessage)p0;
136             long now = System.currentTimeMillis();
137             System.out.println("" + now + ", " +
138                                    size + ", " +
139                                    (now - bm.readLong()));
140             
141             System.out.println("hello!");
142             TopicPublisher p = ts_auto.createPublisher(theTopic2);
143             p.publish(p0);
144             System.out.println("Time elapsed: " + (System.currentTimeMillis() - now) + " ms");
145         }
146         catch (Exception JavaDoc e) {
147             log.error("", e);;
148         }
149     }
150     
151     public void testPublish()
152         throws Exception JavaDoc
153     {
154         TopicPublisher tp = ts_auto.createPublisher(theTopic);
155         BytesMessage bm = ts_auto.createBytesMessage();
156         byte[] bytes = new byte[size-8];
157         
158         while(true)
159         {
160             bm.reset();
161             bm.writeLong(System.currentTimeMillis());
162             bm.writeBytes(bytes);
163             
164             tp.publish(bm);
165             Thread.sleep(1); // max. throughput = 1000 * 1000 = 1000000
166
}
167     }
168     
169     protected TopicConnectionFactory getConnectionFactory()
170     {
171         return new UnicastConnectionFactory(host, port);
172     }
173     
174 }
175
Popular Tags