1 3 package org.jgroups.tests; 4 5 10 11 import org.jgroups.Message; 12 import org.jgroups.util.MagicObjectOutputStream; 13 import org.jgroups.util.MagicObjectInputStream; 14 import org.jgroups.util.ExposedByteArrayOutputStream; 15 import org.jgroups.util.Buffer; 16 import org.jgroups.conf.ClassConfigurator; 17 import org.jgroups.protocols.*; 18 import org.jgroups.stack.IpAddress; 19 import org.apache.commons.logging.Log; 20 import org.apache.commons.logging.LogFactory; 21 22 import java.net.InetAddress ; 23 import java.io.*; 24 25 26 public class MessageSerializationTest { 27 static final Log log=LogFactory.getLog(MessageSerializationTest.class); 28 29 public MessageSerializationTest() { 30 } 31 32 33 public static void main(String [] args) throws Exception { 34 boolean add_headers=false; 35 InetAddress addr=InetAddress.getLocalHost(); 36 int num=10000; 37 boolean use_magic=false; 38 boolean use_streamable=false; 39 40 for(int i=0; i < args.length; i++) { 41 if("-add_headers".equals(args[i])) { 42 add_headers=true; 43 continue; 44 } 45 if("-num".equals(args[i])) { 46 num=Integer.parseInt(args[++i]); 47 continue; 48 } 49 if("-use_magic".equals(args[i])) { 50 use_magic=true; 51 continue; 52 } 53 if("-use_streamable".equals(args[i])) { 54 use_streamable=true; 55 continue; 56 } 57 help(); 58 return; 59 } 60 61 62 ClassConfigurator.getInstance(true); 63 long start=System.currentTimeMillis(); 64 for(int i=0; i < num; i++) { 65 Message m=new Message(new IpAddress(addr, 5555), new IpAddress(addr, 6666), new byte[256]); 66 if(add_headers) 67 addHeaders(m); 68 69 ExposedByteArrayOutputStream msg_data=new ExposedByteArrayOutputStream(); 70 Buffer jgbuf; 71 72 if(use_streamable) { 73 DataOutputStream dos=new DataOutputStream(msg_data); 74 m.writeTo(dos); 75 dos.close(); 76 } 77 else { 78 ObjectOutputStream msg_out=use_magic? new MagicObjectOutputStream(msg_data) : new ObjectOutputStream(msg_data); 79 m.writeExternal(msg_out); 80 msg_out.close(); 82 } 83 84 jgbuf=new Buffer(msg_data.getRawBuffer(), 0, msg_data.size()); 85 86 ByteArrayInputStream msg_in_data=new ByteArrayInputStream(jgbuf.getBuf(), jgbuf.getOffset(), jgbuf.getLength()); 87 Message m2=(Message)Message.class.newInstance(); 88 89 if(use_streamable) { 90 DataInputStream dis=new DataInputStream(msg_in_data); 91 m2.readFrom(dis); 92 dis.close(); 93 } 94 else { 95 ObjectInputStream msg_in=use_magic? new MagicObjectInputStream(msg_in_data) : new ObjectInputStream(msg_in_data); 96 97 m2.readExternal(msg_in); 98 msg_in.close(); 100 } 101 102 } 103 104 long stop=System.currentTimeMillis(); 105 System.out.println("Serializing and deserializing a message " + num + " times took " + (stop - start) + "ms."); 106 } 107 108 111 static void addHeaders(Message msg) { 112 msg.putHeader("UDP", new UdpHeader("MyGroup")); 113 msg.putHeader("PING", new PingHeader(PingHeader.GET_MBRS_REQ, null)); 114 msg.putHeader("FD_SOCK", new FD_SOCK.FdHeader()); 115 msg.putHeader("VERIFY_SUSPECT", new VERIFY_SUSPECT.VerifyHeader()); 116 msg.putHeader("STABLE", new org.jgroups.protocols.pbcast.STABLE.StableHeader()); 117 msg.putHeader("NAKACK", new org.jgroups.protocols.pbcast.NakAckHeader()); 118 msg.putHeader("UNICAST", new UNICAST.UnicastHeader()); 119 msg.putHeader("FRAG", new FragHeader()); 120 msg.putHeader("GMS", new org.jgroups.protocols.pbcast.GMS.GmsHeader()); 121 } 122 123 124 static void help() { 125 System.out.println("MessageSerializationTest [-help] [-add_headers] [-num <iterations>] " + 126 "[-use_magic] [-use_streamable]"); 127 } 128 } 129 | Popular Tags |