1 package org.jgroups.tests; 2 3 import java.io.IOException ; 4 import java.io.OutputStream ; 5 import java.net.InetAddress ; 6 import java.net.ServerSocket ; 7 import java.net.Socket ; 8 9 13 public class LargeStateServer { 14 ServerSocket srv_sock; 15 InetAddress bind_addr; 16 17 18 private void start(String bind_addr, int chunk_size) throws Exception { 19 this.bind_addr=InetAddress.getByName(bind_addr); 20 srv_sock=new ServerSocket (7500, 50, this.bind_addr); 21 while(true) { 22 System.out.println("-- waiting for clients to connect"); 23 Socket sock=srv_sock.accept(); 24 sock.setSendBufferSize(chunk_size); 25 sendLargeState(sock); 26 sock.close(); 27 } 28 } 29 30 private void sendLargeState(Socket sock) throws IOException { 31 long total=0, start, stop; 32 OutputStream out=sock.getOutputStream(); 33 start=System.currentTimeMillis(); 34 for(int i=0; i < 1000000; i++) { 35 byte[] buf=new byte[1000]; 36 out.write(buf, 0, buf.length); 37 total+=1000; 38 if(total % 100000000 == 0) 39 System.out.println("-- sent " + (total / 1000000) + " MB"); 40 } 41 stop=System.currentTimeMillis(); 42 out.close(); 43 System.out.println("- done, wrote " + (total / 1000000) + " MB in " + (stop-start) + "ms (" + 44 (total / 1000000) / ((stop-start) / 1000.0) + " MB/sec"); 45 } 46 47 public static void main(String [] args) throws Exception { 48 String bind_addr=null; 49 int chunk=10000; 50 51 for(int i=0; i < args.length; i++) { 52 if(args[i].equals("-bind_addr")) { 53 bind_addr=args[++i]; 54 continue; 55 } 56 if(args[i].equals("-chunk")) { 57 chunk=Integer.parseInt(args[++i]); 58 continue; 59 } 60 System.out.println("LargeStateServer [-bind_addr <addr>] [-chunk <size in bytes>]"); 61 return; 62 } 63 new LargeStateServer().start(bind_addr, chunk); 64 } 65 66 67 } 68 | Popular Tags |