001: // PerfsClient.java
002:
003: package applis.ejb.perfs;
004:
005: import java.util.Date;
006: import javax.naming.Context;
007: import javax.naming.InitialContext;
008: import javax.rmi.PortableRemoteObject;
009: import javax.transaction.UserTransaction;
010:
011: import util.Trace;
012:
013: class PerfsClientThread extends Thread {
014:
015: static private int trace = Trace.DB_1;
016:
017: private int m_loops;
018: private int m_reads;
019: private int m_writes;
020: private int m_reqsize;
021: private int m_beans;
022: private int m_tx;
023: private long m_time;
024: private int m_perfnb;
025:
026: public PerfsClientThread(int b, int r, int w, int s, int n, int t,
027: int perfnb) {
028: m_loops = b;
029: m_reads = r;
030: m_writes = w;
031: m_reqsize = s;
032: m_beans = n;
033: m_tx = t;
034: m_perfnb = perfnb;
035: }
036:
037: public void run() {
038:
039: Trace.outln(trace, "Running a PerfsClientThread...");
040:
041: UserTransaction ut = null;
042: Context initialContext = null;
043: try {
044: initialContext = new InitialContext();
045: ut = Transaction.getUserTransaction();
046: } catch (Exception e) {
047: Trace.errln("ERROR: Cannot lookup UserTransaction:\n" + e);
048: System.exit(2);
049: }
050: if (ut == null) {
051: Trace.errln("ERROR: Cannot get UserTransaction");
052: System.exit(2);
053: }
054: if (m_beans == 0) {
055: Trace.outln(trace, "No bean");
056: return;
057: }
058:
059: // Connecting to each Perfs_X Home, and get the Perfs object
060: Perfs t_objPerfs[] = new Perfs[m_beans];
061: for (int i = 1; i <= m_beans; i++) {
062: String beanName = "";
063: switch (m_perfnb) {
064: case 1:
065: beanName = "PerfsHome_";
066: break;
067: case 2:
068: beanName = "Perfs2Home_";
069: break;
070: case 3:
071: beanName = "Perfs3Home_";
072: break;
073: }
074: beanName += i;
075: PerfsHome home = null;
076: try {
077: home = (PerfsHome) PortableRemoteObject.narrow(
078: initialContext.lookup(beanName),
079: PerfsHome.class);
080: } catch (Exception e) {
081: Trace.errln("ERROR: Cannot lookup " + beanName + ":\n"
082: + e);
083: System.exit(2);
084: }
085: Trace.outln(trace, "Connected to the '" + beanName
086: + "' Object");
087:
088: try {
089: t_objPerfs[i - 1] = home.findByCode(m_reqsize / 100);
090: } catch (Exception e) {
091: Trace.errln("ERROR: Cannot find the object "
092: + m_reqsize / 100 + " of " + beanName + ":\n"
093: + e);
094: System.exit(2);
095: }
096: }
097:
098: // Build the message used for the write request
099: String s_writeMsg = new String();
100: String s_date = (new Date().toString());
101: while ((s_writeMsg.length() + s_date.length() + 1) <= m_reqsize) {
102: s_writeMsg = s_writeMsg.concat(s_date);
103: s_writeMsg = s_writeMsg.concat(";");
104: }
105: for (int i = s_writeMsg.length(); i < m_reqsize; i++) {
106: s_writeMsg = s_writeMsg.concat(".");
107: }
108:
109: Trace.outln(trace, "Start loop for " + m_loops
110: + " transactions on " + m_beans + " servers");
111: Trace.outln(trace, "Each transaction does " + m_reads
112: + " read and " + m_writes + " write requests");
113: Trace.outln(trace, "Request size is " + m_reqsize);
114:
115: // Loop on transactions
116: long d_begin = System.currentTimeMillis();
117:
118: for (int l = 0; l < m_loops; l++) {
119: // Begin the transaction
120: if (m_tx == PerfsClient.TX_CLIENT) {
121: try {
122: ut.begin();
123: } catch (Exception e) {
124: Trace.errln("ERROR: Problem during begin:\n" + e);
125: System.exit(2);
126: }
127: }
128:
129: // Requests in the transaction
130: try {
131: for (int r = 0; r < m_reads + m_writes; r++) {
132: if (r < m_reads) {
133: if (m_tx == PerfsClient.TX_CONTAINER) {
134: t_objPerfs[r % m_beans].txread();
135: } else {
136: t_objPerfs[r % m_beans].read();
137: }
138: } else {
139: if (m_tx == PerfsClient.TX_CONTAINER) {
140: t_objPerfs[r % m_beans].txwrite(s_writeMsg);
141: } else {
142: t_objPerfs[r % m_beans].write(s_writeMsg);
143: }
144: }
145: }
146: } catch (Exception e) {
147: Trace
148: .errln("ERROR: Problem during a bussiness method:\n"
149: + e);
150: System.exit(2);
151: }
152:
153: // Commit the transaction
154: if (m_tx == PerfsClient.TX_CLIENT) {
155: try {
156: ut.commit();
157: } catch (Exception e) {
158: Trace
159: .errln("ERROR: Problem during a commit:\n"
160: + e);
161: System.exit(2);
162: }
163: }
164:
165: Trace.outln(trace, "End of loop " + (l + 1) + " OK");
166: }
167:
168: long d_end = System.currentTimeMillis();
169: m_time = (d_end - d_begin) / (m_loops);
170: Trace.outln(trace, "Time per transaction in millisec: "
171: + m_time);
172: }
173:
174: public long getTime() {
175: return m_time;
176: }
177: }
178:
179: public class PerfsClient {
180:
181: static private int trace = Trace.DB_1;
182:
183: static public int TX_CLIENT = 0;
184: static public int TX_CONTAINER = 1;
185: static public int TX_NONE = 2;
186:
187: private static final int m_MAXBEANS = 3;
188: private static final int m_MAXTHREADS = 50;
189: private static final int m_MAXREQSIZE = 800;
190:
191: private static int m_loops = 1;
192: private static int m_poolsize = 1;
193: private static int m_reads = 0;
194: private static int m_writes = 0;
195: private static int m_beans = 0;
196: private static int m_reqsize = 100;
197: private static boolean m_recreate = false;
198: private static boolean m_cmp2 = false;
199: private static boolean m_irc = false;
200: private static int m_tx = TX_CLIENT;
201:
202: private static void usage() {
203: Trace.errln("Usage: java applis.ejb.perfs.PerfsClient <args>");
204: Trace.errln(" -i re-create the data-base tables");
205: Trace.errln(" -2 use beans cmp2");
206: Trace
207: .errln(" -b <loop> number of loops (transactions) per thread");
208: Trace.errln(" -p <thread> number of client threads");
209: Trace
210: .errln(" -r <read> number of read requests per transaction");
211: Trace
212: .errln(" -w <write> number of write requests per transaction");
213: Trace.errln(" -s <size> request data size in bytes");
214: Trace.errln(" -n <beans> number of Perfs beans used");
215: Trace.errln(" -h no tx");
216: Trace.errln(" -c container tx");
217: Trace.errln("default args:");
218: Trace.errln(" -b " + m_loops + " -p " + m_poolsize + " -r "
219: + m_reads + " -w " + m_writes + " -s " + m_reqsize
220: + " -n " + m_beans);
221: }
222:
223: public static void main(String args[]) {
224:
225: Trace.configure();
226:
227: // Get command args
228: for (int argn = 0; argn < args.length; argn++) {
229: String s_arg = args[argn];
230: Integer i_arg;
231: if ((s_arg.equals("-b") == true)) {
232: s_arg = args[++argn];
233: i_arg = java.lang.Integer.valueOf(s_arg);
234: m_loops = i_arg.intValue();
235: } else if ((s_arg.equals("-p") == true)) {
236: s_arg = args[++argn];
237: i_arg = java.lang.Integer.valueOf(s_arg);
238: m_poolsize = i_arg.intValue();
239: } else if ((s_arg.equals("-r") == true)) {
240: s_arg = args[++argn];
241: i_arg = java.lang.Integer.valueOf(s_arg);
242: m_reads = i_arg.intValue();
243: } else if ((s_arg.equals("-w") == true)) {
244: s_arg = args[++argn];
245: i_arg = java.lang.Integer.valueOf(s_arg);
246: m_writes = i_arg.intValue();
247: } else if ((s_arg.equals("-s") == true)) {
248: s_arg = args[++argn];
249: i_arg = java.lang.Integer.valueOf(s_arg);
250: m_reqsize = i_arg.intValue();
251: } else if ((s_arg.equals("-n") == true)) {
252: s_arg = args[++argn];
253: i_arg = java.lang.Integer.valueOf(s_arg);
254: m_beans = i_arg.intValue();
255: } else if (s_arg.equals("-i") == true) {
256: m_recreate = true;
257: } else if (s_arg.equals("-cmp2") == true) {
258: m_cmp2 = true;
259: } else if (s_arg.equals("-irc") == true) {
260: m_irc = true;
261: } else if (s_arg.equals("-h") == true) {
262: m_tx = TX_NONE;
263: } else if (s_arg.equals("-c") == true) {
264: m_tx = TX_CONTAINER;
265: } else if (s_arg.equals("-?") == true) {
266: usage();
267: System.exit(0);
268: } else {
269: usage();
270: System.exit(2);
271: }
272: }
273:
274: // Check command args
275: if (m_loops < 1) {
276: usage();
277: System.exit(2);
278: }
279: if ((m_poolsize < 1) || (m_poolsize > m_MAXTHREADS)) {
280: usage();
281: System.exit(2);
282: }
283: if (m_reads < 0) {
284: usage();
285: System.exit(2);
286: }
287: if (m_writes < 0) {
288: usage();
289: System.exit(2);
290: }
291: if ((m_reqsize < 1) || (m_reqsize > m_MAXREQSIZE)) {
292: usage();
293: System.exit(2);
294: }
295: if ((m_reqsize % 100) != 0) {
296: usage();
297: System.exit(2);
298: }
299: if (m_beans > m_MAXBEANS) {
300: usage();
301: System.exit(2);
302: }
303: if (m_reads + m_writes < m_beans) {
304: Trace
305: .errln("All beans will not be used. You must have r+w>=n");
306: System.exit(2);
307: }
308:
309: Trace.outln(trace, "PerfsClient" + " -b " + m_loops + " -p "
310: + m_poolsize + " -r " + m_reads + " -w " + m_writes
311: + " -s " + m_reqsize + " -n " + m_beans);
312:
313: if (m_recreate) {
314: // Connecting to the DBHome object to create the Perfs_X tables
315: DBHome dbhome = null;
316: DB session = null;
317: String sName = "PerfsDBHome";
318: try {
319: Context initialContext = new InitialContext();
320: dbhome = (DBHome) PortableRemoteObject.narrow(
321: initialContext.lookup(sName), DBHome.class);
322: } catch (Exception e) {
323: Trace.errln("ERROR: Cannot bind to PerfsDBHome:\n" + e);
324: System.exit(2);
325: }
326: Trace.outln(trace, "Connected to the PerfsDBHome Object");
327:
328: try {
329: session = dbhome.create();
330: for (int i = 1; i <= 3; i++) {
331: if (m_cmp2) {
332: session.initTable(i, m_irc);
333: Trace.outln(trace, "Table " + i
334: + " initialized");
335: } else {
336: session.createTable("Perfs_" + i);
337: Trace.outln(trace, "Table Perfs_" + i
338: + " created");
339: }
340: }
341: } catch (Exception e) {
342: Trace
343: .errln("ERROR: Problem during the creation of the tables:\n"
344: + e);
345: System.exit(2);
346: }
347: }
348:
349: // Start pool of client threads
350: PerfsClientThread t_thr[] = new PerfsClientThread[m_MAXTHREADS];
351: long ttime[] = new long[m_MAXTHREADS];
352: for (int p = 0; p < m_poolsize; p++) {
353: int pnb;
354: if (m_cmp2) {
355: if (m_irc) {
356: pnb = 3;
357: } else {
358: pnb = 2;
359: }
360: } else {
361: pnb = 1;
362: }
363: t_thr[p] = new PerfsClientThread(m_loops, m_reads,
364: m_writes, m_reqsize, m_beans, m_tx, pnb);
365: t_thr[p].start();
366: }
367: for (int p = 0; p < m_poolsize; p++) {
368: try {
369: t_thr[p].join();
370: ttime[p] = t_thr[p].getTime();
371:
372: } catch (InterruptedException e) {
373: Trace
374: .errln("ERROR: Problem in PerfsClientThread.join():\n"
375: + e);
376: System.exit(2);
377: }
378: }
379: String txtype = " - no tx";
380: if (m_tx == TX_CONTAINER)
381: txtype = " - container tx";
382: if (m_tx == TX_CLIENT)
383: txtype = " - client tx";
384:
385: Trace.out("PerfsClient " + " -b " + m_loops + " -p "
386: + m_poolsize + " -r " + m_reads + " -w " + m_writes
387: + " -n " + m_beans + " -s " + m_reqsize + txtype);
388: for (int p = 0; p < m_poolsize; p++) {
389: Trace.out(" " + ttime[p]);
390: }
391: Trace.outln("");
392: }
393:
394: }
|