KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.io.InputStream JavaDoc;
25 import java.util.Properties JavaDoc;
26
27 import EDU.oswego.cs.dl.util.concurrent.CopyOnWriteArrayList;
28 import com.cluster.simple.sessionbeans.SimpleSessionHome;
29
30 /**
31  * This class performs repetitive lookups of an EJB that's bound
32  * during deployment. It allows the user to compare EJB lookup
33  * performance using the local naming server vs. using HA-JNDI.
34  *
35  * @author <a HREF="mailto:tom@jboss.org">Tom Elrod</a>
36  * @version $Revision: 37406 $
37  */

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

145    }
146    
147    public Properties JavaDoc getPropAsResource(String JavaDoc name) throws Exception JavaDoc
148    {
149       InputStream JavaDoc is = getClass().getResourceAsStream("/META-INF/" + name);
150       if (is == null)
151       {
152          throw new Exception JavaDoc("Unable to locate resource: " + name);
153       }
154       Properties JavaDoc confProp = new Properties JavaDoc();
155       confProp.load(is);
156       return confProp;
157    }
158
159    public class Counter
160    {
161       private long count = 0;
162
163       public void increment()
164       {
165          count++;
166       }
167
168       public void decrement()
169       {
170          count--;
171       }
172
173       public long getValue()
174       {
175          return count;
176       }
177    }
178
179 }
180
Popular Tags