KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > harness > EJBTestHarness


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.harness;
23
24 import EDU.oswego.cs.dl.util.concurrent.CopyOnWriteArrayList;
25
26 /**
27  * @author <a HREF="mailto:tom@jboss.org">Tom Elrod</a>
28  */

29 public class EJBTestHarness
30 {
31    public static void main(String JavaDoc[] args)
32    {
33       int iterations = 10;
34       int numOfClients = 10;
35       boolean printLookupTime = false;
36       String JavaDoc jndiURL = "";
37       if (args.length > 0)
38       {
39          iterations = Integer.parseInt(args[0]);
40       }
41       if (args.length > 1)
42       {
43          numOfClients = Integer.parseInt(args[1]);
44       }
45       if (args.length > 2)
46       {
47          printLookupTime = Boolean.valueOf(args[2]).booleanValue();
48       }
49       if (args.length > 3)
50       {
51          jndiURL = args[3];
52       }
53
54       EJBTestHarness test = new EJBTestHarness();
55       test.runTest(iterations, numOfClients, jndiURL, printLookupTime);
56    }
57
58    private void runTest(final int iterations, int numOfClients, final String JavaDoc jndiURL, final boolean printTime)
59    {
60       final CopyOnWriteArrayList timesList = new CopyOnWriteArrayList();
61       final Counter counter = new Counter();
62       long start = System.currentTimeMillis();
63
64       System.out.println("Starting run now with " + numOfClients + " clients and " + iterations + " iterations.");
65
66       for (int x = 0; x < numOfClients; x++)
67       {
68          new Thread JavaDoc()
69          {
70             public void run()
71             {
72                try
73                {
74                   for (int i = 0; i < iterations; i++)
75                   {
76                      long startTime = System.currentTimeMillis();
77                      EJBTestClient client = new EJBTestClient();
78                      client.execute(jndiURL);
79                      long endTime = System.currentTimeMillis();
80                      timesList.add(new Long JavaDoc(endTime - startTime));
81                      if (printTime)
82                         System.out.println(endTime - startTime);
83                      counter.increment();
84                      long value = counter.getValue();
85                      if (value % 10000 == 0)
86                      {
87                         System.out.println("Executed " + value + " times so far.");
88                      }
89
90                   }
91                }
92                catch (Exception JavaDoc e)
93                {
94                   System.out.println("Error calling test client to do jndi lookup");
95                   e.printStackTrace();
96                }
97             }
98          }.start();
99       }
100
101
102       while (timesList.size() < (iterations * numOfClients))
103       {
104          try
105          {
106             Thread.sleep(1000);
107          }
108          catch (InterruptedException JavaDoc e)
109          {
110             e.printStackTrace(); //TODO: -TME Implement
111
}
112       }
113
114       long end = System.currentTimeMillis();
115
116       System.out.println("Ran test with total of " + timesList.size() + " (iterations: " + iterations +
117                          ", number of clients: " + numOfClients + ") in " + (end - start) / 1000 + " seconds.");
118
119       long total = 0;
120       for (int i = 0; i < timesList.size(); i++)
121       {
122          total += ((Long JavaDoc) timesList.get(i)).longValue();
123       }
124
125       System.out.println("Average time to make lookup is " + (total / (iterations * numOfClients)) + " milliseconds.");
126
127       //System.out.println("Counter says was executed " + counter.getValue() + " times.");
128

129    }
130
131    public class Counter
132    {
133       private long count = 0;
134
135       public void increment()
136       {
137          count++;
138       }
139
140       public void decrement()
141       {
142          count--;
143       }
144
145       public long getValue()
146       {
147          return count;
148       }
149    }
150
151 }
152
Popular Tags