KickJava   Java API By Example, From Geeks To Geeks.

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


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 import javax.naming.InitialContext JavaDoc;
27 import javax.naming.NameAlreadyBoundException JavaDoc;
28 import java.io.InputStream JavaDoc;
29 import java.util.Properties JavaDoc;
30
31 /**
32  * This class performs repetitive lookups of an object that's bound
33  * using the same naming provider (e.g., if run against port 1099, it
34  * will bind and lookup using local JNDI). It allows the user to compare
35  * lookiup performance using the local naming server vs. using HA-JNDI
36  * where the object will be found and RPC calls aren't necessary.
37  *
38  * @author <a HREF="mailto:tom@jboss.org">Tom Elrod</a>
39  * @author <a HREF="mailto:jgauthier@novell.com">Jerry Gauthier</a>
40  * @version $Revision: 37406 $
41  */

42 public class HAJNDITestHarness
43 {
44    public static String JavaDoc SIMPLE_CONFIG_SERVER_PROP = "services.properties";
45    private static String JavaDoc NODE1 = "ha_node1";
46    private static String JavaDoc NODE2 = NODE1 + "/" +"ha_node2";
47    private static String JavaDoc NODE3 = NODE2 + "/" + "ha_node3";
48    private static String JavaDoc BINDKEY = NODE3 + "/" + "ha_bind_key";
49    
50    public static void main(String JavaDoc[] args)
51    {
52       int iterations = 10;
53       int numOfClients = 10;
54       boolean printLookupTime = false;
55       
56       if (args.length > 0)
57       {
58          iterations = Integer.parseInt(args[0]);
59       }
60       if (args.length > 1)
61       {
62          numOfClients = Integer.parseInt(args[1]);
63       }
64       if (args.length > 2)
65       {
66          printLookupTime = Boolean.valueOf(args[2]).booleanValue();
67       }
68
69       HAJNDITestHarness test = new HAJNDITestHarness();
70       try
71       {
72          System.out.println("HAJNDITestHarness properties");
73          Properties JavaDoc props = test.getPropAsResource(SIMPLE_CONFIG_SERVER_PROP);
74          props.list(System.out);
75          System.out.println("getting initial context");
76          InitialContext JavaDoc ctx = new InitialContext JavaDoc(props);
77
78          try
79          {
80             ctx.createSubcontext(NODE1);
81          }
82          catch (NameAlreadyBoundException JavaDoc e)
83          {
84             // do nothing
85
}
86
87          try
88          {
89             ctx.createSubcontext(NODE2);
90          }
91          catch (NameAlreadyBoundException JavaDoc e)
92          {
93             // do nothing
94
}
95
96          try
97          {
98             ctx.createSubcontext(NODE3);
99          }
100          catch (NameAlreadyBoundException JavaDoc e)
101          {
102             // do nothing
103
}
104
105          try
106          {
107             HABindingObject bindValue = new HABindingObject();
108             bindValue.setName("John Smith");
109             bindValue.setCompany("Acme Corporation");
110             bindValue.setLocation("New York, NY");
111             bindValue.setCountry("US");
112             bindValue.setYear(1934);
113             bindValue.setManager(false);
114             
115             ctx.bind(BINDKEY, bindValue);
116          }
117          catch (NameAlreadyBoundException JavaDoc e)
118          {
119             // do nothing
120
}
121          test.runTest(iterations, numOfClients, printLookupTime);
122       }
123       catch (Exception JavaDoc e)
124       {
125          System.out.println(e);
126       }
127    }
128
129    private void runTest(final int iterations, int numOfClients, final boolean printTime)
130    {
131       final CopyOnWriteArrayList timesList = new CopyOnWriteArrayList();
132       final Counter counter = new Counter();
133       long start = System.currentTimeMillis();
134
135       System.out.println("Starting run now with " + numOfClients + " clients and " + iterations + " iterations.");
136
137       for (int x = 0; x < numOfClients; x++)
138       {
139          new Thread JavaDoc()
140          {
141             public void run()
142             {
143                try
144                {
145                   for (int i = 0; i < iterations; i++)
146                   {
147                      long startTime = System.currentTimeMillis();
148                      HATestClient client = new HATestClient();
149                      Object JavaDoc o = client.lookup(BINDKEY, HABindingObject.class);
150                      long endTime = System.currentTimeMillis();
151                      timesList.add(new Long JavaDoc(endTime - startTime));
152                      if (printTime)
153                         System.out.println(endTime - startTime);
154                      counter.increment();
155                      long value = counter.getValue();
156                      if (value % 10000 == 0)
157                      {
158                         System.out.println("Executed " + value + " times so far.");
159                      }
160                   }
161                }
162                catch (Exception JavaDoc e)
163                {
164                   System.out.println("Error calling test client to do hajndi_binding lookup");
165                   e.printStackTrace();
166                }
167             }
168          }.start();
169       }
170
171       while (timesList.size() < (iterations * numOfClients))
172       {
173          try
174          {
175             Thread.sleep(1000);
176          }
177          catch (InterruptedException JavaDoc e)
178          {
179             e.printStackTrace(); //TODO: -TME Implement
180
}
181       }
182
183       long end = System.currentTimeMillis();
184
185       System.out.println("Ran test with total of " + timesList.size() + " (iterations: " + iterations +
186                          ", number of clients: " + numOfClients + ") in " + (end - start) / 1000 + " seconds.");
187
188       long total = 0;
189       for (int i = 0; i < timesList.size(); i++)
190       {
191          total += ((Long JavaDoc) timesList.get(i)).longValue();
192       }
193
194       System.out.println("Average time to make lookup is " + (total / (iterations * numOfClients)) + " milliseconds.");
195    }
196    
197    private Properties JavaDoc getPropAsResource(String JavaDoc name) throws Exception JavaDoc
198    {
199       InputStream JavaDoc is = getClass().getResourceAsStream("/META-INF/" + name);
200       if (is == null)
201       {
202          throw new Exception JavaDoc("Unable to locate resource: " + name);
203       }
204       Properties JavaDoc confProp = new Properties JavaDoc();
205       confProp.load(is);
206       return confProp;
207    }
208
209    public class Counter
210    {
211       private long count = 0;
212
213       public void increment()
214       {
215          count++;
216       }
217
218       public void decrement()
219       {
220          count--;
221       }
222
223       public long getValue()
224       {
225          return count;
226       }
227    }
228   
229 }
230
Popular Tags