aboutsummaryrefslogtreecommitdiffstats
path: root/vespajlib/developernotes/XMLWriterMicroBenchmark.java
blob: 0196f6fb595921220a9a10a60fd280f62d600e48 (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.text;

import com.yahoo.io.ByteWriter;

import java.io.ByteArrayOutputStream;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.nio.charset.CharsetEncoder;

/**
 * It is what it says
 *
 * @author bratseth
 */
public class XMLWriterMicroBenchmark {

    private final ByteArrayOutputStream output;
    private final XMLWriter xmlWriter;

    public XMLWriterMicroBenchmark(boolean optimize) {
        // setup
        output=new ByteArrayOutputStream();
        Charset cs = Charset.forName("utf-8");
        CharsetEncoder encoder = cs.newEncoder();
        xmlWriter=new XMLWriter(new ByteWriter(output, encoder), optimize);
    }

    public void benchmark(int sizeInK,boolean verifyOutput) {
        System.out.println("Warming up...");
        writeStrings(1000); // warm-up

        System.out.println("Starting benchmark...");
        long startTime=System.currentTimeMillis();
        writeStrings(sizeInK);
        long endTime=System.currentTimeMillis();
        System.out.println("Done.\nWriting " + sizeInK + "k strings took " + (endTime-startTime) + "ms");

        if (verifyOutput) {
            System.out.println("First 1k of output:");
            String result=null;
            try { result=output.toString("utf-8"); } catch (UnsupportedEncodingException e) { throw new RuntimeException(e); }
            System.out.println(result.substring(0,Math.min(500,result.length())));
        }
    }

    private void writeStrings(int sizeInK) {
        for (int i=0; i<1000*sizeInK; i++) {
            xmlWriter.openTag("dummytag").content(i,false).closeTag();
        }
    }

    public static void main(String[] args) {
        System.out.println("Unoptimized: -------------------------");
        new XMLWriterMicroBenchmark(false).benchmark(10000,false);
        System.out.println("Optimized:   ------------------------");
        new XMLWriterMicroBenchmark(true).benchmark(10000,false);
    }


}