001: package org.jgroups.tests;
002:
003: import org.jgroups.*;
004: import org.jgroups.protocols.PERF_TP;
005: import org.jgroups.protocols.pbcast.NakAckHeader;
006: import org.jgroups.stack.Protocol;
007:
008: import java.io.DataInputStream;
009: import java.io.DataOutputStream;
010: import java.util.Vector;
011:
012: /**
013: * Sends num_msgs up the stack. Stack has to have DUMMY_TP transport and PERF_TP top protocol
014: * @author Bela Ban Feb 24, 2004
015: * @version $Id: SimplePerfTest.java,v 1.2 2005/04/18 13:55:57 belaban Exp $
016: */
017: public class SimplePerfTest {
018: JChannel ch = null;
019: PERF_TP tp = null;
020: DataInputStream in = null;
021: DataOutputStream out = null;
022: private View view;
023:
024: public static void main(String[] args) {
025: String props = null;
026: int num_msgs = 1000;
027: int size = 1000; // bytes
028:
029: for (int i = 0; i < args.length; i++) {
030: if ("-props".equals(args[i])) {
031: props = args[++i];
032: continue;
033: }
034: if ("-num".equals(args[i])) {
035: num_msgs = Integer.parseInt(args[++i]);
036: continue;
037: }
038: if ("-size".equals(args[i])) {
039: size = Integer.parseInt(args[++i]);
040: continue;
041: }
042: help();
043: return;
044: }
045:
046: try {
047: new SimplePerfTest().start(props, num_msgs, size);
048: } catch (Exception e) {
049: e.printStackTrace();
050: }
051: }
052:
053: private static void help() {
054: System.out
055: .println("PerfTpTest [-help] [-props <properties>] [-num <num msgs>] "
056: + "[-size <msg size (in bytes)]");
057: }
058:
059: void start(String props, int num_msgs, int size) throws Exception {
060: Message msg;
061: Protocol transport;
062: byte[] buf = new byte[size];
063: Address local_addr;
064: NakAckHeader hdr;
065:
066: ch = new JChannel(props);
067: ch.connect("demo");
068: local_addr = ch.getLocalAddress();
069: Vector members = new Vector();
070: members.add(local_addr);
071: view = new View(local_addr, 0, members);
072: ch.down(new Event(Event.BECOME_SERVER));
073: ch.down(new Event(Event.VIEW_CHANGE, view));
074: tp = PERF_TP.getInstance();
075:
076: Vector protocols = ch.getProtocolStack().getProtocols();
077: transport = (Protocol) protocols.lastElement();
078:
079: System.out.println("sending " + num_msgs + " up the stack");
080:
081: tp.setExpectedMessages(num_msgs); // this starts the time
082: for (int i = 0; i < num_msgs; i++) {
083: msg = new Message(null, local_addr, buf);
084: hdr = new NakAckHeader(NakAckHeader.MSG, i);
085: msg.putHeader("NAKACK", hdr);
086: transport.up(new Event(Event.MSG, msg));
087: if (i % 10000 == 0) {
088: System.out.println("passed up " + i + " messages");
089: }
090: }
091: synchronized (tp) {
092: if (tp.done()) {
093: ;
094: } else {
095: tp.wait();
096: }
097: }
098: long total = tp.getTotalTime();
099: double msgs_per_ms = num_msgs / (double) total;
100: double msgs_per_sec = msgs_per_ms * 1000;
101: double time_per_msg = total / (double) num_msgs;
102: double usec_per_msg = time_per_msg * 1000;
103:
104: System.out.println("num_msgs = " + num_msgs + ", total_time = "
105: + total + "ms");
106: System.out.println("msgs/millisec = " + msgs_per_ms
107: + ", msgs/sec = " + msgs_per_sec + "\ntime/msg = "
108: + time_per_msg + " ms" + " (" + usec_per_msg
109: + " usec/msg)");
110: ch.close();
111: }
112: }
|