77import com .fasterxml .jackson .databind .ObjectMapper ;
88import com .fasterxml .jackson .module .afterburner .AfterburnerModule ;
99import com .jsoniter .JsonIterator ;
10+ import com .jsoniter .ReflectionDecoder ;
11+ import com .jsoniter .spi .ExtensionManager ;
1012import com .jsoniter .spi .TypeLiteral ;
1113import org .junit .Test ;
1214import org .openjdk .jmh .Main ;
1315import org .openjdk .jmh .annotations .*;
16+ import org .openjdk .jmh .infra .BenchmarkParams ;
1417import org .openjdk .jmh .infra .Blackhole ;
1518
1619import java .io .IOException ;
@@ -25,6 +28,7 @@ public class SimpleObjectBinding {
2528 private DslJson dslJson ;
2629 private Class <TestObject > clazz ;
2730 private String inputStr ;
31+ private TestObject testObject ;
2832
2933 @ CompiledJson
3034 public static class TestObject {
@@ -44,7 +48,7 @@ public String toString() {
4448 private JsonIterator iter ;
4549
4650 @ Setup (Level .Trial )
47- public void benchSetup () {
51+ public void benchSetup (BenchmarkParams params ) {
4852 inputStr = "{'field1':100,'field2':101}" ;
4953 input = inputStr .replace ('\'' , '"' ).getBytes ();
5054 iter = JsonIterator .parse (input );
@@ -54,60 +58,98 @@ public void benchSetup() {
5458 };
5559 clazz = TestObject .class ;
5660 jackson = new ObjectMapper ();
57- jackson .registerModule (new AfterburnerModule ());
5861 dslJson = new DslJson ();
62+ testObject = new TestObject ();
63+ if (params != null ) {
64+ if (params .getBenchmark ().contains ("withReflection" )) {
65+ ExtensionManager .registerTypeDecoder (TestObject .class , new ReflectionDecoder (TestObject .class ));
66+ }
67+ if (params .getBenchmark ().contains ("withBindApiStrictMode" )) {
68+ JsonIterator .enableStrictMode ();
69+ }
70+ if (params .getBenchmark ().contains ("withJacksonAfterburner" )) {
71+ jackson .registerModule (new AfterburnerModule ());
72+ }
73+ }
5974 }
6075
6176 @ Test
6277 public void test () throws IOException {
63- benchSetup ();
78+ benchSetup (null );
6479 System .out .println (withIterator ());
65- System .out .println (withBindApiNoneStrictMode ());
66- System .out .println (withBindApiStrictMode ());
80+ System .out .println (withIteratorIfElse ());
81+ System .out .println (withIteratorIntern ());
82+ System .out .println (withBindApi ());
83+ System .out .println (withExistingObject ());
6784 System .out .println (withJackson ());
6885 System .out .println (withDsljson ());
6986 System .out .println (withFastjson ());
7087 }
7188
72- @ Benchmark
89+ public static void main (String [] args ) throws Exception {
90+ Main .main (new String []{
91+ "SimpleObjectBinding.*" ,
92+ "-i" , "5" ,
93+ "-wi" , "5" ,
94+ "-f" , "1"
95+ });
96+ }
97+
98+ // @Benchmark
7399 public void withIterator (Blackhole bh ) throws IOException {
74100 bh .consume (withIterator ());
75101 }
76102
77- @ Benchmark
78- public void withBindApiNoneStrictMode (Blackhole bh ) throws IOException {
79- bh .consume (withBindApiNoneStrictMode ());
103+ // @Benchmark
104+ public void withIteratorIfElse (Blackhole bh ) throws IOException {
105+ bh .consume (withIteratorIfElse ());
80106 }
81107
82- @ Benchmark
108+ // @Benchmark
109+ public void withIteratorIntern (Blackhole bh ) throws IOException {
110+ bh .consume (withIteratorIntern ());
111+ }
112+
113+ // @Benchmark
114+ public void withoutExistingObject (Blackhole bh ) throws IOException {
115+ bh .consume (withBindApi ());
116+ }
117+
118+ // @Benchmark
83119 public void withBindApiStrictMode (Blackhole bh ) throws IOException {
84- bh .consume (withBindApiStrictMode ());
120+ bh .consume (withBindApi ());
121+ }
122+
123+ // @Benchmark
124+ public void withReflection (Blackhole bh ) throws IOException {
125+ bh .consume (withBindApi ());
126+ }
127+
128+ // @Benchmark
129+ public void withExistingObject (Blackhole bh ) throws IOException {
130+ bh .consume (withExistingObject ());
85131 }
86132
87133 @ Benchmark
88- public void withJackson (Blackhole bh ) throws IOException {
134+ public void withJacksonAfterburner (Blackhole bh ) throws IOException {
89135 bh .consume (withJackson ());
90136 }
91137
92138 @ Benchmark
139+ public void withJacksonNoAfterburner (Blackhole bh ) throws IOException {
140+ bh .consume (withJackson ());
141+ }
142+
143+ // @Benchmark
93144 public void withDsljson (Blackhole bh ) throws IOException {
94145 bh .consume (withDsljson ());
95146 }
96147
97- @ Benchmark
148+ // @Benchmark
98149 public void withFastjson (Blackhole bh ) throws IOException {
99150 bh .consume (withFastjson ());
100151 }
101152
102- public static void main (String [] args ) throws Exception {
103- Main .main (new String []{
104- "SimpleObjectBinding.*" ,
105- "-i" , "5" ,
106- "-wi" , "5" ,
107- "-f" , "1"
108- });
109- }
110-
111153 private TestObject withIterator () throws IOException {
112154 iter .reset ();
113155 TestObject obj = new TestObject ();
@@ -126,17 +168,51 @@ private TestObject withIterator() throws IOException {
126168 return obj ;
127169 }
128170
129- private TestObject withBindApiNoneStrictMode () throws IOException {
171+ private TestObject withIteratorIfElse () throws IOException {
130172 iter .reset ();
131- return iter .read (typeLiteral );
173+ TestObject obj = new TestObject ();
174+ for (String field = iter .readObject (); field != null ; field = iter .readObject ()) {
175+ if (field .equals ("field1" )) {
176+ obj .field1 = iter .readInt ();
177+ continue ;
178+ }
179+ if (field .equals ("field2" )) {
180+ obj .field2 = iter .readInt ();
181+ continue ;
182+ }
183+ iter .skip ();
184+ }
185+ return obj ;
186+ }
187+
188+ private TestObject withIteratorIntern () throws IOException {
189+ iter .reset ();
190+ TestObject obj = new TestObject ();
191+ for (String field = iter .readObject (); field != null ; field = iter .readObject ()) {
192+ field = field .intern ();
193+ if (field == "field1" ) {
194+ obj .field1 = iter .readInt ();
195+ continue ;
196+ }
197+ if (field == "field2" ) {
198+ obj .field2 = iter .readInt ();
199+ continue ;
200+ }
201+ iter .skip ();
202+ }
203+ return obj ;
132204 }
133205
134- private TestObject withBindApiStrictMode () throws IOException {
135- JsonIterator .enableStrictMode ();
206+ private TestObject withBindApi () throws IOException {
136207 iter .reset ();
137208 return iter .read (typeLiteral );
138209 }
139210
211+ private TestObject withExistingObject () throws IOException {
212+ iter .reset ();
213+ return iter .read (typeLiteral , testObject );
214+ }
215+
140216 private TestObject withJackson () throws IOException {
141217 return jackson .readValue (input , typeRef );
142218 }
0 commit comments