1 package test.perf; 2 3 import test.perf.test1.*; 4 5 import java.util.*; 6 import java.net.URL ; 7 import java.io.*; 8 9 14 public class PerformanceTest { 15 private AbstractTest[] tests; 16 17 public static void main(String [] args) { 18 PerformanceTest pt = new PerformanceTest(); 19 pt.start(); 20 } 21 22 public PerformanceTest() { 23 tests = getTests(); 24 } 25 26 public void start() { 27 try { 28 for (int i = 0; i < tests.length; i++) { 29 tests[i].init(); 30 31 long l1 = System.currentTimeMillis(); 32 tests[i].test(tests[i].getLoopCount()); 33 long l2 = System.currentTimeMillis(); 34 35 String cName = tests[i].getClass().getName(); 36 String tName = cName.substring(cName.lastIndexOf('.') + 1); 37 System.out.println("It took " + (l2 - l1) + 38 " ms to execute "+ tName); 39 } 40 } 41 catch(Exception e) { 42 System.out.println("Test failed: " + e); 43 } 44 } 45 46 private AbstractTest[] getTests() { 47 ArrayList al = new ArrayList(); 48 49 URL location = getClass().getProtectionDomain().getCodeSource().getLocation(); 50 String cName = getClass().getName(); 51 String pkg = cName.substring(0, cName.lastIndexOf('.') + 1); 52 String fName = location.getFile() + pkg.replace('.', '/'); 53 54 File f = new File(fName); 55 56 File[] files = f.listFiles(); 57 if (files != null) { 58 for (int i = 0; i < files.length; i++) { 59 if (files[i].isDirectory()) { 60 processDir(pkg + files[i].getName(), files[i], al); 61 } 62 } 63 } 64 65 return (AbstractTest[])al.toArray(new AbstractTest[0]); 66 } 67 68 69 private void processDir(String pkg, File dir, List testCases) { 70 File[] files = dir.listFiles(); 71 if (files != null) { 72 for (int i = 0; i < files.length; i++) { 73 if (!files[i].isDirectory()) { 74 try { 75 String cName = pkg + "." +files[i].getName(); 76 cName = cName.substring(0, cName.lastIndexOf('.')); 77 Class c = Class.forName(cName); 78 Object o = c.newInstance(); 79 if (AbstractTest.class.isInstance(o)) { 80 testCases.add(o); 81 } 82 } 83 catch(Exception e) { 84 System.out.println("Failed to get testcase: " + 85 files[i].getName() + ", " + e); 86 } 87 } 88 } 89 } 90 } 91 92 93 void printResults() { 94 } 95 } 96 | Popular Tags |