1 package org.apache.ojb.ejb.pb; 2 3 17 18 import javax.ejb.EJBHome ; 19 import javax.naming.Context ; 20 import javax.rmi.PortableRemoteObject ; 21 import java.math.BigDecimal ; 22 import java.util.ArrayList ; 23 import java.util.Iterator ; 24 import java.util.List ; 25 26 import org.apache.commons.lang.SystemUtils; 27 import org.apache.ojb.broker.OJBRuntimeException; 28 import org.apache.ojb.ejb.ArticleVO; 29 import org.apache.ojb.ejb.ContextHelper; 30 31 37 public class StressTest 38 { 39 private static int iterationsPerThread = 50; 40 private static int concurrentThreads = 2; 41 private PBSessionRemote bean; 42 private static boolean testClientPass = true; 43 44 50 private long[] times; 51 52 55 private Thread threads[] = null; 56 57 public StressTest() 58 { 59 } 60 61 public static void performTest(int[] args) throws Exception 62 { 63 if (args.length > 0) 64 { 65 concurrentThreads = args[0]; 66 } 67 if (args.length > 1) 68 { 69 iterationsPerThread = args[1]; 70 } 71 72 StressTest test = new StressTest(); 73 int objectCount = 0; 74 int objectCountAfter = 0; 75 test.init(); 76 objectCount = test.getArticleCount(); 77 test.runMultithreaded(); 78 79 System.out.println("Test-Info: Objects in DB before PB test: " + objectCount); 80 objectCountAfter = test.getArticleCount(); 81 System.out.println("Test-Info: Objects in DB after PB test: " + objectCountAfter); 82 System.out.println("Test-Info: Stress test was successful? - " + (objectCount == objectCountAfter && testClientPass) + " -"); 83 if(!testClientPass) throw new RuntimeException ("Test does not pass"); 84 } 85 86 89 private void init() throws Exception 90 { 91 Context ctx = ContextHelper.getContext(); 92 times = new long[4]; 93 try 94 { 95 Object object = PortableRemoteObject.narrow(ctx.lookup(PBSessionHome.JNDI_NAME), EJBHome .class); 96 bean = ((PBSessionHome) object).create(); 97 } 98 catch(Exception e) 99 { 100 e.printStackTrace(System.err); 101 throw e; 102 } 103 } 104 105 private int getArticleCount() throws Exception 106 { 107 try 108 { 109 return bean.getArticleCount(); 110 } 111 catch(java.rmi.RemoteException e) 112 { 113 e.printStackTrace(); 114 throw e; 115 } 116 } 117 118 private void runMultithreaded() throws Exception 119 { 120 String sep = SystemUtils.LINE_SEPARATOR; 121 122 System.out.println(sep + sep + "++ Start thread generation for PB api test ++"); 123 System.out.println("Begin with performance test, " + concurrentThreads + 124 " concurrent threads, handle " + iterationsPerThread + " articles per thread"); 125 PBTestClient[] clientsPB = new PBTestClient[concurrentThreads]; 126 for (int i = 0; i < concurrentThreads; i++) 127 { 128 PBTestClient obj = new PBTestClient(this); 129 clientsPB[i] = obj; 130 } 131 System.out.println(""); 132 times[0] = System.currentTimeMillis(); 133 runTestClients(clientsPB); 134 times[0] = (System.currentTimeMillis() - times[0]); 135 System.out.println(buildTestSummary("PB API")); 136 System.out.println("++ End of performance test PB api ++" + sep + sep); 137 } 138 139 142 synchronized void interruptThreads() 143 { 144 testClientPass = false; 145 if (threads != null) 146 { 147 for (int i = 0; i < threads.length; i++) 148 { 149 threads[i].interrupt(); 150 } 151 } 152 System.err.println("## Test failed! ##"); 153 System.err.println("## Test failed! ##"); 154 } 155 156 159 void runTestClients(final TestClient[] runnables) throws Exception 160 { 161 if (runnables == null) 162 { 163 throw new IllegalArgumentException ("runnables is null"); 164 } 165 threads = new Thread [runnables.length]; 166 for (int i = 0; i < threads.length; i++) 167 { 168 threads[i] = new Thread (runnables[i]); 169 } 170 for (int i = 0; i < threads.length; i++) 171 { 172 threads[i].start(); 173 threads[i].join(); 174 } 175 threads = null; 176 } 177 178 synchronized void addTime(int position, long time) 179 { 180 times[position] = times[position] + time; 181 } 182 183 private String buildTestSummary(String key) 184 { 185 String sep = System.getProperty("line.separator"); 186 StringBuffer buf = new StringBuffer (); 187 buf.append(sep); 188 buf.append("----------------------------------------------------"); 189 buf.append(sep); 190 buf.append("TEST SUMMARY - " + key); 191 buf.append(sep); 192 buf.append(concurrentThreads + " concurrent threads, handle " + iterationsPerThread + " articles per thread"); 193 buf.append(sep); 194 buf.append("Test period: " + (((double) times[0]) / 1000) + " [sec]"); 195 buf.append(sep); 196 buf.append("Inserting period: " + times[1] + " [msec]"); 197 buf.append(sep); 198 buf.append("Fetching period: " + times[2] + " [msec]"); 199 buf.append(sep); 200 buf.append("Deleting period: " + times[3] + " [msec]"); 201 buf.append(sep); 202 buf.append("----------------------------------------------------"); 203 204 return buf.toString(); 205 } 206 207 208 209 213 static abstract class TestClient implements Runnable 214 { 215 216 } 217 218 221 static class PBTestClient extends TestClient 222 { 223 private List articlesList; 224 private PBSessionRemote bean; 225 private String articleName; 226 private StressTest test; 227 228 public PBTestClient(StressTest arg) 229 { 230 test = arg; 231 init(); 232 articlesList = new ArrayList (); 233 articleName = "PBTestClient_" + System.currentTimeMillis(); 234 for(int i = 0; i < iterationsPerThread; i++) 235 { 236 articlesList.add(createArticle(articleName)); 237 } 238 } 239 240 protected void init() 241 { 242 Context ctx = ContextHelper.getContext(); 243 try 244 { 245 Object object = PortableRemoteObject.narrow(ctx.lookup(PBSessionHome.JNDI_NAME), EJBHome .class); 246 bean = ((PBSessionHome) object).create(); 247 } 248 catch(Exception e) 249 { 250 e.printStackTrace(); 251 throw new OJBRuntimeException("Can't lookup bean: " + PBSessionHome.JNDI_NAME, e); 252 } 253 } 254 255 public void run() 256 { 257 try 259 { 260 insertNewArticles(); 261 readAllArticles(); 262 deleteArticles(); 263 } 264 catch(Throwable e) 265 { 266 System.err.println("Error in client: " + e.getMessage()); 267 test.interruptThreads(); 268 throw new OJBRuntimeException("[" + PBTestClient.class.getName() 269 + "] Stress test client cause exception, thread was " + Thread.currentThread(), e); 270 } 271 } 272 273 277 private ArticleVO createArticle(String articleName) 278 { 279 ArticleVO a = new ArticleVO(); 280 a.setName(articleName); 281 a.setPrice(new BigDecimal (0.45 * articleName.hashCode())); 282 a.setDescription("description " + articleName.hashCode()); 283 return a; 284 } 285 286 protected void deleteArticles() throws Exception 287 { 288 long start = System.currentTimeMillis(); 289 290 bean.deleteObjects(articlesList); 291 292 long stop = System.currentTimeMillis(); 293 test.addTime(3, stop - start); 294 } 295 296 protected void insertNewArticles() throws Exception 297 { 298 long start = System.currentTimeMillis(); 299 300 articlesList = bean.storeObjects(articlesList); 301 302 long stop = System.currentTimeMillis(); 303 test.addTime(1, stop - start); 304 } 305 306 protected void readAllArticles() throws Exception 307 { 308 long start = System.currentTimeMillis(); 309 310 Iterator it = bean.getArticlesByName(articleName).iterator(); 311 while(it.hasNext()) it.next(); 312 313 long stop = System.currentTimeMillis(); 314 test.addTime(2, stop - start); 315 } 316 } 317 } 318 | Popular Tags |