test client
[IRC.git] / Robust / src / Benchmarks / Chat / NetsClient.java
1 import java.awt.*;
2 import java.io.*;
3 import java.net.*;
4 import java.util.*;
5
6 public class NetsClient extends Thread {
7
8     static boolean debug;
9     static int sendoption=0;
10
11     public static void main(String argv[]) {
12
13         String host=null;
14         int numberofclients=0;
15         int numberofmessages=0;
16         int port=4321;
17
18         NetsClient.debug=false;
19         try {
20             host=argv[0];
21             port=Integer.parseInt(argv[1]);
22             numberofclients=Integer.parseInt(argv[2]);
23             numberofmessages=Integer.parseInt(argv[3]);
24         }
25         catch (Exception e) {
26             System.out.println("NetsClient host port numberofclients numberofmessages debugflag");
27         }
28         try {
29             NetsClient.debug=(Integer.parseInt(argv[4])==1);
30             NetsClient.sendoption=Integer.parseInt(argv[5]);
31         } catch (Exception e) {}
32
33         NetsClient[] tarray=new NetsClient[numberofclients];
34         for (int i = 0; i < numberofclients; i++) {
35             String room="test";
36             tarray[i] = new NetsClient(i, host, port,
37                                        numberofmessages, numberofclients, room);
38             if (debug)
39                 System.out.println("Attempting to start "+i);
40             tarray[i].connectt();
41         }
42
43         long starttime=System.currentTimeMillis();
44         for (int i = 0; i < numberofclients; i++)
45             tarray[i].start();
46         try {
47             for (int i = 0; i < numberofclients; i++) {
48                 tarray[i].join();
49             }
50         } catch (InterruptedException e) {
51             e.printStackTrace();
52             System.out.println(e);
53         }
54         long endtime=System.currentTimeMillis();
55
56         int messages=0;
57         for(int i=0;i<numberofclients;i++)
58             messages+=tarray[i].lines;
59
60         System.out.println("ChatClient");
61         System.out.println("numclients:" + numberofclients);
62         System.out.println("port:" + port);
63         System.out.println("number of messages:" + numberofmessages);
64         System.out.println("Elapsed time:(mS)" + (endtime - starttime));
65         System.out.println("Throughput:" + (double) numberofclients*
66                            ((sendoption==4) ? 1 : numberofclients) *
67                            numberofmessages/((double) (endtime-starttime)));
68         System.out.println("Lines="+messages+" out of "+numberofclients*(numberofclients-1)*numberofmessages);
69     }
70
71     public NetsClient(int clientnumber, String host,
72                       int port, int nom, int noc, String room) {
73         this.port=port;
74         this.clientnumber=clientnumber;
75         this.host=host;
76         this.nom=nom;
77         this.noc=noc;
78         this.room=room;
79     }
80
81     String room;
82     int nom, noc,clientnumber,port;
83     String host;
84     Socket sock;
85     PrintStream pout;
86     InputStream in;
87     OutputStream out;
88     //DataInputStream din;
89     BufferedReader d;
90     int lines=0;
91
92     public void connectt() {
93         try{
94             sock = new Socket(host, port); // unix server
95             if (debug)
96                 System.out.println("connection made");
97             in = sock.getInputStream();
98             out = sock.getOutputStream();
99             pout = new PrintStream(out);
100             //din = new DataInputStream(in);
101             pout.println(room);
102         }
103         catch (UnknownHostException e ) {
104             System.out.println("can't find host");
105         }
106         catch (IOException e) {
107             System.out.println("Error connecting to host");
108         }
109     }
110
111     public void run() {
112         if (debug)
113             System.out.println("client thread started");
114         int ns=0;
115
116         try {
117             for(int nr=0;nr<noc*nom;nr++) {
118                 if ((nr%noc)==clientnumber) {
119                     ns++;
120                     pout.println("0|"+clientnumber+"|hello#"+ns+"**");
121                 }
122                 while(in.available()>0) {
123                     int nchar=in.read();
124                     if (nchar==10)
125                         lines++;
126                 }
127             }
128             pout.flush();
129             long time=System.currentTimeMillis();
130             while((System.currentTimeMillis()-time)<5*1000) {
131                 if(in.available()>0) {
132                     int nchar=in.read();
133                     time=System.currentTimeMillis();
134                     if (nchar==10)
135                         lines++;
136                 }
137             }
138         }
139
140         catch (UnknownHostException e ) {System.out.println("can't find host"); }
141         catch ( IOException e ) {System.out.println("Error connecting to host");}
142
143     }
144
145 } // end of client class