1 23 24 package org.enhydra.xml.xmlc.perf; 25 26 import java.io.FileNotFoundException ; 27 import java.io.FileOutputStream ; 28 import java.io.PrintStream ; 29 import java.math.BigDecimal ; 30 31 import org.enhydra.xml.xmlc.XMLCFactory; 32 import org.enhydra.xml.xmlc.XMLCStdFactory; 33 import org.enhydra.xml.xmlc.XMLObject; 34 35 39 public class PerfRunner { 40 43 private static final int TEST_COUNT = 6; private static final int REPEAT_COUNT = 10; private static final String TEST_RESULTS_FILE = "perf.out"; 46 private static final String NOOP_EDITOR = 47 "tests.org.enhydra.xml.xmlc.perf.NoOpEditor"; 48 49 class TimeResult { 50 private double createTime; 52 private double toDocumentTime; 53 private double editTime; 54 private int iterations; 55 private int editedNodeCount; 56 TimeResult(long createTime, long editTime, 57 long toDocumentTime, int iterations, 58 int editedNodeCount) { 59 this.createTime = createTime; 60 this.editTime = editTime; 61 this.toDocumentTime = toDocumentTime; 62 this.iterations = iterations; 63 this.editedNodeCount = editedNodeCount; 64 } 65 double getTotalTime() { 66 return getCreateTime() + getToDocumentTime() + getEditTime(); 67 } 68 Number getTotalTime(int scale) { 69 return getScaledNumber(getTotalTime(), scale); 70 } 71 Number getAverageTime(int scale) { 72 return getScaledNumber(getTotalTime()/iterations, scale); 73 } 74 double getCreateTime() { 75 return createTime/1000.0; 76 } 77 Number getAverageCreateTime(int scale) { 78 return getScaledNumber(getCreateTime()/iterations, scale); 79 } 80 double getToDocumentTime() { 81 return toDocumentTime/1000.0; 82 } 83 Number getAverageToDocumentTime(int scale) { 84 return getScaledNumber(getToDocumentTime()/iterations, scale); 85 } 86 double getEditTime() { 87 return editTime/1000.0; 88 } 89 Number getAverageEditTime(int scale) { 90 return getScaledNumber(getEditTime()/iterations, scale); 91 } 92 int getEditedNodeCount() { 93 return editedNodeCount/iterations; 94 } 95 } 96 97 100 Number getScaledNumber(double num, int scale) 101 throws NumberFormatException { 102 BigDecimal bd = new BigDecimal (num); 103 return bd.setScale(scale, BigDecimal.ROUND_HALF_UP); 104 } 105 106 109 Number calcRate(double num, int scale) 110 throws NumberFormatException { 111 if (num == 0) { 112 return new BigDecimal (-1); 113 } 114 BigDecimal bd = new BigDecimal (1.0/num); 115 return bd.setScale(scale, BigDecimal.ROUND_HALF_UP); 116 } 117 118 124 static DOMEditor loadEditor(String className) 125 throws ClassNotFoundException , InstantiationException , IllegalAccessException { 126 return (DOMEditor)Class.forName(className).newInstance(); 127 } 128 129 133 private TimeResult runTestIterations(Class docClass, DOMEditor editor, 134 int repeatCount) 135 throws Exception { 136 XMLCFactory factory = new XMLCStdFactory(null, null); 137 long checkPoint; 138 long deltaCreate = 0, deltaEdit = 0, deltaToDocument = 0; 139 int editedNodeCount = 0; 140 141 for (int cnt = 0; cnt < repeatCount; cnt++) { 142 143 checkPoint = System.currentTimeMillis(); 144 XMLObject xmlcObj = factory.create(docClass); 145 deltaCreate 146 += (System.currentTimeMillis() - checkPoint); 147 148 checkPoint = System.currentTimeMillis(); 149 editedNodeCount += editor.edit(xmlcObj); 150 deltaEdit 151 += (System.currentTimeMillis() - checkPoint); 152 153 checkPoint = System.currentTimeMillis(); 154 String docStr = xmlcObj.toDocument(); 155 deltaToDocument 156 += (System.currentTimeMillis() - checkPoint); 157 } 158 return new TimeResult(deltaCreate, deltaEdit, deltaToDocument, 159 repeatCount, editedNodeCount); 160 } 161 162 165 private void runTest(String docClassName, 166 int testCount, 167 int repeatCount, 168 DOMEditor editor, 169 PrintStream out) 170 throws Exception { 171 do { 172 Class docClass = Class.forName(docClassName); 173 TimeResult timeSec[] = new TimeResult[testCount]; 174 175 for (int cnt = 0; cnt < testCount; cnt++) { 176 timeSec[cnt] = runTestIterations(docClass, editor, repeatCount); 177 } 178 179 if (out != null) { 180 out.println("**Test class = " + docClass.getName()); 181 out.println("**Test editor = " + editor); 182 out.println("**Test = " + TEST_COUNT 183 + ", Repeat = " + REPEAT_COUNT); 184 out.println(); 185 double totalSec = 0; 186 double totalCreateSec = 0; 187 double totalEditSec = 0; 188 double totalToDocumentSec = 0; 189 190 for (int cnt = 0; cnt < testCount; cnt++) { 191 out.println("Pass " + cnt + ":\t" 192 + timeSec[cnt].getAverageTime(4) 193 + " sec/iteration " 194 + "(create=" 195 + timeSec[cnt].getAverageCreateTime(4) 196 + ", edit=" 197 + timeSec[cnt].getAverageEditTime(5) 198 + " (" + timeSec[cnt].getEditedNodeCount() 199 + " nodes)" 200 + ", toDocument=" 201 + timeSec[cnt].getAverageToDocumentTime(4) 202 + ")"); 203 totalSec += timeSec[cnt].getTotalTime(); 204 totalCreateSec += timeSec[cnt].getCreateTime(); 205 totalEditSec += timeSec[cnt].getEditTime(); 206 totalToDocumentSec += timeSec[cnt].getToDocumentTime(); 207 } 208 209 double totalAveTimeSec = 210 totalSec/(repeatCount*testCount); 211 double totalAveCreateSec = 212 totalCreateSec/(repeatCount*testCount); 213 double totalAveEditSec = 214 totalEditSec/(repeatCount*testCount); 215 double totalAveToDocumentSec = 216 totalToDocumentSec/(repeatCount*testCount); 217 out.println(); 218 out.println("Average: " 219 + getScaledNumber(totalAveTimeSec, 4) 220 + " sec/iteration " 221 + "(create=" 222 + getScaledNumber(totalAveCreateSec, 4) 223 + ", edit=" 224 + getScaledNumber(totalAveEditSec, 4) 225 + ", toDocument=" 226 + getScaledNumber(totalAveToDocumentSec, 4) 227 + ")"); 228 229 out.println("Rate: " 230 + calcRate(totalAveTimeSec, 2) 231 + " iterations/sec " 232 + "(create=" 233 + calcRate(totalAveCreateSec, 2) 234 + ", edit=" 235 + calcRate(totalAveEditSec, 2) 236 + ", toDocument=" 237 + calcRate(totalAveToDocumentSec, 2) 238 + ")"); 239 240 out.println(); 241 out.println(); 242 } 243 } while (editor.next()); 244 } 245 246 public static void main(String [] args) throws Exception { 247 PrintStream out = System.out; 248 249 if (args.length < 1) { 250 System.err.println("wrong # args: PerfRunner class [editors]"); 251 System.exit(1); 252 } 253 254 try { 255 FileOutputStream fout = new FileOutputStream (TEST_RESULTS_FILE, true); 256 out = new PrintStream (fout); 257 } catch (SecurityException e) { 258 System.err.println(TEST_RESULTS_FILE + ": permission denied: " 259 + e.getMessage()); 260 } catch (FileNotFoundException e) { 261 System.err.println(TEST_RESULTS_FILE + ": file not found: " 262 + e.getMessage()); 263 } 264 265 PerfRunner tests = new PerfRunner(); 266 267 tests.runTest(args[0], TEST_COUNT, 269 REPEAT_COUNT, 270 loadEditor(NOOP_EDITOR), 271 null); 272 273 if (args.length == 1) { 274 tests.runTest(args[0], TEST_COUNT, 276 REPEAT_COUNT, 277 loadEditor(NOOP_EDITOR), 278 out); 279 } else { 280 for (int i=1; i<args.length; i++) { 282 tests.runTest(args[0], TEST_COUNT, 283 REPEAT_COUNT, 284 loadEditor(args[i]), 285 out); 286 } 287 } 288 } 289 } 290 291 292 293 294 295 296 | Popular Tags |