|
| 1 | +package com.jsoniter.demo; |
| 2 | + |
| 3 | + |
| 4 | +import com.dslplatform.json.DslJson; |
| 5 | +import com.fasterxml.jackson.core.JsonGenerator; |
| 6 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 7 | +import com.jsoniter.output.JsonStream; |
| 8 | +import org.junit.Test; |
| 9 | +import org.openjdk.jmh.Main; |
| 10 | +import org.openjdk.jmh.annotations.*; |
| 11 | +import org.openjdk.jmh.infra.BenchmarkParams; |
| 12 | + |
| 13 | +import java.io.ByteArrayOutputStream; |
| 14 | +import java.io.IOException; |
| 15 | + |
| 16 | +@State(Scope.Thread) |
| 17 | +public class IntegerOutput { |
| 18 | + |
| 19 | + private ByteArrayOutputStream baos; |
| 20 | + private ObjectMapper objectMapper; |
| 21 | + private JsonStream stream; |
| 22 | + private byte[] buffer; |
| 23 | + private DslJson dslJson; |
| 24 | + |
| 25 | + public static void main(String[] args) throws Exception { |
| 26 | + Main.main(new String[]{ |
| 27 | + "IntegerOutput", |
| 28 | + "-i", "5", |
| 29 | + "-wi", "5", |
| 30 | + "-f", "1", |
| 31 | + }); |
| 32 | + } |
| 33 | + |
| 34 | + @Test |
| 35 | + public void test() throws IOException { |
| 36 | + benchSetup(null); |
| 37 | + jsoniter(); |
| 38 | + System.out.println(baos.toString()); |
| 39 | + jackson(); |
| 40 | + System.out.println(baos.toString()); |
| 41 | + dsljson(); |
| 42 | + System.out.println(baos.toString()); |
| 43 | + } |
| 44 | + |
| 45 | + @Setup(Level.Trial) |
| 46 | + public void benchSetup(BenchmarkParams params) { |
| 47 | + baos = new ByteArrayOutputStream(1024 * 64); |
| 48 | + objectMapper = new ObjectMapper(); |
| 49 | + objectMapper.getFactory().configure(JsonGenerator.Feature.ESCAPE_NON_ASCII, true); |
| 50 | + stream = new JsonStream(baos, 4096); |
| 51 | + buffer = new byte[4096]; |
| 52 | + dslJson = new DslJson(); |
| 53 | + } |
| 54 | + |
| 55 | + @Benchmark |
| 56 | + public void jsoniter() throws IOException { |
| 57 | + baos.reset(); |
| 58 | + stream.reset(baos, buffer); |
| 59 | + stream.writeVal(1024); |
| 60 | + stream.flush(); |
| 61 | + } |
| 62 | + |
| 63 | + @Benchmark |
| 64 | + public void jackson() throws IOException { |
| 65 | + baos.reset(); |
| 66 | + objectMapper.writeValue(baos, 1024); |
| 67 | + } |
| 68 | + |
| 69 | + @Benchmark |
| 70 | + public void dsljson() throws IOException { |
| 71 | + baos.reset(); |
| 72 | + dslJson.serialize(1024, baos); |
| 73 | + } |
| 74 | +} |
0 commit comments