KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > applis > ejb > perfs > PerfsClient


1 // PerfsClient.java
2

3 package applis.ejb.perfs;
4
5 import java.util.Date;
6 import javax.naming.Context;
7 import javax.naming.InitialContext;
8 import javax.rmi.PortableRemoteObject;
9 import javax.transaction.UserTransaction;
10
11 import util.Trace;
12
13
14 class PerfsClientThread extends Thread {
15
16     static private int trace = Trace.DB_1;
17
18     private int m_loops;
19     private int m_reads;
20     private int m_writes;
21     private int m_reqsize;
22     private int m_beans;
23     private int m_tx;
24     private long m_time;
25     private int m_perfnb;
26
27     public PerfsClientThread(int b, int r, int w, int s, int n, int t, int perfnb) {
28         m_loops = b;
29         m_reads = r;
30         m_writes = w;
31         m_reqsize = s;
32         m_beans = n;
33         m_tx = t;
34         m_perfnb = perfnb;
35     }
36
37     public void run() {
38             
39         Trace.outln(trace, "Running a PerfsClientThread...");
40
41         UserTransaction ut = null;
42         Context initialContext = null;
43         try {
44             initialContext = new InitialContext();
45             ut = Transaction.getUserTransaction();
46         } catch (Exception e) {
47             Trace.errln("ERROR: Cannot lookup UserTransaction:\n"+e);
48             System.exit(2);
49         }
50         if (ut == null) {
51             Trace.errln("ERROR: Cannot get UserTransaction");
52             System.exit(2);
53         }
54         if (m_beans == 0) {
55             Trace.outln(trace, "No bean");
56             return;
57         }
58
59         // Connecting to each Perfs_X Home, and get the Perfs object
60
Perfs t_objPerfs [] = new Perfs [m_beans];
61         for (int i = 1; i <= m_beans; i++) {
62             String beanName = "";
63             switch (m_perfnb) {
64             case 1:
65                 beanName = "PerfsHome_";
66                 break;
67             case 2:
68                 beanName = "Perfs2Home_";
69                 break;
70             case 3:
71                 beanName = "Perfs3Home_";
72                 break;
73             }
74             beanName += i;
75             PerfsHome home = null;
76             try {
77                 home = (PerfsHome) PortableRemoteObject.narrow(initialContext.lookup(beanName), PerfsHome.class);
78             } catch (Exception e) {
79                 Trace.errln("ERROR: Cannot lookup "+beanName+":\n"+e);
80                 System.exit(2);
81             }
82             Trace.outln(trace, "Connected to the '"+beanName+"' Object");
83
84             try {
85                 t_objPerfs[i-1] = home.findByCode(m_reqsize/100);
86             } catch (Exception e) {
87                 Trace.errln("ERROR: Cannot find the object " + m_reqsize/100 + " of "+beanName+":\n"+e);
88                 System.exit(2);
89             }
90         }
91         
92         // Build the message used for the write request
93
String s_writeMsg = new String();
94         String s_date = (new Date().toString());
95         while ((s_writeMsg.length() + s_date.length() + 1) <= m_reqsize) {
96             s_writeMsg = s_writeMsg.concat(s_date);
97             s_writeMsg = s_writeMsg.concat(";");
98         }
99         for (int i = s_writeMsg.length(); i < m_reqsize; i++) {
100             s_writeMsg = s_writeMsg.concat(".");
101         }
102
103         Trace.outln(trace, "Start loop for " + m_loops + " transactions on " + m_beans + " servers");
104         Trace.outln(trace, "Each transaction does " + m_reads + " read and " + m_writes + " write requests");
105         Trace.outln(trace, "Request size is " + m_reqsize);
106
107         // Loop on transactions
108
long d_begin = System.currentTimeMillis();
109
110         for (int l = 0; l < m_loops; l++) {
111             // Begin the transaction
112
if (m_tx == PerfsClient.TX_CLIENT) {
113                 try {
114                     ut.begin();
115                 } catch (Exception e) {
116                     Trace.errln("ERROR: Problem during begin:\n"+e);
117                     System.exit(2);
118                 }
119             }
120
121             // Requests in the transaction
122
try {
123                 for (int r = 0; r < m_reads + m_writes; r++) {
124                     if (r < m_reads) {
125                         if (m_tx == PerfsClient.TX_CONTAINER) {
126                             t_objPerfs[r%m_beans].txread();
127                         } else {
128                             t_objPerfs[r%m_beans].read();
129                         }
130                     } else {
131                         if (m_tx == PerfsClient.TX_CONTAINER) {
132                             t_objPerfs[r%m_beans].txwrite(s_writeMsg);
133                         } else {
134                             t_objPerfs[r%m_beans].write(s_writeMsg);
135                         }
136                     }
137                 }
138             } catch (Exception e) {
139                 Trace.errln("ERROR: Problem during a bussiness method:\n"+e);
140                 System.exit(2);
141             }
142
143             // Commit the transaction
144
if (m_tx == PerfsClient.TX_CLIENT) {
145                 try {
146                     ut.commit();
147                 } catch (Exception e) {
148                     Trace.errln("ERROR: Problem during a commit:\n"+e);
149                     System.exit(2);
150                 }
151             }
152
153             Trace.outln(trace, "End of loop " + (l+1) + " OK");
154         }
155         
156         long d_end = System.currentTimeMillis();
157         m_time = (d_end - d_begin) / (m_loops);
158         Trace.outln(trace,"Time per transaction in millisec: " + m_time);
159     }
160     public long getTime(){
161         return m_time;
162     }
163 }
164
165
166 public class PerfsClient {
167
168     static private int trace = Trace.DB_1;
169
170     static public int TX_CLIENT = 0;
171     static public int TX_CONTAINER = 1;
172     static public int TX_NONE = 2;
173
174     private static final int m_MAXBEANS = 3;
175     private static final int m_MAXTHREADS = 50;
176     private static final int m_MAXREQSIZE = 800;
177
178     private static int m_loops = 1;
179     private static int m_poolsize = 1;
180     private static int m_reads = 0;
181     private static int m_writes = 0;
182     private static int m_beans = 0;
183     private static int m_reqsize = 100;
184     private static boolean m_recreate = false;
185     private static boolean m_cmp2 = false;
186     private static boolean m_irc = false;
187     private static int m_tx = TX_CLIENT;
188
189
190     private static void usage() {
191         Trace.errln("Usage: java applis.ejb.perfs.PerfsClient <args>");
192         Trace.errln(" -i re-create the data-base tables");
193         Trace.errln(" -2 use beans cmp2");
194         Trace.errln(" -b <loop> number of loops (transactions) per thread");
195         Trace.errln(" -p <thread> number of client threads");
196         Trace.errln(" -r <read> number of read requests per transaction");
197         Trace.errln(" -w <write> number of write requests per transaction");
198         Trace.errln(" -s <size> request data size in bytes");
199         Trace.errln(" -n <beans> number of Perfs beans used");
200         Trace.errln(" -h no tx");
201         Trace.errln(" -c container tx");
202         Trace.errln("default args:");
203         Trace.errln(" -b " + m_loops +
204                     " -p " + m_poolsize +
205                     " -r " + m_reads +
206                     " -w " + m_writes +
207                     " -s " + m_reqsize +
208                     " -n " + m_beans
209                     );
210     }
211
212     public static void main(String args[]) {
213
214         Trace.configure();
215
216         // Get command args
217
for (int argn = 0; argn < args.length; argn++) {
218             String s_arg = args[argn];
219             Integer i_arg;
220             if ((s_arg.equals("-b") == true)) {
221                 s_arg = args[++argn];
222                 i_arg = java.lang.Integer.valueOf(s_arg);
223                 m_loops = i_arg.intValue();
224             } else if ((s_arg.equals("-p") == true)) {
225                 s_arg = args[++argn];
226                 i_arg = java.lang.Integer.valueOf(s_arg);
227                 m_poolsize = i_arg.intValue();
228             } else if ((s_arg.equals("-r") == true)) {
229                 s_arg = args[++argn];
230                 i_arg = java.lang.Integer.valueOf(s_arg);
231                 m_reads = i_arg.intValue();
232             } else if ((s_arg.equals("-w") == true)) {
233                 s_arg = args[++argn];
234                 i_arg = java.lang.Integer.valueOf(s_arg);
235                 m_writes = i_arg.intValue();
236             } else if ((s_arg.equals("-s") == true)) {
237                 s_arg = args[++argn];
238                 i_arg = java.lang.Integer.valueOf(s_arg);
239                 m_reqsize = i_arg.intValue();
240             } else if ((s_arg.equals("-n") == true)) {
241                 s_arg = args[++argn];
242                 i_arg = java.lang.Integer.valueOf(s_arg);
243                 m_beans = i_arg.intValue();
244             } else if (s_arg.equals("-i") == true) {
245                 m_recreate = true;
246             } else if (s_arg.equals("-cmp2") == true) {
247                 m_cmp2 = true;
248             } else if (s_arg.equals("-irc") == true) {
249                 m_irc = true;
250             } else if (s_arg.equals("-h") == true) {
251                 m_tx = TX_NONE;
252             } else if (s_arg.equals("-c") == true) {
253                 m_tx = TX_CONTAINER;
254             } else if (s_arg.equals("-?") == true) {
255                 usage();
256                 System.exit(0);
257             } else {
258                 usage();
259                 System.exit(2);
260             }
261         }
262
263         // Check command args
264
if (m_loops < 1) {
265             usage();
266             System.exit(2);
267         }
268         if ((m_poolsize < 1) || (m_poolsize > m_MAXTHREADS)) {
269             usage();
270             System.exit(2);
271         }
272         if (m_reads < 0) {
273             usage();
274             System.exit(2);
275         }
276         if (m_writes < 0) {
277             usage();
278             System.exit(2);
279         }
280         if ((m_reqsize < 1) || (m_reqsize > m_MAXREQSIZE)) {
281             usage();
282             System.exit(2);
283         }
284         if ((m_reqsize%100) != 0) {
285             usage();
286             System.exit(2);
287         }
288         if (m_beans > m_MAXBEANS) {
289             usage();
290             System.exit(2);
291         }
292         if (m_reads + m_writes < m_beans) {
293             Trace.errln("All beans will not be used. You must have r+w>=n");
294             System.exit(2);
295         }
296     
297         Trace.outln(trace,
298                     "PerfsClient" +
299                     " -b " + m_loops + " -p " + m_poolsize +
300                     " -r " + m_reads + " -w " + m_writes +
301                     " -s " + m_reqsize + " -n " + m_beans
302                     );
303
304
305         if (m_recreate) {
306             // Connecting to the DBHome object to create the Perfs_X tables
307
DBHome dbhome = null;
308             DB session = null;
309             String sName = "PerfsDBHome";
310             try {
311                 Context initialContext = new InitialContext();
312                 dbhome = (DBHome) PortableRemoteObject.narrow(initialContext.lookup(sName), DBHome.class);
313             } catch (Exception e) {
314                 Trace.errln("ERROR: Cannot bind to PerfsDBHome:\n"+e);
315                 System.exit(2);
316             }
317             Trace.outln(trace, "Connected to the PerfsDBHome Object");
318  
319             try {
320                 session = dbhome.create();
321                 for (int i = 1; i <= 3; i++) {
322                     if (m_cmp2) {
323                         session.initTable(i, m_irc);
324                         Trace.outln(trace, "Table "+i+" initialized");
325                     } else {
326                         session.createTable("Perfs_"+i);
327                         Trace.outln(trace, "Table Perfs_"+i+" created");
328                     }
329                 }
330             } catch (Exception e) {
331                 Trace.errln("ERROR: Problem during the creation of the tables:\n"+e);
332                 System.exit(2);
333             }
334         }
335
336         // Start pool of client threads
337
PerfsClientThread t_thr[] = new PerfsClientThread [m_MAXTHREADS];
338         long ttime[] = new long[m_MAXTHREADS];
339         for (int p = 0; p < m_poolsize; p++) {
340             int pnb;
341             if (m_cmp2) {
342                 if (m_irc) {
343                     pnb = 3;
344                 } else {
345                     pnb = 2;
346                 }
347             } else {
348                 pnb = 1;
349             }
350             t_thr[p] = new PerfsClientThread(m_loops, m_reads, m_writes, m_reqsize, m_beans, m_tx, pnb);
351             t_thr[p].start();
352         }
353         for (int p = 0; p < m_poolsize; p++) {
354             try {
355                 t_thr[p].join();
356                 ttime[p] = t_thr[p].getTime();
357
358             } catch (InterruptedException e) {
359                 Trace.errln("ERROR: Problem in PerfsClientThread.join():\n"+e);
360                 System.exit(2);
361             }
362         }
363         String txtype = " - no tx";
364         if (m_tx == TX_CONTAINER) txtype = " - container tx";
365         if (m_tx == TX_CLIENT) txtype = " - client tx";
366
367         Trace.out("PerfsClient " +
368                   " -b " + m_loops +
369                   " -p " + m_poolsize +
370                   " -r " + m_reads +
371                   " -w " + m_writes +
372                   " -n " + m_beans +
373                   " -s " + m_reqsize +
374                   txtype);
375         for (int p = 0; p < m_poolsize; p++) {
376             Trace.out(" "+ ttime[p]);
377         }
378         Trace.outln("");
379     }
380
381 }
382
Popular Tags