-
Notifications
You must be signed in to change notification settings - Fork 8
/
SerFastjson.java
56 lines (49 loc) · 1.88 KB
/
SerFastjson.java
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
package com.jsoniter.benchmark.with_10_double_fields;
import com.alibaba.fastjson.serializer.JSONSerializer;
import com.alibaba.fastjson.serializer.SerializeConfig;
import com.alibaba.fastjson.serializer.SerializeWriter;
import com.alibaba.fastjson.util.IOUtils;
import com.jsoniter.benchmark.All;
import org.openjdk.jmh.Main;
import org.openjdk.jmh.annotations.*;
import org.openjdk.jmh.infra.BenchmarkParams;
import org.openjdk.jmh.infra.Blackhole;
import org.openjdk.jmh.runner.RunnerException;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
@State(Scope.Thread)
public class SerFastjson {
private TestObject testObject;
private ByteArrayOutputStream byteArrayOutputStream;
private SerializeConfig serializeConfig;
@Setup(Level.Trial)
public void benchSetup(BenchmarkParams params) {
testObject = TestObject.createTestObject();
byteArrayOutputStream = new ByteArrayOutputStream();
serializeConfig = SerializeConfig.getGlobalInstance();
}
@Benchmark
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
public void ser(Blackhole bh) throws IOException {
for (int i = 0; i < 1000; i++) {
byteArrayOutputStream.reset();
SerializeWriter writer = new SerializeWriter();
JSONSerializer serializer = new JSONSerializer(writer, serializeConfig);
serializer.write(testObject);
writer.writeToEx(byteArrayOutputStream, IOUtils.UTF8);
serializer.close();
bh.consume(byteArrayOutputStream);
}
}
public static void main(String[] args) throws IOException, RunnerException {
All.loadJMH();
Main.main(new String[]{
"with_10_double_fields.SerFastjson",
"-i", "5",
"-wi", "5",
"-f", "1",
});
}
}