|
| 1 | +package com.jsoniter.demo.object_with_1_double_field; |
| 2 | + |
| 3 | +import com.fasterxml.jackson.core.type.TypeReference; |
| 4 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 5 | +import com.fasterxml.jackson.module.afterburner.AfterburnerModule; |
| 6 | +import org.openjdk.jmh.Main; |
| 7 | +import org.openjdk.jmh.annotations.*; |
| 8 | +import org.openjdk.jmh.infra.BenchmarkParams; |
| 9 | +import org.openjdk.jmh.infra.Blackhole; |
| 10 | +import org.openjdk.jmh.runner.RunnerException; |
| 11 | + |
| 12 | +import java.io.ByteArrayOutputStream; |
| 13 | +import java.io.IOException; |
| 14 | + |
| 15 | +/* |
| 16 | +Benchmark Mode Cnt Score Error Units |
| 17 | +BenchJackson.deser thrpt 5 5822021.435 ± 208030.778 ops/s |
| 18 | +BenchJackson.ser thrpt 5 5359413.928 ± 180612.520 ops/s |
| 19 | + */ |
| 20 | +@State(Scope.Thread) |
| 21 | +public class BenchJackson { |
| 22 | + |
| 23 | + private ObjectMapper objectMapper; |
| 24 | + private TypeReference<TestObject> typeReference; |
| 25 | + private ByteArrayOutputStream byteArrayOutputStream; |
| 26 | + private byte[] testJSON; |
| 27 | + private TestObject testObject; |
| 28 | + |
| 29 | + @Setup(Level.Trial) |
| 30 | + public void benchSetup(BenchmarkParams params) { |
| 31 | + objectMapper = new ObjectMapper(); |
| 32 | + objectMapper.registerModule(new AfterburnerModule()); |
| 33 | + typeReference = new TypeReference<TestObject>() { |
| 34 | + }; |
| 35 | + byteArrayOutputStream = new ByteArrayOutputStream(); |
| 36 | + testJSON = TestObject.createTestJSON(); |
| 37 | + testObject = TestObject.createTestObject(); |
| 38 | + } |
| 39 | + |
| 40 | + @Benchmark |
| 41 | + public void ser(Blackhole bh) throws IOException { |
| 42 | + byteArrayOutputStream.reset(); |
| 43 | + objectMapper.writeValue(byteArrayOutputStream, testObject); |
| 44 | + bh.consume(byteArrayOutputStream); |
| 45 | + } |
| 46 | + |
| 47 | + |
| 48 | + @Benchmark |
| 49 | + public void deser(Blackhole bh) throws IOException { |
| 50 | + bh.consume(objectMapper.readValue(testJSON, typeReference)); |
| 51 | + } |
| 52 | + |
| 53 | + public static void main(String[] args) throws IOException, RunnerException { |
| 54 | + Main.main(new String[]{ |
| 55 | + "object_with_1_double_field.BenchJackson", |
| 56 | + "-i", "5", |
| 57 | + "-wi", "5", |
| 58 | + "-f", "1", |
| 59 | + }); |
| 60 | + } |
| 61 | +} |
0 commit comments