KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.jgroups.tests;
2
3 import org.jgroups.*;
4 import org.jgroups.protocols.PERF_TP;
5 import org.jgroups.stack.Protocol;
6
7 import java.io.*;
8 import java.util.Vector JavaDoc;
9 import java.util.LinkedList JavaDoc;
10 import java.util.List JavaDoc;
11 import java.util.Iterator JavaDoc;
12
13 /**
14  * Test of PERF_TP. Requirement: transport needs to be PERF_TP
15  * @author Bela Ban Feb 24, 2004
16  * @version $Id: PerfTpTest.java,v 1.8 2005/04/18 13:55:35 belaban Exp $
17  */

18 public class PerfTpTest {
19     JChannel ch=null;
20     PERF_TP tp=null;
21     DataInputStream in=null;
22     DataOutputStream out=null;
23
24
25     public static void main(String JavaDoc[] args) {
26         String JavaDoc props=null;
27         int num_msgs=1000;
28         int size=1000; // bytes
29
String JavaDoc file_name=null;
30         boolean write=true;
31
32         for(int i=0; i < args.length; i++) {
33             if("-props".equals(args[i])){
34                 props=args[++i];
35                 continue;
36             }
37             if("-num".equals(args[i])) {
38                 num_msgs=Integer.parseInt(args[++i]);
39                 continue;
40             }
41             if("-size".equals(args[i])) {
42                 size=Integer.parseInt(args[++i]);
43                 continue;
44             }
45             if("-file_name".equals(args[i])) {
46                 file_name=args[++i];
47                 continue;
48             }
49             if("-write".equals(args[i])) {
50                 write=true;
51                 continue;
52             }
53             if("-read".equals(args[i])) {
54                 write=false;
55                 continue;
56             }
57             help();
58             return;
59         }
60
61         try {
62             new PerfTpTest().start(props, num_msgs, size, file_name, write);
63         }
64         catch(Exception JavaDoc e) {
65             e.printStackTrace();
66         }
67     }
68
69     private static void help() {
70         System.out.println("PerfTpTest [-help] [-props <properties>] [-num <num msgs>] " +
71                            "[-size <msg size (in bytes)] [-file_name <filename>] [-write] [-read]");
72     }
73
74     void start(String JavaDoc props, int num_msgs, int size, String JavaDoc file_name, boolean write) throws Exception JavaDoc {
75         Message msg;
76         Protocol transport;
77         byte[] buf=new byte[size];
78         Address local_addr;
79         View view;
80
81         if(file_name != null) {
82             if(write)
83                 out=new DataOutputStream(new FileOutputStream(file_name));
84             else
85                 in=new DataInputStream(new FileInputStream(file_name));
86         }
87
88         ch=new JChannel(props);
89         ch.connect("demo");
90         tp=PERF_TP.getInstance();
91         local_addr=ch.getLocalAddress();
92         Vector JavaDoc members=new Vector JavaDoc();
93         members.add(local_addr);
94         view=new View(local_addr, 0, members);
95         ch.down(new Event(Event.BECOME_SERVER));
96         ch.down(new Event(Event.VIEW_CHANGE, view));
97
98         if(write) {
99             tp.setExpectedMessages(num_msgs);
100             for(int i=0; i < num_msgs; i++) {
101                 msg=new Message(null, local_addr, buf);
102                 ch.send(msg);
103                 if(out != null)
104                     msg.writeTo(out);
105                 if(i % 1000 == 0)
106                     System.out.println("sent " + i + " messages");
107             }
108         }
109         else {
110             List JavaDoc msgs=new LinkedList JavaDoc();
111             Vector JavaDoc protocols=ch.getProtocolStack().getProtocols();
112             transport=(Protocol)protocols.lastElement();
113             int i=0;
114             while(true) {
115                 msg=new Message();
116                 try {
117                     msg.readFrom(in);
118                     msgs.add(msg);
119                 }
120                 catch(EOFException eof) {
121                     break;
122                 }
123             }
124
125             num_msgs=msgs.size();
126             System.out.println("read " + num_msgs + " msgs from file");
127             tp.setExpectedMessages(msgs.size()); // this starts the time
128
for(Iterator JavaDoc it=msgs.iterator(); it.hasNext();) {
129                 msg=(Message)it.next();
130                 i++;
131                 transport.up(new Event(Event.MSG, msg));
132                 if(i % 10000 == 0) {
133                     System.out.println("passed up " + i + " messages");
134                 }
135             }
136         }
137         synchronized(tp) {
138             if(tp.done()) {
139                 ;
140             }
141             else {
142                 tp.wait();
143             }
144         }
145         long total=tp.getTotalTime();
146         double msgs_per_ms=num_msgs / (double)total;
147         double msgs_per_sec=msgs_per_ms * 1000;
148         double time_per_msg=total / (double)num_msgs;
149         double usec_per_msg=time_per_msg * 1000;
150
151         System.out.println("num_msgs = " + num_msgs + ", total_time = " + total + "ms");
152         System.out.println("msgs/millisec = " + msgs_per_ms + ", msgs/sec = " + msgs_per_sec +
153                 "\ntime/msg = " + time_per_msg + " ms" +
154                 " (" + usec_per_msg + " usec/msg)");
155         ch.close();
156     }
157 }
158
Popular Tags