KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jgroups > tests > SynchronousMessageSpeedTest


1 package org.jgroups.tests;
2
3
4 import org.jgroups.Channel;
5 import org.jgroups.JChannel;
6 import org.jgroups.Message;
7
8
9
10
11 /**
12  * Tests the time to multicast a message to everyone and then receive the responses
13  * @author Bela Ban
14  * @version $Id: SynchronousMessageSpeedTest.java,v 1.1 2005/07/22 10:30:21 belaban Exp $
15  */

16 public class SynchronousMessageSpeedTest {
17     Channel channel;
18     String JavaDoc props=null;
19     boolean server=false; // role is client by default
20
int num=1000, received=0;
21     static final long TIMEOUT=10000;
22
23
24
25     public SynchronousMessageSpeedTest(String JavaDoc props, boolean server, int num) {
26         this.props=props;
27         this.server=server;
28         this.num=num;
29     }
30
31
32
33     public void start() throws Exception JavaDoc {
34         Object JavaDoc obj;
35         Message msg;
36         channel=new JChannel(props);
37         channel.setOpt(Channel.LOCAL, Boolean.FALSE); // do *not* receive my own messages
38
channel.connect("MessageDispatcherSpeedTestGroup");
39
40         try {
41             while(channel.getNumMessages() > 0)
42                 channel.receive(10); // clear the input queue
43

44             if(server) {
45                 System.out.println("-- Started as server. Press ctrl-c to kill");
46                 int i=0;
47                 while(true) {
48                     obj=channel.receive(0);
49                     if(obj instanceof Message) {
50                         msg=(Message)obj;
51                         Message rsp=new Message(msg.getSrc(), null, null);
52                         if(++received % 1000 == 0)
53                             System.out.println("-- received " + received);
54                         channel.send(rsp);
55                     }
56                 }
57             }
58             else {
59                 sendMessages(num);
60             }
61         }
62         catch(Throwable JavaDoc t) {
63             t.printStackTrace(System.err);
64         }
65         finally {
66             channel.close();
67         }
68     }
69
70
71     void sendMessages(int num) throws Exception JavaDoc {
72         long start, stop;
73         int show=num/10;
74         Object JavaDoc obj;
75
76         if(show <=0) show=1;
77         start=System.currentTimeMillis();
78
79         System.out.println("-- sending " + num + " messages");
80         for(int i=1; i <= num; i++) {
81             channel.send(new Message());
82             if(i % show == 0)
83                 System.out.println("-- sent " + i);
84
85             while(true) {
86                 obj=channel.receive(0);
87                 if(obj instanceof Message) {
88                     received++;
89                     if(received % show == 0)
90                         System.out.println("-- received response: " + received);
91                     break;
92                 }
93             }
94         }
95         stop=System.currentTimeMillis();
96         printStats(stop-start, num);
97     }
98
99
100
101     void printStats(long total_time, int num) {
102         double throughput=((double)num)/((double)total_time/1000.0);
103         System.out.println("time for " + num + " remote calls was " +
104                            total_time + ", avg=" + (total_time / (double)num) +
105                            "ms/invocation, " + (long)throughput + " calls/sec");
106     }
107
108
109
110
111     public static void main(String JavaDoc[] args) {
112         String JavaDoc props=null;
113         boolean server=false;
114         int num=1000;
115         SynchronousMessageSpeedTest test;
116
117         for(int i=0; i < args.length; i++) {
118             if("-props".equals(args[i])) {
119                 props=args[++i];
120                 continue;
121             }
122             if("-server".equals(args[i])) {
123                 server=true;
124                 continue;
125             }
126             if("-num".equals(args[i])) {
127                 num=Integer.parseInt(args[++i]);
128                 continue;
129             }
130             help();
131             return;
132         }
133
134
135         try {
136             test=new SynchronousMessageSpeedTest(props, server, num);
137             test.start();
138         }
139         catch(Exception JavaDoc e) {
140             System.err.println(e);
141         }
142     }
143
144     static void help() {
145         System.out.println("RpcDispatcherSpeedTest [-help] [-props <props>] [-server] [-num <number of calls>]");
146     }
147
148
149 }
150
Popular Tags