001: // BankClient.java
002:
003: package org.objectweb.clusterDemo;
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: import java.lang.Thread;
011:
012: import org.objectweb.clusterDemo.*;
013:
014: public class BankClient {
015:
016: private static final int m_MAXTHREADS = 100;
017:
018: private static int m_loops = 1;
019:
020: private static int m_clients = 1;
021:
022: private static void usage() {
023: System.out
024: .println("Usage: java org.objectweb.clusterDemo.BankClient <args>");
025: System.out
026: .println(" -l <loop> number of loops per thread");
027: System.out
028: .println(" -c <client> number of client threads");
029: System.out.println("default args:");
030: System.out.println(" -l " + m_loops + " -c " + m_clients);
031: }
032:
033: public static void main(String args[]) {
034:
035: // Get command args
036: for (int argn = 0; argn < args.length; argn++) {
037: String s_arg = args[argn];
038: Integer i_arg;
039: if ((s_arg.equals("-l") == true)) {
040: s_arg = args[++argn];
041: i_arg = java.lang.Integer.valueOf(s_arg);
042: m_loops = i_arg.intValue();
043: } else if ((s_arg.equals("-c") == true)) {
044: s_arg = args[++argn];
045: i_arg = java.lang.Integer.valueOf(s_arg);
046: m_clients = i_arg.intValue();
047: } else if (s_arg.equals("-?") == true) {
048: usage();
049: System.exit(0);
050: } else {
051: usage();
052: System.exit(2);
053: }
054: }
055:
056: // Check command args
057: if (m_loops < 1) {
058: usage();
059: System.exit(2);
060: }
061: if (m_clients < 0) {
062: usage();
063: System.exit(2);
064: }
065:
066: System.out.println("BankClient" + " -b " + m_loops + " -c "
067: + m_clients);
068:
069: // Start pool of client threads
070: BankClientThread t_thr[] = new BankClientThread[m_MAXTHREADS];
071: long ttime[] = new long[m_MAXTHREADS];
072: for (int p = 0; p < m_clients; p++) {
073: t_thr[p] = new BankClientThread(m_loops);
074: t_thr[p].start();
075: }
076: for (int p = 0; p < m_clients; p++) {
077: try {
078: t_thr[p].join();
079: ttime[p] = t_thr[p].getTime();
080:
081: } catch (InterruptedException e) {
082: System.out
083: .println("ERROR: Problem in BankClientThread.join():\n"
084: + e);
085: System.exit(2);
086: }
087: }
088:
089: System.out.println("BankClient " + " -l " + m_loops + " -c "
090: + m_clients);
091: for (int p = 0; p < m_clients; p++) {
092: System.out.println(" " + ttime[p]);
093: }
094: System.out.println("");
095: }
096:
097: }
098:
099: class BankClientThread extends Thread {
100:
101: private int m_loops;
102:
103: private long m_time;
104:
105: private Integer m_accountId;
106:
107: public BankClientThread(int b) {
108: m_loops = b;
109: }
110:
111: public void run() {
112:
113: System.out.println("Running a BankClientThread...");
114: BankSessionHome bsh;
115: BankSession bs = null;
116:
117: Context initialContext = null;
118: try {
119: initialContext = new InitialContext();
120: bsh = (BankSessionHome) initialContext
121: .lookup("BankEJBBankSessionHome");
122: bs = bsh.create();
123: } catch (Exception e) {
124: System.out.println("ERROR: Problem initializing :\n" + e);
125: System.exit(2);
126: }
127:
128: // Create a client account
129: String clientName = "Client" + System.currentTimeMillis();
130: try {
131: m_accountId = bs.createAccount(clientName, 1000);
132: } catch (Exception e) {
133: System.out.println("ERROR: Problem creating Account :\n"
134: + e);
135: System.exit(2);
136: }
137:
138: // Loop
139: long d_begin = System.currentTimeMillis();
140:
141: System.out.println("Start loop");
142: System.out
143: .println("Each loop does 1 debit and 1 credit operations");
144:
145: for (int l = 0; l < m_loops; l++) {
146:
147: // Do one debit and one credit
148: try {
149: bs.debit(m_accountId, clientName, 100, "Retrait ATM");
150: //sleep(1000);
151: bs.credit(m_accountId, clientName, 100, "Depot ATM");
152: //sleep(1000);
153:
154: } catch (Exception e) {
155: System.out
156: .println("ERROR: Problem during debit / credit phase :\n"
157: + e);
158: System.exit(2);
159: }
160:
161: System.out.println("End of loop " + (l + 1) + " OK");
162: }
163:
164: long d_end = System.currentTimeMillis();
165: m_time = (d_end - d_begin) / (m_loops);
166: //m_time = m_time - 2000;
167: System.out.println("Time per transaction in millisec: "
168: + m_time);
169: }
170:
171: public long getTime() {
172: return m_time;
173: }
174: }
|