aboutsummaryrefslogtreecommitdiffstats
path: root/vespajlib/src/test/java/com/yahoo/text/JsonMicroBenchmarkTestCase.java
blob: e2f9614aa16b8804cf56aeaf36b7108a062ceb11 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.text;

import static org.junit.Assert.*;

import java.io.ByteArrayOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.TreeMap;

import com.fasterxml.jackson.core.JsonEncoding;
import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonGenerator;
import org.junit.After;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;

@SuppressWarnings("deprecation")
public class JsonMicroBenchmarkTestCase {

    private static final long RUNTIME = 20L * 60L * 1000L;

    @Before
    public void setUp() throws Exception {
    }

    @After
    public void tearDown() throws Exception {
    }

    enum Strategy {
        VESPAJLIB, JACKSON;
    }

    private static abstract class BenchFactory {
        abstract Bench produce();
    }

    private static class VespajlibFactory extends BenchFactory {

        @Override
        Bench produce() {
            return new OutputWithWriter();
        }

    }

    private static class JacksonFactory extends BenchFactory {
        @Override
        Bench produce() {
            return new OutputWithGenerator();
        }
    }

    private static abstract class Bench implements Runnable {
        public volatile long runs;
        public volatile long start;
        public volatile long end;
        public volatile long metric;

        /**
         * Object identity is used to differentiate between different implementation strategies, toString() is used to print a report.
         *
         * @return an object with a descriptive toString() for the implementation under test
         */
        abstract Object category();

        @Override
        public final void run() {
            Random random = new Random(42L);
            long localBytesWritten = 0L;
            long localRuns = 0;

            start = System.currentTimeMillis();
            long target = start + JsonMicroBenchmarkTestCase.RUNTIME;

            while (System.currentTimeMillis() < target) {
                for (int i = 0; i < 1000; ++i) {
                    localBytesWritten += iterate(random);
                }
                localRuns += 1000L;
            }
            end = System.currentTimeMillis();
            runs = localRuns;
            metric = localBytesWritten;
        }

        abstract int iterate(Random random);
    }

    private static final class OutputWithGenerator extends Bench {

        public OutputWithGenerator() {
        }


        int iterate(Random random) {
            JsonGenerator generator;
            ByteArrayOutputStream generatorOut = new ByteArrayOutputStream();
            try {
                generator = new JsonFactory().createJsonGenerator(generatorOut,
                        JsonEncoding.UTF8);
            } catch (IOException e) {
                e.printStackTrace();
                return 0;
            }
            try {
                serialize(generatedDoc(random), generator);
            } catch (IOException e) {
                e.printStackTrace();
                return 0;
            }
            try {
                generator.close();
            } catch (IOException e) {
                e.printStackTrace();
                return 0;
            }
            return generatorOut.toByteArray().length;
        }

        static void serialize(Map<String, Object> m, JsonGenerator g) throws IOException {
            g.writeStartObject();
            for (Map.Entry<String, Object> e : m.entrySet()) {
                g.writeFieldName(e.getKey());
                serializeField(g, e.getValue());
            }
            g.writeEndObject();
        }

        @SuppressWarnings("unchecked")
        static void serializeField(JsonGenerator g, final Object value)
                throws IOException {
            if (value instanceof Map) {
                serialize((Map<String, Object>) value, g);
            } else if (value instanceof Number) {
                g.writeNumber(((Number) value).intValue());
            } else if (value instanceof String) {
                g.writeString((String) value);
            } else if (value instanceof List) {
                g.writeStartArray();
                for (Object o : (List<Object>) value) {
                    serializeField(g, o);
                }
                g.writeEndArray();
            } else {
                throw new IllegalArgumentException();
            }
        }

        @Override
        Object category() {
            return Strategy.JACKSON;
        }

    }

    private static final class OutputWithWriter extends Bench {

        OutputWithWriter() {
        }

