KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.jgroups.tests;
2
3 import org.jgroups.*;
4 import org.jgroups.protocols.PERF_TP;
5 import org.jgroups.protocols.pbcast.NakAckHeader;
6 import org.jgroups.stack.Protocol;
7
8 import java.io.DataInputStream JavaDoc;
9 import java.io.DataOutputStream JavaDoc;
10 import java.util.Vector JavaDoc;
11
12 /**
13  * Sends num_msgs up the stack. Stack has to have DUMMY_TP transport and PERF_TP top protocol
14  * @author Bela Ban Feb 24, 2004
15  * @version $Id: SimplePerfTest.java,v 1.2 2005/04/18 13:55:57 belaban Exp $
16  */

17 public class SimplePerfTest {
18     JChannel ch=null;
19     PERF_TP tp=null;
20     DataInputStream JavaDoc in=null;
21     DataOutputStream JavaDoc out=null;
22     private View view;
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

30         for(int i=0; i < args.length; i++) {
31             if("-props".equals(args[i])){
32                 props=args[++i];
33                 continue;
34             }
35             if("-num".equals(args[i])) {
36                 num_msgs=Integer.parseInt(args[++i]);
37                 continue;
38             }
39             if("-size".equals(args[i])) {
40                 size=Integer.parseInt(args[++i]);
41                 continue;
42             }
43             help();
44             return;
45         }
46
47         try {
48             new SimplePerfTest().start(props, num_msgs, size);
49         }
50         catch(Exception JavaDoc e) {
51             e.printStackTrace();
52         }
53     }
54
55     private static void help() {
56         System.out.println("PerfTpTest [-help] [-props <properties>] [-num <num msgs>] " +
57                            "[-size <msg size (in bytes)]");
58     }
59
60     void start(String JavaDoc props, int num_msgs, int size) throws Exception JavaDoc {
61         Message msg;
62         Protocol transport;
63         byte[] buf=new byte[size];
64         Address local_addr;
65         NakAckHeader hdr;
66
67         ch=new JChannel(props);
68         ch.connect("demo");
69         local_addr=ch.getLocalAddress();
70         Vector JavaDoc members=new Vector JavaDoc();
71         members.add(local_addr);
72         view=new View(local_addr, 0, members);
73         ch.down(new Event(Event.BECOME_SERVER));
74         ch.down(new Event(Event.VIEW_CHANGE, view));
75         tp=PERF_TP.getInstance();
76
77         Vector JavaDoc protocols=ch.getProtocolStack().getProtocols();
78         transport=(Protocol)protocols.lastElement();
79
80
81         System.out.println("sending " + num_msgs + " up the stack");
82
83         tp.setExpectedMessages(num_msgs); // this starts the time
84
for(int i=0; i < num_msgs; i++) {
85             msg=new Message(null, local_addr, buf);
86             hdr=new NakAckHeader(NakAckHeader.MSG, i);
87             msg.putHeader("NAKACK", hdr);
88             transport.up(new Event(Event.MSG, msg));
89             if(i % 10000 == 0) {
90                 System.out.println("passed up " + i + " messages");
91             }
92         }
93         synchronized(tp) {
94             if(tp.done()) {
95                 ;
96             }
97             else {
98                 tp.wait();
99             }
100         }
101         long total=tp.getTotalTime();
102         double msgs_per_ms=num_msgs / (double)total;
103         double msgs_per_sec=msgs_per_ms * 1000;
104         double time_per_msg=total / (double)num_msgs;
105         double usec_per_msg=time_per_msg * 1000;
106
107         System.out.println("num_msgs = " + num_msgs + ", total_time = " + total + "ms");
108         System.out.println("msgs/millisec = " + msgs_per_ms + ", msgs/sec = " + msgs_per_sec +
109                 "\ntime/msg = " + time_per_msg + " ms" +
110                 " (" + usec_per_msg + " usec/msg)");
111         ch.close();
112     }
113 }
114
Popular Tags