1 22 package org.jboss.harness; 23 24 import EDU.oswego.cs.dl.util.concurrent.CopyOnWriteArrayList; 25 26 import javax.naming.InitialContext ; 27 import javax.naming.NameAlreadyBoundException ; 28 import java.io.InputStream ; 29 import java.util.Properties ; 30 31 42 public class HAJNDITestHarness 43 { 44 public static String SIMPLE_CONFIG_SERVER_PROP = "services.properties"; 45 private static String NODE1 = "ha_node1"; 46 private static String NODE2 = NODE1 + "/" +"ha_node2"; 47 private static String NODE3 = NODE2 + "/" + "ha_node3"; 48 private static String BINDKEY = NODE3 + "/" + "ha_bind_key"; 49 50 public static void main(String [] 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 props = test.getPropAsResource(SIMPLE_CONFIG_SERVER_PROP); 74 props.list(System.out); 75 System.out.println("getting initial context"); 76 InitialContext ctx = new InitialContext (props); 77 78 try 79 { 80 ctx.createSubcontext(NODE1); 81 } 82 catch (NameAlreadyBoundException e) 83 { 84 } 86 87 try 88 { 89 ctx.createSubcontext(NODE2); 90 } 91 catch (NameAlreadyBoundException e) 92 { 93 } 95 96 try 97 { 98 ctx.createSubcontext(NODE3); 99 } 100 catch (NameAlreadyBoundException e) 101 { 102 } 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 e) 118 { 119 } 121 test.runTest(iterations, numOfClients, printLookupTime); 122 } 123 catch (Exception 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 () 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 o = client.lookup(BINDKEY, HABindingObject.class); 150 long endTime = System.currentTimeMillis(); 151 timesList.add(new Long (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 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 e) 178 { 179 e.printStackTrace(); } 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 ) 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 getPropAsResource(String name) throws Exception 198 { 199 InputStream is = getClass().getResourceAsStream("/META-INF/" + name); 200 if (is == null) 201 { 202 throw new Exception ("Unable to locate resource: " + name); 203 } 204 Properties confProp = new Properties (); 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 |