        int iterate(Random random) {
            ByteArrayOutputStream writerOut = new ByteArrayOutputStream();
            JSONWriter writer = new JSONWriter(writerOut);
            try {
                serialize(generatedDoc(random), writer);
            } catch (IOException e) {
                e.printStackTrace();
                return 0;
            }
            return writerOut.toByteArray().length;
        }

        static void serialize(Map<String, Object> m, JSONWriter w) throws IOException {
            w.beginObject();
            for (Map.Entry<String, Object> e : m.entrySet()) {
                w.beginField(e.getKey());
                final Object value = e.getValue();
                serializeField(w, value);
                w.endField();
            }
            w.endObject();
        }

        @SuppressWarnings("unchecked")
        static void serializeField(JSONWriter w, final Object value)
                throws IOException {
            if (value instanceof Map) {
                serialize((Map<String, Object>) value, w);
            } else if (value instanceof Number) {
                w.value((Number) value);
            } else if (value instanceof String) {
                w.value((String) value);
            } else if (value instanceof List) {
                w.beginArray();
                for (Object o : (List<Object>) value) {
                    w.beginArrayValue();
                    serializeField(w, o);
                    w.endArrayValue();
                }
                w.endArray();
            } else {
                throw new IllegalArgumentException();
            }
        }

        @Override
        Object category() {
            return Strategy.VESPAJLIB;
        }

    }

    @Test
    @Ignore
    public final void test() throws InterruptedException {
        final OutputWithWriter forWriter = new OutputWithWriter();
        Thread writerThread = new Thread(forWriter);
        final OutputWithGenerator forGenerator = new OutputWithGenerator();
        Thread generatorThread = new Thread(forGenerator);
        writerThread.start();
        generatorThread.start();
        writerThread.join();
        generatorThread.join();
        System.out.println("Generator time: " + (forGenerator.end - forGenerator.start));
        System.out.println("Writer time: " + (forWriter.end - forWriter.start));
        System.out.println("Output length from generator: " + forGenerator.metric);
        System.out.println("Output length from writer: " + forWriter.metric);
        System.out.println("Iterations with generator: " + forGenerator.runs);
        System.out.println("Iterations with writer: " + forWriter.runs);
        System.out.println("Iterations/s with generator: " + ((double) forGenerator.runs / (double) (forGenerator.end - forGenerator.start)) * 1000.0d);
        System.out.println("Iterations/s  with writer: " + ((double) forWriter.runs / (double) (forWriter.end - forWriter.start)) * 1000.0d);
    }

    @Test
    @Ignore
    public final void test16Threads() throws InterruptedException {
        List<Thread> threads = new ArrayList<>(16);
        List<Bench> benches = createBenches(8, new VespajlibFactory(), new JacksonFactory());

        for (Bench bench : benches) {
            threads.add(new Thread(bench));
        }
        for (Thread t : threads) {
            t.start();
        }
        for (Thread t : threads) {
            t.join();
        }

        System.out.println("8 Jackson threads competing with 8 VespaJLib threads.");
        metrics(benches, Strategy.JACKSON);
        metrics(benches, Strategy.VESPAJLIB);
    }

    @Test
    @Ignore
    public final void test16ThreadsJacksonOnly() throws InterruptedException {
        List<Thread> threads = new ArrayList<>(16);
        List<Bench> benches = createBenches(16, new JacksonFactory());

        for (Bench bench : benches) {
            threads.add(new Thread(bench));
        }
        for (Thread t : threads) {
            t.start();
        }
        for (Thread t : threads) {
            t.join();
        }

        System.out.println("16 Jackson threads.");
        metrics(benches, Strategy.JACKSON);
    }

    @Test
    @Ignore
    public final void test16ThreadsVespaJlibOnly() throws InterruptedException {
        List<Thread> threads = new ArrayList<>(16);
        List<Bench> benches = createBenches(16, new VespajlibFactory());

        for (Bench bench : benches) {
            threads.add(new Thread(bench));
        }
        for (Thread t : threads) {
            t.start();
        }
        for (Thread t : threads) {
            t.join();
        }

        System.out.println("16 VespaJLib threads.");
        metrics(benches, Strategy.VESPAJLIB);
    }


