77import com .jsoniter .DecodingMode ;
88import com .jsoniter .JsonIterator ;
99import com .jsoniter .spi .TypeLiteral ;
10+ import com .squareup .moshi .JsonAdapter ;
11+ import com .squareup .moshi .Moshi ;
1012import org .junit .Test ;
1113import org .openjdk .jmh .Main ;
1214import org .openjdk .jmh .annotations .*;
@@ -29,6 +31,7 @@ public class ModelTest {
2931 private TypeLiteral <Model > modelTypeLiteral ; // this is thread-safe can reused
3032 private ObjectMapper jackson ;
3133 private TypeReference <Model > modelTypeReference ;
34+ private JsonAdapter <Model > moshiAdapter ;
3235
3336 @ Setup (Level .Trial )
3437 public void benchSetup (BenchmarkParams params ) {
@@ -43,6 +46,8 @@ public void benchSetup(BenchmarkParams params) {
4346 jackson .registerModule (new AfterburnerModule ());
4447 modelTypeReference = new TypeReference <Model >() {
4548 };
49+ Moshi moshi = new Moshi .Builder ().build ();
50+ moshiAdapter = moshi .adapter (Model .class );
4651 }
4752
4853 public static void main (String [] args ) throws IOException , RunnerException {
@@ -60,6 +65,7 @@ public void test() throws IOException {
6065 benchSetup (null );
6166 iter .reset (inputBytes );
6267 System .out .println (iter .read (modelTypeLiteral ).name );
68+ System .out .println (moshiAdapter .fromJson (input ).name );
6369 }
6470
6571// public static void main(String[] args) throws Exception {
@@ -90,14 +96,19 @@ public void jsoniter_easy_mode(Blackhole bh) throws IOException {
9096 bh .consume (JsonIterator .deserialize (inputBytes , Model .class ));
9197 }
9298
93- @ Benchmark
99+ // @Benchmark
94100 public void fastjson (Blackhole bh ) throws IOException {
95101 // this is not a exactly fair comparison,
96102 // as string => object is not
97103 // bytes => object
98104 bh .consume (JSON .parseObject (input , Model .class ));
99105 }
100106
107+ @ Benchmark
108+ public void moshi (Blackhole bh ) throws IOException {
109+ bh .consume (moshiAdapter .fromJson (input ));
110+ }
111+
101112// @Benchmark
102113 public void jackson (Blackhole bh ) throws IOException {
103114 bh .consume (jackson .readValue (inputBytes , modelTypeReference ));
0 commit comments