001: // $Id: MessageSerializationTest.java,v 1.10 2005/08/10 11:31:03 belaban Exp $
002:
003: package org.jgroups.tests;
004:
005: /**
006: * @author Filip Hanik
007: * @author Bela Ban
008: * @version 1.0
009: */
010:
011: import org.jgroups.Message;
012: import org.jgroups.util.MagicObjectOutputStream;
013: import org.jgroups.util.MagicObjectInputStream;
014: import org.jgroups.util.ExposedByteArrayOutputStream;
015: import org.jgroups.util.Buffer;
016: import org.jgroups.conf.ClassConfigurator;
017: import org.jgroups.protocols.*;
018: import org.jgroups.stack.IpAddress;
019: import org.apache.commons.logging.Log;
020: import org.apache.commons.logging.LogFactory;
021:
022: import java.net.InetAddress;
023: import java.io.*;
024:
025: public class MessageSerializationTest {
026: static final Log log = LogFactory
027: .getLog(MessageSerializationTest.class);
028:
029: public MessageSerializationTest() {
030: }
031:
032: public static void main(String[] args) throws Exception {
033: boolean add_headers = false;
034: InetAddress addr = InetAddress.getLocalHost();
035: int num = 10000;
036: boolean use_magic = false;
037: boolean use_streamable = false;
038:
039: for (int i = 0; i < args.length; i++) {
040: if ("-add_headers".equals(args[i])) {
041: add_headers = true;
042: continue;
043: }
044: if ("-num".equals(args[i])) {
045: num = Integer.parseInt(args[++i]);
046: continue;
047: }
048: if ("-use_magic".equals(args[i])) {
049: use_magic = true;
050: continue;
051: }
052: if ("-use_streamable".equals(args[i])) {
053: use_streamable = true;
054: continue;
055: }
056: help();
057: return;
058: }
059:
060: ClassConfigurator.getInstance(true);
061: long start = System.currentTimeMillis();
062: for (int i = 0; i < num; i++) {
063: Message m = new Message(new IpAddress(addr, 5555),
064: new IpAddress(addr, 6666), new byte[1000]);
065: if (add_headers)
066: addHeaders(m);
067:
068: ExposedByteArrayOutputStream msg_data = new ExposedByteArrayOutputStream();
069: Buffer jgbuf;
070:
071: if (use_streamable) {
072: DataOutputStream dos = new DataOutputStream(msg_data);
073: m.writeTo(dos);
074: dos.close();
075: } else {
076: ObjectOutputStream msg_out = use_magic ? new MagicObjectOutputStream(
077: msg_data)
078: : new ObjectOutputStream(msg_data);
079: m.writeExternal(msg_out);
080: // msg_out.writeObject(m);
081: msg_out.close();
082: }
083:
084: jgbuf = new Buffer(msg_data.getRawBuffer(), 0, msg_data
085: .size());
086:
087: ByteArrayInputStream msg_in_data = new ByteArrayInputStream(
088: jgbuf.getBuf(), jgbuf.getOffset(), jgbuf
089: .getLength());
090: Message m2 = (Message) Message.class.newInstance();
091:
092: if (use_streamable) {
093: DataInputStream dis = new DataInputStream(msg_in_data);
094: m2.readFrom(dis);
095: dis.close();
096: } else {
097: ObjectInputStream msg_in = use_magic ? new MagicObjectInputStream(
098: msg_in_data)
099: : new ObjectInputStream(msg_in_data);
100:
101: m2.readExternal(msg_in);
102: // Message m2=(Message)msg_in.readObject();
103: msg_in.close();
104: }
105:
106: }
107:
108: long stop = System.currentTimeMillis();
109: System.out.println("Serializing and deserializing a message "
110: + num + " times took " + (stop - start) + "ms.");
111: }
112:
113: /**
114: * Adds some dummy headers to the message
115: */
116: static void addHeaders(Message msg) {
117: msg.putHeader("UDP", new UdpHeader("MyGroup"));
118: msg.putHeader("PING", new PingHeader(PingHeader.GET_MBRS_REQ,
119: null));
120: msg.putHeader("FD_SOCK", new FD_SOCK.FdHeader());
121: msg.putHeader("VERIFY_SUSPECT",
122: new VERIFY_SUSPECT.VerifyHeader());
123: msg.putHeader("STABLE",
124: new org.jgroups.protocols.pbcast.STABLE.StableHeader());
125: msg.putHeader("NAKACK",
126: new org.jgroups.protocols.pbcast.NakAckHeader());
127: msg.putHeader("UNICAST", new UNICAST.UnicastHeader());
128: msg.putHeader("FRAG", new FragHeader());
129: msg.putHeader("GMS",
130: new org.jgroups.protocols.pbcast.GMS.GmsHeader());
131: }
132:
133: static void help() {
134: System.out
135: .println("MessageSerializationTest [-help] [-add_headers] [-num <iterations>] "
136: + "[-use_magic] [-use_streamable]");
137: }
138: }
|