11package com .jsoniter .demo ;
22
33import com .fasterxml .jackson .annotation .JsonCreator ;
4+ import com .fasterxml .jackson .annotation .JsonIgnore ;
45import com .fasterxml .jackson .annotation .JsonProperty ;
56import com .fasterxml .jackson .core .type .TypeReference ;
67import com .fasterxml .jackson .databind .ObjectMapper ;
78import com .fasterxml .jackson .module .afterburner .AfterburnerModule ;
89import com .jsoniter .JsonIterator ;
9- import com .jsoniter .annotation .JsoniterAnnotationSupport ;
10+ import com .jsoniter .ReflectionDecoder ;
11+ import com .jsoniter .annotation .JacksonAnnotationSupport ;
12+ import com .jsoniter .spi .ExtensionManager ;
1013import com .jsoniter .spi .TypeLiteral ;
1114import org .junit .Test ;
1215import org .openjdk .jmh .Main ;
1316import org .openjdk .jmh .annotations .*;
17+ import org .openjdk .jmh .infra .BenchmarkParams ;
1418import org .openjdk .jmh .infra .Blackhole ;
1519
1620import java .io .IOException ;
@@ -25,14 +29,15 @@ public class ConstructorBinding {
2529 private String inputStr ;
2630
2731 public static class TestObject {
32+ @ JsonIgnore
2833 private int field1 ;
34+ @ JsonIgnore
2935 private int field2 ;
3036
3137 @ JsonCreator
32- @ com .jsoniter .annotation .JsonCreator
3338 public TestObject (
34- @ JsonProperty ("field1" ) @ com . jsoniter . annotation . JsonProperty ( "field1" ) int field1 ,
35- @ JsonProperty ("field2" ) @ com . jsoniter . annotation . JsonProperty ( "field2" ) int field2 ) {
39+ @ JsonProperty ("field1" ) int field1 ,
40+ @ JsonProperty ("field2" ) int field2 ) {
3641 this .field1 = field1 ;
3742 this .field2 = field2 ;
3843 }
@@ -50,36 +55,35 @@ public String toString() {
5055 private JsonIterator iter ;
5156
5257 @ Setup (Level .Trial )
53- public void benchSetup () {
58+ public void benchSetup (BenchmarkParams params ) {
5459 inputStr = "{'field1':100,'field2':101}" ;
5560 input = inputStr .replace ('\'' , '"' ).getBytes ();
5661 iter = JsonIterator .parse (input );
5762 typeLiteral = new TypeLiteral <TestObject >() {
5863 };
5964 typeRef = new TypeReference <TestObject >() {
6065 };
61- JsoniterAnnotationSupport .enable ();
66+ JacksonAnnotationSupport .enable ();
6267 jackson = new ObjectMapper ();
6368 jackson .registerModule (new AfterburnerModule ());
69+ if (params != null ) {
70+ if (params .getBenchmark ().contains ("withJsoniterStrictMode" )) {
71+ JsonIterator .enableStrictMode ();
72+ }
73+ if (params .getBenchmark ().contains ("withJsoniterReflection" )) {
74+ ExtensionManager .registerTypeDecoder (TestObject .class , new ReflectionDecoder (TestObject .class ));
75+ }
76+ }
6477 }
6578
6679 @ Test
6780 public void test () throws IOException {
68- benchSetup ();
81+ benchSetup (null );
82+ ExtensionManager .registerTypeDecoder (TestObject .class , new ReflectionDecoder (TestObject .class ));
6983 System .out .println (withJsoniter ());
7084 System .out .println (withJackson ());
7185 }
7286
73- @ Benchmark
74- public void withJsoniter (Blackhole bh ) throws IOException {
75- bh .consume (withJsoniter ());
76- }
77-
78- @ Benchmark
79- public void withJackson (Blackhole bh ) throws IOException {
80- bh .consume (withJackson ());
81- }
82-
8387 public static void main (String [] args ) throws Exception {
8488 Main .main (new String []{
8589 "ConstructorBinding" ,
@@ -89,6 +93,26 @@ public static void main(String[] args) throws Exception {
8993 });
9094 }
9195
96+ // @Benchmark
97+ public void withJsoniterHashMode (Blackhole bh ) throws IOException {
98+ bh .consume (withJsoniter ());
99+ }
100+
101+ // @Benchmark
102+ public void withJsoniterStrictMode (Blackhole bh ) throws IOException {
103+ bh .consume (withJsoniter ());
104+ }
105+
106+ @ Benchmark
107+ public void withJsoniterReflection (Blackhole bh ) throws IOException {
108+ bh .consume (withJsoniter ());
109+ }
110+
111+ @ Benchmark
112+ public void withJackson (Blackhole bh ) throws IOException {
113+ bh .consume (withJackson ());
114+ }
115+
92116 private TestObject withJsoniter () throws IOException {
93117 iter .reset ();
94118 return iter .read (typeLiteral );
0 commit comments