Debian Codegraph @

src/test/java/perf/ReaderTestBase.java

raw
1 package perf;
2
3 import java.io.*;
4
5 import com.fasterxml.jackson.core.JsonFactory;
6 import com.fasterxml.jackson.core.JsonGenerator;
7 import com.fasterxml.jackson.core.JsonParser;
8 import com.fasterxml.jackson.databind.*;
9
10 abstract class ReaderTestBase
11 {
12 protected final static int WARMUP_ROUNDS = 5;
13
14 protected String _desc1, _desc2;
15
16 protected int hash;
17 protected long startMeasure = System.currentTimeMillis() + 5000L;
18 protected int roundsDone = 0;
19 protected int REPS;
20 private double[] timeMsecs;
21
22 protected abstract int targetSizeMegs();
23
24 protected void testFromBytes(ObjectMapper mapper1, String desc1,
25 Object inputValue1, Class<?> inputClass1,
26 ObjectMapper mapper2, String desc2,
27 Object inputValue2, Class<?> inputClass2)
28 throws Exception
29 {
30 final byte[] byteInput1 = mapper1.writeValueAsBytes(inputValue1);
31 final byte[] byteInput2 = mapper2.writeValueAsBytes(inputValue2);
32 // Let's try to guestimate suitable size... to get to N megs to process
33 REPS = (int) ((double) (targetSizeMegs() * 1000 * 1000) / (double) byteInput1.length);
34
35 // sanity check:
36 /*T1 back1 =*/ mapper1.readValue(byteInput1, inputClass1);
37 /*T2 back2 =*/ mapper2.readValue(byteInput2, inputClass2);
38 System.out.println("Input successfully round-tripped for both input types");
39
40 _desc1 = String.format("%s (%d bytes)", desc1, byteInput1.length);
41 _desc2 = String.format("%s (%d bytes)", desc2, byteInput2.length);
42
43 System.out.printf("Read %d/%d bytes to bind; will do %d repetitions to get about %d megs\n",
44 byteInput1.length, byteInput2.length, REPS, targetSizeMegs());
45
46 doTest(mapper1, byteInput1, inputClass1, mapper2, byteInput2, inputClass2);
47 }
48
49 protected void doTest(ObjectMapper mapper1, byte[] byteInput1, Class<?> inputClass1,
50 ObjectMapper mapper2, byte[] byteInput2, Class<?> inputClass2)
51 throws Exception
52 {
53 System.out.print("Warming up");
54
55 final ObjectReader jsonReader = mapper1.reader()
56 .forType(inputClass1);
57 final ObjectReader arrayReader = mapper2.reader()
58 .forType(inputClass2);
59
60 int i = 0;
61 final int TYPES = 2;
62
63 timeMsecs = new double[TYPES];
64
65 while (true) {
66 Thread.sleep(100L);
67 final int type = (i++ % TYPES);
68
69 String msg;
70 double msesc;
71
72 switch (type) {
73 case 0:
74 msg = _desc1;
75 msesc = testDeser1(REPS, byteInput1, jsonReader);
76 break;
77 case 1:
78 msg = _desc2;
79 msesc = testDeser2(REPS, byteInput2, arrayReader);
80 break;
81 default:
82 throw new Error();
83 }
84 updateStats(type, (i % 17) == 0, msg, msesc);
85 }
86 }
87
88 private void updateStats(int type, boolean doGc, String msg, double msecs)
89 throws Exception
90 {
91 final boolean lf = (type == (timeMsecs.length - 1));
92
93 if (startMeasure == 0L) { // skip first N seconds
94 timeMsecs[type] += msecs;
95 } else {
96 if (lf) {
97 if (System.currentTimeMillis() >= startMeasure) {
98 startMeasure = 0L;
99 System.out.println(" complete!");
100 } else {
101 System.out.print(".");
102 }
103 }
104 return;
105 }
106
107 System.out.printf("Test '%s' [hash: 0x%s] -> %.1f msecs\n", msg, Integer.toHexString(hash), msecs);
108 if (lf) {
109 ++roundsDone;
110 if ((roundsDone % 3) == 0 ) {
111 double den = (double) roundsDone;
112 System.out.printf("Averages after %d rounds (%s/%s): %.1f / %.1f msecs\n",
113 (int) den, _desc1, _desc2,
114 timeMsecs[0] / den, timeMsecs[1] / den);
115 }
116 System.out.println();
117 }
118 if (doGc) {
119 System.out.println("[GC]");
120 Thread.sleep(100L);
121 System.gc();
122 Thread.sleep(100L);
123 }
124 }
125
126 protected double testDeser1(int reps, byte[] input, ObjectReader reader) throws Exception {
127 return _testDeser(reps, input, reader);
128 }
129 protected double testDeser2(int reps, byte[] input, ObjectReader reader) throws Exception {
130 return _testDeser(reps, input, reader);
131 }
132
133 protected final double _testDeser(int reps, byte[] input, ObjectReader reader) throws Exception
134 {
135 long start = System.nanoTime();
136 Object result = null;
137 while (--reps >= 0) {
138 result = reader.readValue(input);
139 }
140 hash = result.hashCode();
141 // return microseconds
142 return _msecsFromNanos(System.nanoTime() - start);
143 }
144
145 protected double testDeser1(int reps, String input, ObjectReader reader) throws Exception {
146 return _testDeser(reps, input, reader);
147 }
148
149 protected double testDeser2(int reps, String input, ObjectReader reader) throws Exception {
150 return _testDeser(reps, input, reader);
151 }
152
153 protected final double _testDeser(int reps, String input, ObjectReader reader) throws Exception
154 {
155 long start = System.nanoTime();
156 Object result = null;
157 while (--reps >= 0) {
158 result = reader.readValue(input);
159 }
160 hash = result.hashCode();
161 return _msecsFromNanos(System.nanoTime() - start);
162 }
163
164 protected final double _msecsFromNanos(long nanos) {
165 return (nanos / 1000000.0);
166 }
167
168 protected static byte[] readAll(String filename) throws IOException
169 {
170 File f = new File(filename);
171 ByteArrayOutputStream bytes = new ByteArrayOutputStream((int) f.length());
172 byte[] buffer = new byte[4000];
173 int count;
174 FileInputStream in = new FileInputStream(f);
175
176 while ((count = in.read(buffer)) > 0) {
177 bytes.write(buffer, 0, count);
178 }
179 in.close();
180 return bytes.toByteArray();
181 }
182
183 protected static byte[] convert(byte[] input, JsonFactory inputF, JsonFactory outputF)
184 throws IOException
185 {
186 ByteArrayOutputStream bytes = new ByteArrayOutputStream(input.length);
187 JsonParser p = inputF.createParser(input);
188 JsonGenerator g = outputF.createGenerator(bytes);
189
190 while (p.nextToken() != null) {
191 g.copyCurrentEvent(p);
192 }
193 p.close();
194 g.close();
195
196 return bytes.toByteArray();
197 }
198 }