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 ; 9 import java.io.DataOutputStream ; 10 import java.util.Vector ; 11 12 17 public class SimplePerfTest { 18 JChannel ch=null; 19 PERF_TP tp=null; 20 DataInputStream in=null; 21 DataOutputStream out=null; 22 private View view; 23 24 25 public static void main(String [] args) { 26 String props=null; 27 int num_msgs=1000; 28 int size=1000; 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 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 props, int num_msgs, int size) throws Exception { 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 members=new Vector (); 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 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); 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 |