1 package org.apache.ojb.performance; 2 3 17 18 19 24 class PerfRunner 25 { 26 private final String PREFIX_LOG = "[" + this.getClass().getName() + "] "; 27 28 37 private long[] testTimes; 38 private ThreadGroup threadGroup; 39 private PerfMain perfMain; 40 private long perfTestId; 41 private boolean checked; 42 45 private Thread threads[] = null; 46 private Class testClass; 47 private PerfTest test; 48 49 50 public PerfRunner(Class perfTestClass) 51 { 52 this.perfTestId = System.currentTimeMillis(); 53 this.checked = false; 54 this.testClass = perfTestClass; 55 this.test = createTest(); 57 this.threadGroup = new ThreadGroup (testName() + "_Group"); 58 } 59 60 private PerfTest createTest() 61 { 62 try 63 { 64 PerfTest result = (PerfTest) testClass.newInstance(); 65 result.setPerfRunner(this); 66 return result; 67 } 68 catch(Exception e) 69 { 70 e.printStackTrace(); 71 throw new RuntimeException ("Can't create test instance: " + e.getMessage()); 72 } 73 } 74 75 78 public String testName() 79 { 80 return test.testName(); 81 } 82 83 private void checkApi() throws Exception 84 { 85 String name = testName() + "_Pre_Test_Object"; 86 PerfArticle article = test.getPreparedPerfArticle(name); 87 PerfArticle[] arr = new PerfArticle[]{article}; 88 test.insertNewArticles(arr); 89 test.readArticlesByCursor(name); 90 test.updateArticles(arr); 91 test.deleteArticles(arr); 92 checked = true; 93 } 94 95 98 protected void interruptThreads() 99 { 100 if (threads != null) 101 { 102 for (int i = 0; i < threads.length; i++) 103 { 104 threads[i].interrupt(); 105 } 106 } 107 PerfMain.printer().println("## Test failed! ##"); 108 PerfMain.printer().println("## Test failed! ##"); 109 } 110 111 114 protected void runTestHandles(final PerfTest[] runnables) 115 { 116 if (runnables == null) 117 { 118 throw new IllegalArgumentException ("runnables is null"); 119 } 120 threads = new Thread [runnables.length]; 121 for (int i = 0; i < threads.length; i++) 122 { 123 threads[i] = new Thread (threadGroup, runnables[i]); 124 } 125 for (int i = 0; i < threads.length; i++) 126 { 127 threads[i].start(); 128 } 129 try 130 { 131 for (int i = 0; i < threads.length; i++) 132 { 133 threads[i].join(); 134 } 135 } 136 catch (InterruptedException ignore) 137 { 138 PerfMain.printer().println(PREFIX_LOG + "Thread join interrupted."); 139 } 140 141 while(threadGroup.activeCount() > 0) 143 { 144 PerfMain.printer().println("## active threads: " + threadGroup.activeCount()); 145 } 146 147 threads = null; 148 } 149 150 public void performTest() 151 { 152 try 153 { 154 test.init(); 156 157 if (!checked) 158 { 159 checkApi(); 160 } 162 163 int objectCount; 164 int objectCountAfter; 165 166 testTimes = new long[7]; 167 168 objectCount = test.articleCount(); 169 170 PerfTest[] perfHandles = new PerfTest[PerfMain.getConcurrentThreads()]; 172 for (int i = 0; i < PerfMain.getConcurrentThreads(); i++) 173 { 174 perfHandles[i] = createTest(); 175 } 176 runTestHandles(perfHandles); 177 178 objectCountAfter = test.articleCount(); 180 perfMain.addPeriodResult(testName(), testTimes); 181 perfMain.addConsistentResult(testName(), objectCount, objectCountAfter); 182 183 test.tearDown(); 185 } 186 catch (Exception e) 187 { 188 e.printStackTrace(); 189 perfMain.registerException(PREFIX_LOG, e); 190 } 191 } 192 193 public void registerException(String causer, Exception e) 194 { 195 perfMain.registerException(causer, e); 196 } 197 198 public synchronized void addTime(short position, long time) 199 { 200 testTimes[position] += time; 201 } 202 203 public void registerPerfMain(PerfMain aPerfMain) 204 { 205 this.perfMain = aPerfMain; 206 } 207 208 public ThreadGroup getThreadGroup() 209 { 210 return threadGroup; 211 } 212 213 public long getPerfTestId() 214 { 215 return perfTestId; 216 } 217 } 218 | Popular Tags |