    private void metrics(List<Bench> benches, Strategy choice) {
        List<Bench> chosen = new ArrayList<>();

        for (Bench b : benches) {
            if (b.category() == choice) {
                chosen.add(b);
            }
        }

        long[] rawTime = new long[chosen.size()];
        long[] rawOutputLength = new long[chosen.size()];
        long[] rawIterations = new long[chosen.size()];
        double[] rawIterationsPerSecond = new double[chosen.size()];

        for (int i = 0; i < chosen.size(); ++i) {
            Bench b = chosen.get(i);
            rawTime[i] = b.end - b.start;
            rawOutputLength[i] = b.metric;
            rawIterations[i] = b.runs;
            rawIterationsPerSecond[i] = ((double) b.runs) / (((double) (b.end - b.start)) / 1000.0d);
        }

        double avgTime = mean(rawTime);
        double avgOutputLength = mean(rawOutputLength);
        double avgIterations = mean(rawIterations);
        double avgIterationsPerSecond = mean(rawIterationsPerSecond);

        System.out.println("For " + choice + ":");
        dumpMetric("run time", rawTime, avgTime, "s", 0.001d);
        dumpMetric("output length", rawOutputLength, avgOutputLength, "bytes", 1.0d);
        dumpMetric("iterations", rawIterations, avgIterations, "", 1.0d);
        dumpMetric("iterations per second", rawIterationsPerSecond, avgIterationsPerSecond, "s**-1", 1.0d);
    }

    private void dumpMetric(String name, long[] raw, double mean, String unit, double scale) {
        System.out.println("Average " + name + ": " + mean  * scale + " " + unit);
        System.out.println("Mean absolute deviation of " + name + ": " + averageAbsoluteDeviationFromMean(raw, mean) * scale + " " + unit);
        System.out.println("Minimum " + name + ": " + min(raw) * scale + " " + unit);
        System.out.println("Maximum " + name + ": " + max(raw) * scale + " " + unit);
    }

    private void dumpMetric(String name, double[] raw, double mean, String unit, double scale) {
        System.out.println("Average " + name + ": " + mean  * scale + " " + unit);
        System.out.println("Mean absolute deviation of " + name + ": " + averageAbsoluteDeviationFromMean(raw, mean) * scale + " " + unit);
        System.out.println("Minimum " + name + ": " + min(raw) * scale + " " + unit);
        System.out.println("Maximum " + name + ": " + max(raw) * scale + " " + unit);
    }

    private List<Bench> createBenches(int ofEach, BenchFactory... factories) {
        List<Bench> l = new ArrayList<>(ofEach * factories.length);

        // note how the bench objects of different objects become intermingled, this is by design
        for (int i = 0; i < ofEach; ++i) {
            for (BenchFactory factory : factories) {
                l.add(factory.produce());
            }
        }
        return l;
    }

    private double mean(long[] values) {
        long sum = 0L;

        // ignore overflow :)
        for (long v : values) {
            sum += v;
        }
        return ((double) sum / (double) values.length);
    }

    private double mean(double[] values) {
        double sum = 0L;

        for (double v : values) {
            sum += v;
        }
        return sum / (double) values.length;
    }

    private double averageAbsoluteDeviationFromMean(long[] values, double mean) {
        double sum = 0.0d;

        for (long v : values) {
            sum += Math.abs(mean - (double) v);
        }

        return sum / (double) values.length;
    }

    private double averageAbsoluteDeviationFromMean(double[] values, double mean) {
        double sum = 0.0d;

        for (double v : values) {
            sum += Math.abs(mean - v);
        }

        return sum / (double) values.length;
    }

    private long min(long[] values) {
        long min = Long.MAX_VALUE;

        for (long v : values) {
            min = Math.min(min, v);
        }
        return min;
    }

    private double min(double[] values) {
        double min = Double.MAX_VALUE;

        for (double v : values) {
            min = Math.min(min, v);
        }
        return min;
    }

