|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +package com.azure.core.util.serializer; |
| 5 | + |
| 6 | +import com.azure.core.annotation.JsonFlatten; |
| 7 | +import com.fasterxml.jackson.annotation.JsonAnyGetter; |
| 8 | +import com.fasterxml.jackson.annotation.JsonAnySetter; |
| 9 | +import com.fasterxml.jackson.annotation.JsonProperty; |
| 10 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 11 | +import org.openjdk.jmh.Main; |
| 12 | +import org.openjdk.jmh.annotations.*; |
| 13 | +import org.openjdk.jmh.infra.Blackhole; |
| 14 | +import org.openjdk.jmh.runner.RunnerException; |
| 15 | + |
| 16 | +import java.io.IOException; |
| 17 | +import java.util.HashMap; |
| 18 | +import java.util.Map; |
| 19 | +import java.util.concurrent.TimeUnit; |
| 20 | + |
| 21 | +@Fork(3) |
| 22 | +@Warmup(iterations = 5, time = 2) |
| 23 | +@Measurement(iterations = 5, time = 10) |
| 24 | +@BenchmarkMode(Mode.AverageTime) |
| 25 | +@OutputTimeUnit(TimeUnit.NANOSECONDS) |
| 26 | +@State(Scope.Thread) |
| 27 | +public class SerializationBenchmark { |
| 28 | + private JacksonAdapter serializer; |
| 29 | + private ObjectMapper mapper; |
| 30 | + private OuterModel simpleModel; |
| 31 | + private OuterModel additionalPropertiesModel; |
| 32 | + private OuterModel jsonAnyModel; |
| 33 | + private OuterModelFlatten flattenModel; |
| 34 | + |
| 35 | + @Setup |
| 36 | + public void setup() { |
| 37 | + this.serializer = new JacksonAdapter(); |
| 38 | + this.mapper = new ObjectMapper(); |
| 39 | + this.simpleModel = new OuterModel("foo", "bar", "baz", Test.PLAIN); |
| 40 | + this.additionalPropertiesModel = new OuterModel("foo", "bar", "baz", Test.ADDITIONAL_PROPERTIES); |
| 41 | + this.jsonAnyModel = new OuterModel("foo", "bar", "baz", Test.JSON_ANY); |
| 42 | + this.flattenModel = new OuterModelFlatten("foo", "bar", "baz"); |
| 43 | + } |
| 44 | + |
| 45 | + enum Test { |
| 46 | + PLAIN, |
| 47 | + ADDITIONAL_PROPERTIES, |
| 48 | + JSON_ANY |
| 49 | + } |
| 50 | + |
| 51 | + class InnerModel { |
| 52 | + InnerModel(String foo, String bar, String baz) { |
| 53 | + this.foo = foo; |
| 54 | + this.bar = bar; |
| 55 | + this.baz = baz; |
| 56 | + } |
| 57 | + |
| 58 | + @JsonProperty |
| 59 | + private String foo; |
| 60 | + |
| 61 | + @JsonProperty |
| 62 | + private String bar; |
| 63 | + |
| 64 | + @JsonProperty |
| 65 | + private String baz; |
| 66 | + } |
| 67 | + |
| 68 | + class InnerModelAdditionalProperties { |
| 69 | + InnerModelAdditionalProperties(String foo, String bar, String baz) { |
| 70 | + this.additionalProperties = new HashMap<>(); |
| 71 | + additionalProperties.put("foo", foo); |
| 72 | + additionalProperties.put("bar", bar); |
| 73 | + additionalProperties.put("baz", baz); |
| 74 | + } |
| 75 | + |
| 76 | + @JsonProperty |
| 77 | + private Map<String, String> additionalProperties; |
| 78 | + } |
| 79 | + |
| 80 | + class InnerModelJsonAny { |
| 81 | + InnerModelJsonAny(String foo, String bar, String baz) { |
| 82 | + this.any = new HashMap<>(); |
| 83 | + any.put("foo", foo); |
| 84 | + any.put("bar", bar); |
| 85 | + any.put("baz", baz); |
| 86 | + } |
| 87 | + |
| 88 | + @JsonAnyGetter |
| 89 | + @JsonAnySetter |
| 90 | + private Map<String, String> any; |
| 91 | + } |
| 92 | + |
| 93 | + |
| 94 | + class MiddleModel { |
| 95 | + MiddleModel(String foo, String bar, String baz, Test test) { |
| 96 | + if (test == Test.PLAIN) { |
| 97 | + this.inner = new InnerModel(foo, bar, baz); |
| 98 | + } else if (test == Test.ADDITIONAL_PROPERTIES) { |
| 99 | + this.inner = new InnerModelAdditionalProperties(foo, bar, baz); |
| 100 | + } else if (test == Test.JSON_ANY) { |
| 101 | + this.inner = new InnerModelJsonAny(foo, bar, baz); |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + @JsonProperty() |
| 106 | + private Object inner; |
| 107 | + } |
| 108 | + |
| 109 | + class OuterModel { |
| 110 | + OuterModel(String foo, String bar, String baz, Test test) { |
| 111 | + this.middle = new MiddleModel(foo, bar, baz, test); |
| 112 | + } |
| 113 | + |
| 114 | + @JsonProperty() |
| 115 | + private MiddleModel middle; |
| 116 | + } |
| 117 | + |
| 118 | + @JsonFlatten |
| 119 | + class OuterModelFlatten { |
| 120 | + OuterModelFlatten(String foo, String bar, String baz) { |
| 121 | + this.foo = foo; |
| 122 | + this.bar = bar; |
| 123 | + this.baz = baz; |
| 124 | + } |
| 125 | + |
| 126 | + @JsonProperty("middle.inner.foo") |
| 127 | + private String foo; |
| 128 | + |
| 129 | + @JsonProperty("middle.inner.bar") |
| 130 | + private String bar; |
| 131 | + |
| 132 | + @JsonProperty("middle.inner.baz") |
| 133 | + private String baz; |
| 134 | + } |
| 135 | + |
| 136 | + @Benchmark |
| 137 | + public void simpleModelObjectMapperSerializeJson(Blackhole blackhole) throws IOException { |
| 138 | + blackhole.consume(this.mapper.writeValueAsString(this.simpleModel)); |
| 139 | + } |
| 140 | + |
| 141 | + @Benchmark |
| 142 | + public void simpleModelJacksonAdapterSerializeJson(Blackhole blackhole) throws IOException { |
| 143 | + blackhole.consume(this.serializer.serialize(this.simpleModel, SerializerEncoding.JSON)); |
| 144 | + } |
| 145 | + |
| 146 | + @Benchmark |
| 147 | + public void additionalPropertiesJacksonAdapterSerializeJson(Blackhole blackhole) throws IOException { |
| 148 | + blackhole.consume(this.serializer.serialize(this.additionalPropertiesModel, SerializerEncoding.JSON)); |
| 149 | + } |
| 150 | + |
| 151 | + @Benchmark |
| 152 | + public void flattenJacksonAdapterSerializeJson(Blackhole blackhole) throws IOException { |
| 153 | + blackhole.consume(this.serializer.serialize(this.flattenModel, SerializerEncoding.JSON)); |
| 154 | + } |
| 155 | + |
| 156 | + @Benchmark |
| 157 | + public void jsonAnyJacksonAdapterSerializeJson(Blackhole blackhole) throws IOException { |
| 158 | + blackhole.consume(this.serializer.serialize(this.jsonAnyModel, SerializerEncoding.JSON)); |
| 159 | + } |
| 160 | + |
| 161 | + @Benchmark |
| 162 | + public void simpleModelJacksonAdapterSerializeXml(Blackhole blackhole) throws IOException { |
| 163 | + blackhole.consume(this.serializer.serialize(this.simpleModel, SerializerEncoding.XML)); |
| 164 | + } |
| 165 | + |
| 166 | + public static void main(String... args) throws IOException, RunnerException { |
| 167 | + Main.main(args); |
| 168 | + } |
| 169 | +} |
0 commit comments