Skip to content

Commit 4ad8aa9

Browse files
committed
encode int/long
1 parent 8f4d4b2 commit 4ad8aa9

5 files changed

Lines changed: 406 additions & 81 deletions

File tree

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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+
}

demo/src/test/java/com/jsoniter/demo/StringOutput.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33

44
import com.dslplatform.json.DslJson;
5+
import com.fasterxml.jackson.core.JsonGenerator;
56
import com.fasterxml.jackson.databind.ObjectMapper;
67
import com.jsoniter.output.JsonStream;
78
import org.junit.Test;
@@ -45,6 +46,7 @@ public void test() throws IOException {
4546
public void benchSetup(BenchmarkParams params) {
4647
baos = new ByteArrayOutputStream(1024 * 64);
4748
objectMapper = new ObjectMapper();
49+
objectMapper.getFactory().configure(JsonGenerator.Feature.ESCAPE_NON_ASCII, true);
4850
stream = new JsonStream(baos, 4096);
4951
buffer = new byte[4096];
5052
dslJson = new DslJson();
@@ -54,19 +56,19 @@ public void benchSetup(BenchmarkParams params) {
5456
public void jsoniter() throws IOException {
5557
baos.reset();
5658
stream.reset(baos, buffer);
57-
stream.writeVal("hello");
59+
stream.writeVal("hello world ~~ hello 中文 ~~~");
5860
stream.flush();
5961
}
6062

6163
@Benchmark
6264
public void jackson() throws IOException {
6365
baos.reset();
64-
objectMapper.writeValue(baos, "hello");
66+
objectMapper.writeValue(baos, "hello world ~~ hello 中文 ~~~");
6567
}
6668

6769
@Benchmark
6870
public void dsljson() throws IOException {
6971
baos.reset();
70-
dslJson.serialize("hello", baos);
72+
dslJson.serialize("hello world ~~ hello 中文 ~~~", baos);
7173
}
7274
}

0 commit comments

Comments
 (0)