    private long max(long[] values) {
        long max = Long.MIN_VALUE;

        for (long v : values) {
            max = Math.max(max, v);
        }
        return max;
    }

    private double max(double[] values) {
        double max = Double.MIN_VALUE;

        for (double v : values) {
            max = Math.max(max, v);
        }
        return max;
    }

    @SuppressWarnings("null")
    @Test
    @Ignore
    public final void testSanity() throws IOException {
        @SuppressWarnings("unused")
        String a, b;
        {
            Random random = new Random(42L);
            JsonGenerator generator = null;
            ByteArrayOutputStream generatorOut = new ByteArrayOutputStream();
            try {
                generator = new JsonFactory().createJsonGenerator(generatorOut,
                        JsonEncoding.UTF8);
            } catch (IOException e) {
                e.printStackTrace();
                fail();
            }
            try {
                OutputWithGenerator.serialize(generatedDoc(random), generator);
            } catch (IOException e) {
                e.printStackTrace();
                fail();
            }
            try {
                generator.close();
            } catch (IOException e) {
                e.printStackTrace();
                fail();
            }
            a = generatorOut.toString("UTF-8");
        }
        {
            Random random = new Random(42L);
            ByteArrayOutputStream writerOut = new ByteArrayOutputStream();
            JSONWriter writer = new JSONWriter(writerOut);
            try {
                OutputWithWriter.serialize(generatedDoc(random), writer);
            } catch (IOException e) {
                e.printStackTrace();
                fail();
            }
            b = writerOut.toString("UTF-8");
        }
        // dumpToFile("/tmp/a", a);
        // dumpToFile("/tmp/b", b);
    }

    @SuppressWarnings("unused")
    private void dumpToFile(String path, String b) throws IOException {
        FileWriter f = new FileWriter(path);
        f.write(b);
        f.close();
    }

    static Map<String, Object> generatedDoc(Random random) {
        return generateObject(random, 0, random.nextInt(8));
    }

    static String generateFieldName(Random random) {
        int len = random.nextInt(100) + 3;
        char[] base = new char[len];
        for (int i = 0; i < len; ++i) {
            base[i] = (char) (random.nextInt(26) + 'a');
        }
        return new String(base);
    }

    static byte[] generateByteArrayPayload(Random random) {
        return null;
    }

    static String generateStringPayload(Random random) {
        int len = random.nextInt(100) + random.nextInt(100) + random.nextInt(100) + random.nextInt(100);
        char[] base = new char[len];
        for (int i = 0; i < len; ++i) {
            base[i] = (char) random.nextInt(0xd800);
        }
        return new String(base);
    }

    static Number generateInt(Random random) {
        return Integer.valueOf(random.nextInt());
    }

    static List<Object> generateArray(Random random, int nesting, int maxNesting) {
        int len = random.nextInt(10) + random.nextInt(10) + random.nextInt(10) + random.nextInt(10);
        List<Object> list = new ArrayList<>(len);
        for (int i = 0; i < len; ++i) {
            list.add(generateStuff(random, nesting, maxNesting));
        }
        return list;
    }

    private static Object generateStuff(Random random, int nesting, int maxNesting) {
        if (nesting >= maxNesting) {
            return generatePrimitive(random);
        } else {
            final int die = random.nextInt(10);
            if (die == 9) {
                return generateObject(random, nesting + 1, maxNesting);
            } else if (die == 8) {
                return generateArray(random, nesting + 1, maxNesting);
            } else {
                return generatePrimitive(random);
            }
        }
    }

    private static Object generatePrimitive(Random random) {
        if (random.nextInt(2) == 0) {
            return generateStringPayload(random);
        } else {
            return generateInt(random);
        }
    }

    static Map<String, Object> generateObject(Random random, int nesting, int maxNesting) {
        int len = random.nextInt(5) + random.nextInt(5) + random.nextInt(5) + random.nextInt(5);
        Map<String, Object> m = new TreeMap<>();
        for (int i = 0; i < len; ++i) {
            m.put(generateFieldName(random), generateStuff(random, nesting, maxNesting));
        }
        return m;
    }

}