KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jgroups > tests > LargeStateClient


1 package org.jgroups.tests;
2
3 import java.net.ServerSocket JavaDoc;
4 import java.net.InetAddress JavaDoc;
5 import java.net.Socket JavaDoc;
6 import java.io.IOException JavaDoc;
7 import java.io.OutputStream JavaDoc;
8 import java.io.InputStream JavaDoc;
9
10 /**
11  * @author Bela Ban
12  * @version $Id: LargeStateClient.java,v 1.1 2006/05/26 06:21:13 belaban Exp $
13  */

14 public class LargeStateClient {
15     Socket JavaDoc sock;
16     InetAddress JavaDoc bind_addr, host_addr;
17     boolean looping=true;
18
19
20     private void start(String JavaDoc bind_addr, String JavaDoc host_addr, int chunk) throws Exception JavaDoc {
21         this.bind_addr=InetAddress.getByName(bind_addr);
22         this.host_addr=InetAddress.getByName(host_addr);
23         sock=new Socket JavaDoc(this.host_addr, 7500, this.bind_addr, 0);
24         sock.setReceiveBufferSize(chunk);
25         readLargeState(sock);
26         sock.close();
27     }
28
29     private void readLargeState(Socket JavaDoc sock) throws IOException JavaDoc {
30         InputStream JavaDoc in=sock.getInputStream();
31         long total=0, start=0, stop;
32         byte[] buf=new byte[sock.getReceiveBufferSize()];
33         int num;
34
35         start=System.currentTimeMillis();
36         while(looping) {
37             num=in.read(buf, 0, buf.length);
38             if(num == -1)
39                 break;
40
41             total+=num;
42             if(total % 100000000 == 0)
43                 System.out.println("-- read " + (total / 1000000) + " MB");
44         }
45         stop=System.currentTimeMillis();
46         System.out.println("- done, read " + (total / 1000000) + " MB in " + (stop-start) + "ms (" +
47                 (total / 1000000) / ((stop-start) / 1000.0) + " MB/sec");
48     }
49
50
51     public static void main(String JavaDoc[] args) throws Exception JavaDoc {
52         String JavaDoc bind_addr=null, host="localhost";
53         int chunk=10000;
54
55         for(int i=0; i < args.length; i++) {
56             if(args[i].equals("-bind_addr")) {
57                 bind_addr=args[++i];
58                 continue;
59             }
60             if(args[i].equals("-host")) {
61                 host=args[++i];
62                 continue;
63             }
64             if(args[i].equals("-chunk")) {
65                 chunk=Integer.parseInt(args[++i]);
66                 continue;
67             }
68             System.out.println("LargeStateServer [-bind_addr <addr>] [-host <host address>][-chunk <chunk size (bytes)>]");
69             return;
70         }
71         new LargeStateClient().start(bind_addr, host, chunk);
72     }
73
74
75 }
76
Popular Tags