1 package org.jgroups.tests.perf; 2 3 import java.io.*; 4 import java.net.InetAddress ; 5 import java.net.ServerSocket ; 6 import java.net.Socket ; 7 import java.text.NumberFormat ; 8 9 14 public class JPerf { 15 boolean sender; 16 int num; 17 InetAddress local_addr; 18 int local_port; 19 int remote_port; 20 InetAddress remote_addr; 21 int size=1000; 22 int incr=1000; 23 24 static NumberFormat f; 25 26 static { 27 f=NumberFormat.getNumberInstance(); 28 f.setGroupingUsed(false); 29 f.setMaximumFractionDigits(2); 30 } 31 32 33 private void start(boolean sender, int num, int size, String local_addr, int local_port, 34 String remote_addr, int remote_port, int receivebuf, int sendbuf) throws IOException { 35 this.sender=sender; 36 this.num=num; 37 this.size=size; 38 this.local_addr=InetAddress.getByName(local_addr); 39 this.local_port=local_port; 40 this.remote_addr=InetAddress.getByName(remote_addr); 41 this.remote_port=remote_port; 42 43 incr=num / 10; 44 45 if(sender) { 46 System.out.println("-- creating socket to " + this.remote_addr + ":" + this.remote_port); 47 Socket sock=new Socket (this.remote_addr, remote_port, this.local_addr, 0); 48 sock.setReceiveBufferSize(receivebuf); 49 sock.setSendBufferSize(sendbuf); 50 DataOutputStream out=new DataOutputStream(new BufferedOutputStream(sock.getOutputStream())); 51 byte[] buf=new byte[size]; 52 int cnt=1; 53 System.out.println("-- sending " + num + " messages"); 54 for(int i=0; i < num; i++) { 55 out.write(buf, 0, buf.length); 56 out.flush(); 57 if(cnt % incr == 0) 58 System.out.println("-- sent " + cnt + " messages"); 59 cnt++; 60 } 61 } 62 else { 63 ServerSocket srv_sock=new ServerSocket (remote_port, 10, this.local_addr); 64 System.out.println("-- waiting for " + num + " messages on " + srv_sock.getLocalSocketAddress()); 65 Socket client_sock=srv_sock.accept(); 66 client_sock.setReceiveBufferSize(receivebuf); 67 client_sock.setSendBufferSize(sendbuf); 68 System.out.println("-- accepted connection from " + client_sock.getRemoteSocketAddress()); 69 DataInputStream in=new DataInputStream(new BufferedInputStream(client_sock.getInputStream())); 70 byte[] buf=new byte[size]; 71 int counter=0; 72 long start=0, stop; 73 while(counter < num) { 74 in.readFully(buf, 0, buf.length); 75 if(start == 0) 76 start=System.currentTimeMillis(); 77 counter++; 78 if(counter % incr == 0) 79 System.out.println("-- received " + counter); 80 } 81 stop=System.currentTimeMillis(); 82 long diff=stop-start; 83 double msgs_sec=(counter / (diff / 1000.0)); 84 System.out.println("\nreceived " + counter + " messages in " + diff + "ms (" + 85 f.format(msgs_sec) + " msgs/sec)"); 86 double throughput=num * size / (diff / 1000.0) / 1000.0; 87 if(throughput < 1000) 88 System.out.println("throughput: " + f.format(throughput) + "KB/sec"); 89 else { 90 throughput/=1000.0; 91 System.out.println("throughput: " + f.format(throughput) + "MB/sec"); 92 } 93 } 94 } 95 96 97 98 static void help() { 99 System.out.println("JPerf [-help] [-sender] [-num <number of msgs] [-size <bytes>] [-local_addr <interface>] [-local_port <port]" + 100 "[-remote_addr <IP addr>] [-remote_port <port>] [-receivebuf <bytes>] [-sendbuf <bytes>]"); 101 } 102 103 public static void main(String [] args) { 104 boolean sender=false; 105 int num=100000; 106 String local_addr="127.0.0.1"; 107 int local_port=5000; 108 int size=1000; 109 int remote_port=10000; 110 int receivebuf=200000, sendbuf=200000; 111 String remote_addr="127.0.0.1"; 112 113 for(int i=0; i < args.length; i++) { 114 if(args[i].equals("-sender")) { 115 sender=true; 116 continue; 117 } 118 if(args[i].equals("-num")) { 119 num=Integer.parseInt(args[++i]); 120 continue; 121 } 122 if(args[i].equals("-size")) { 123 size=Integer.parseInt(args[++i]); 124 continue; 125 } 126 if(args[i].equals("-local_addr")) { 127 local_addr=args[++i]; 128 continue; 129 } 130 if(args[i].equals("-remote_addr")) { 131 remote_addr=args[++i]; 132 continue; 133 } 134 if(args[i].equals("-local_port")) { 135 local_port=Integer.parseInt(args[++i]); 136 continue; 137 } 138 if(args[i].equals("-remote_port")) { 139 remote_port=Integer.parseInt(args[++i]); 140 continue; 141 } 142 if(args[i].equals("-receivebuf")) { 143 receivebuf=Integer.parseInt(args[++i]); 144 continue; 145 } 146 if(args[i].equals("-sendbuf")) { 147 sendbuf=Integer.parseInt(args[++i]); 148 continue; 149 } 150 help(); 151 return; 152 } 153 try { 154 new JPerf().start(sender, num, size, local_addr, local_port, remote_addr, remote_port, receivebuf, sendbuf); 155 } 156 catch(IOException e) { 157 e.printStackTrace(); 158 } 159 } 160 161 162 163 } 164 | Popular Tags |