001: package org.jgroups.tests;
002:
003: import org.jgroups.*;
004: import org.jgroups.protocols.PERF_TP;
005: import org.jgroups.stack.Protocol;
006:
007: import java.io.*;
008: import java.util.Vector;
009: import java.util.LinkedList;
010: import java.util.List;
011: import java.util.Iterator;
012:
013: /**
014: * Test of PERF_TP. Requirement: transport needs to be PERF_TP
015: * @author Bela Ban Feb 24, 2004
016: * @version $Id: PerfTpTest.java,v 1.8 2005/04/18 13:55:35 belaban Exp $
017: */
018: public class PerfTpTest {
019: JChannel ch = null;
020: PERF_TP tp = null;
021: DataInputStream in = null;
022: DataOutputStream out = null;
023:
024: public static void main(String[] args) {
025: String props = null;
026: int num_msgs = 1000;
027: int size = 1000; // bytes
028: String file_name = null;
029: boolean write = true;
030:
031: for (int i = 0; i < args.length; i++) {
032: if ("-props".equals(args[i])) {
033: props = args[++i];
034: continue;
035: }
036: if ("-num".equals(args[i])) {
037: num_msgs = Integer.parseInt(args[++i]);
038: continue;
039: }
040: if ("-size".equals(args[i])) {
041: size = Integer.parseInt(args[++i]);
042: continue;
043: }
044: if ("-file_name".equals(args[i])) {
045: file_name = args[++i];
046: continue;
047: }
048: if ("-write".equals(args[i])) {
049: write = true;
050: continue;
051: }
052: if ("-read".equals(args[i])) {
053: write = false;
054: continue;
055: }
056: help();
057: return;
058: }
059:
060: try {
061: new PerfTpTest().start(props, num_msgs, size, file_name,
062: write);
063: } catch (Exception e) {
064: e.printStackTrace();
065: }
066: }
067:
068: private static void help() {
069: System.out
070: .println("PerfTpTest [-help] [-props <properties>] [-num <num msgs>] "
071: + "[-size <msg size (in bytes)] [-file_name <filename>] [-write] [-read]");
072: }
073:
074: void start(String props, int num_msgs, int size, String file_name,
075: boolean write) throws Exception {
076: Message msg;
077: Protocol transport;
078: byte[] buf = new byte[size];
079: Address local_addr;
080: View view;
081:
082: if (file_name != null) {
083: if (write)
084: out = new DataOutputStream(new FileOutputStream(
085: file_name));
086: else
087: in = new DataInputStream(new FileInputStream(file_name));
088: }
089:
090: ch = new JChannel(props);
091: ch.connect("demo");
092: tp = PERF_TP.getInstance();
093: local_addr = ch.getLocalAddress();
094: Vector members = new Vector();
095: members.add(local_addr);
096: view = new View(local_addr, 0, members);
097: ch.down(new Event(Event.BECOME_SERVER));
098: ch.down(new Event(Event.VIEW_CHANGE, view));
099:
100: if (write) {
101: tp.setExpectedMessages(num_msgs);
102: for (int i = 0; i < num_msgs; i++) {
103: msg = new Message(null, local_addr, buf);
104: ch.send(msg);
105: if (out != null)
106: msg.writeTo(out);
107: if (i % 1000 == 0)
108: System.out.println("sent " + i + " messages");
109: }
110: } else {
111: List msgs = new LinkedList();
112: Vector protocols = ch.getProtocolStack().getProtocols();
113: transport = (Protocol) protocols.lastElement();
114: int i = 0;
115: while (true) {
116: msg = new Message();
117: try {
118: msg.readFrom(in);
119: msgs.add(msg);
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 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: } else {
141: tp.wait();
142: }
143: }
144: long total = tp.getTotalTime();
145: double msgs_per_ms = num_msgs / (double) total;
146: double msgs_per_sec = msgs_per_ms * 1000;
147: double time_per_msg = total / (double) num_msgs;
148: double usec_per_msg = time_per_msg * 1000;
149:
150: System.out.println("num_msgs = " + num_msgs + ", total_time = "
151: + total + "ms");
152: System.out.println("msgs/millisec = " + msgs_per_ms
153: + ", msgs/sec = " + msgs_per_sec + "\ntime/msg = "
154: + time_per_msg + " ms" + " (" + usec_per_msg
155: + " usec/msg)");
156: ch.close();
157: }
158: }
|