diff options
author | Jon Bratseth <bratseth@yahoo-inc.com> | 2016-06-15 23:09:44 +0200 |
---|---|---|
committer | Jon Bratseth <bratseth@yahoo-inc.com> | 2016-06-15 23:09:44 +0200 |
commit | 72231250ed81e10d66bfe70701e64fa5fe50f712 (patch) | |
tree | 2728bba1131a6f6e5bdf95afec7d7ff9358dac50 /vespajlib/developernotes/XMLWriterMicroBenchmark.java |
Publish
Diffstat (limited to 'vespajlib/developernotes/XMLWriterMicroBenchmark.java')
-rw-r--r-- | vespajlib/developernotes/XMLWriterMicroBenchmark.java | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/vespajlib/developernotes/XMLWriterMicroBenchmark.java b/vespajlib/developernotes/XMLWriterMicroBenchmark.java new file mode 100644 index 00000000000..67570d54ea6 --- /dev/null +++ b/vespajlib/developernotes/XMLWriterMicroBenchmark.java @@ -0,0 +1,61 @@ +// Copyright 2016 Yahoo Inc. 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 <a href="mailto:bratseth@yahoo-inc.com">Jon Bratseth</a> + */ +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); + } + + +} |