• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package perf;
2 
3 abstract class ParserTestBase
4 {
5     protected int hash;
6 
test(String desc1, String desc2, int expSize)7     protected <T1, T2> void test(String desc1, String desc2, int expSize)
8         throws Exception
9     {
10         // guessing we have 500 byte
11         final int REPS = (int) ((double) (10 * 1000 * 1000) / (double) expSize);
12 
13         System.out.printf("Estimating %d bytes to read; will do %d repetitions\n",
14                 expSize, REPS);
15 
16         int i = 0;
17         int roundsDone = 0;
18         final int TYPES = 2;
19         final int WARMUP_ROUNDS = 5;
20 
21         final long[] times = new long[TYPES];
22 
23         while (true) {
24             try {  Thread.sleep(100L); } catch (InterruptedException ie) { }
25             int round = (i++ % TYPES);
26 
27             String msg;
28             boolean lf = (round == 0);
29 
30             long msecs;
31 
32             switch (round) {
33             case 0:
34                 msg = desc1;
35                 msecs = _testRead1(REPS);
36                 break;
37             case 1:
38                 msg = desc2;
39                 msecs = _testRead2(REPS);
40                 break;
41             default:
42                 throw new Error();
43             }
44 
45             // skip first 5 rounds to let results stabilize
46             if (roundsDone >= WARMUP_ROUNDS) {
47                 times[round] += msecs;
48             }
49 
50             System.out.printf("Test '%s' [hash: 0x%s] -> %d msecs\n", msg, this.hash, msecs);
51             if (lf) {
52                 ++roundsDone;
53                 if ((roundsDone % 3) == 0 && roundsDone > WARMUP_ROUNDS) {
54                     double den = (double) (roundsDone - WARMUP_ROUNDS);
55                     System.out.printf("Averages after %d rounds ("+desc1+" / "+desc2+"): %.1f / %.1f msecs\n",
56                             (int) den,
57                             times[0] / den, times[1] / den);
58 
59                 }
60                 System.out.println();
61             }
62             if ((i % 17) == 0) {
63                 System.out.println("[GC]");
64                 Thread.sleep(100L);
65                 System.gc();
66                 Thread.sleep(100L);
67             }
68         }
69     }
70 
_testRead1(int reps)71     protected long _testRead1(int reps) throws Exception {
72         final long start = System.currentTimeMillis();
73         testRead1(reps);
74         return System.currentTimeMillis() - start;
75     }
76 
_testRead2(int reps)77     protected long _testRead2(int reps) throws Exception {
78         final long start = System.currentTimeMillis();
79         testRead2(reps);
80         return System.currentTimeMillis() - start;
81     }
82 
testRead1(int reps)83     protected abstract void testRead1(int reps) throws Exception;
84 
testRead2(int reps)85     protected abstract void testRead2(int reps) throws Exception;
86 
aposToQuotes(String json)87     protected static String aposToQuotes(String json) {
88         return json.replace("'", "\"");
89     }
90 }
91 
92