|
| 1 | +package com.alibaba.fastjson3.benchmark; |
| 2 | + |
| 3 | +import com.alibaba.fastjson3.JSONParser; |
| 4 | +import com.alibaba.fastjson3.JSONPath; |
| 5 | +import org.openjdk.jmh.annotations.*; |
| 6 | +import org.openjdk.jmh.infra.Blackhole; |
| 7 | +import org.openjdk.jmh.runner.Runner; |
| 8 | +import org.openjdk.jmh.runner.RunnerException; |
| 9 | +import org.openjdk.jmh.runner.options.Options; |
| 10 | +import org.openjdk.jmh.runner.options.OptionsBuilder; |
| 11 | + |
| 12 | +import java.lang.reflect.Type; |
| 13 | +import java.math.BigDecimal; |
| 14 | +import java.util.concurrent.TimeUnit; |
| 15 | + |
| 16 | +/** |
| 17 | + * Performance comparison: fastjson3 TypedMultiPath vs fastjson2 JSONPathTypedMulti. |
| 18 | + * |
| 19 | + * Run: |
| 20 | + * <pre> |
| 21 | + * mvn package -pl benchmark3 -DskipTests && \ |
| 22 | + * java -jar benchmark3/target/benchmarks.jar JSONPathTypedMultiBenchmark |
| 23 | + * </pre> |
| 24 | + */ |
| 25 | +@BenchmarkMode(Mode.Throughput) |
| 26 | +@OutputTimeUnit(TimeUnit.MILLISECONDS) |
| 27 | +@Warmup(iterations = 3, time = 3) |
| 28 | +@Measurement(iterations = 5, time = 3) |
| 29 | +@Fork(1) |
| 30 | +@State(Scope.Benchmark) |
| 31 | +public class JSONPathTypedMultiBenchmark { |
| 32 | + |
| 33 | + // ==================== Test data ==================== |
| 34 | + |
| 35 | + static final String SMALL_JSON = |
| 36 | + "{\"id\":1001,\"name\":\"DataWorks\",\"score\":99.5}"; |
| 37 | + |
| 38 | + static final String MEDIUM_JSON = |
| 39 | + "{\"id\":1001,\"name\":\"DataWorks\",\"score\":99.5," |
| 40 | + + "\"active\":true,\"version\":42,\"email\":\"[email protected]\"," |
| 41 | + + "\"phone\":\"+86-13800138000\",\"age\":25}"; |
| 42 | + |
| 43 | + static final String LARGE_JSON; |
| 44 | + static { |
| 45 | + StringBuilder sb = new StringBuilder("{"); |
| 46 | + for (int i = 0; i < 50; i++) { |
| 47 | + sb.append("\"padding_field_").append(i).append("\":\"value_").append(i).append("\","); |
| 48 | + } |
| 49 | + sb.append("\"id\":1001,\"name\":\"DataWorks\",\"score\":99.5}"); |
| 50 | + LARGE_JSON = sb.toString(); |
| 51 | + } |
| 52 | + |
| 53 | + static final String NESTED_JSON = |
| 54 | + "{\"data\":{\"id\":1001,\"name\":\"DataWorks\",\"score\":99.5}}"; |
| 55 | + |
| 56 | + static final String ARRAY_NESTED_JSON = |
| 57 | + "[{\"id\":1001,\"name\":\"DataWorks\",\"score\":99.5}]"; |
| 58 | + |
| 59 | + static final String INDEX_JSON = "[101,\"DataWorks\",99.5,true,42]"; |
| 60 | + |
| 61 | + static final byte[] SMALL_JSON_BYTES = SMALL_JSON.getBytes(); |
| 62 | + static final byte[] LARGE_JSON_BYTES = LARGE_JSON.getBytes(); |
| 63 | + |
| 64 | + // --- fastjson3 paths --- |
| 65 | + |
| 66 | + static final JSONPath FJ3_SMALL = JSONPath.of( |
| 67 | + new String[]{"$.id", "$.name", "$.score"}, |
| 68 | + new Type[]{Long.class, String.class, BigDecimal.class} |
| 69 | + ); |
| 70 | + |
| 71 | + static final JSONPath FJ3_MEDIUM = JSONPath.of( |
| 72 | + new String[]{"$.id", "$.name", "$.score", "$.active", "$.version", |
| 73 | + "$.email", "$.phone", "$.age"}, |
| 74 | + new Type[]{Long.class, String.class, BigDecimal.class, Boolean.class, |
| 75 | + Long.class, String.class, String.class, Integer.class} |
| 76 | + ); |
| 77 | + |
| 78 | + static final JSONPath FJ3_LARGE = JSONPath.of( |
| 79 | + new String[]{"$.id", "$.name", "$.score"}, |
| 80 | + new Type[]{Long.class, String.class, BigDecimal.class} |
| 81 | + ); |
| 82 | + |
| 83 | + static final JSONPath FJ3_NESTED = JSONPath.of( |
| 84 | + new String[]{"$.data.id", "$.data.name", "$.data.score"}, |
| 85 | + new Type[]{Long.class, String.class, BigDecimal.class} |
| 86 | + ); |
| 87 | + |
| 88 | + static final JSONPath FJ3_ARRAY_NESTED = JSONPath.of( |
| 89 | + new String[]{"$[0].id", "$[0].name", "$[0].score"}, |
| 90 | + new Type[]{Long.class, String.class, BigDecimal.class} |
| 91 | + ); |
| 92 | + |
| 93 | + static final JSONPath FJ3_INDEX = JSONPath.of( |
| 94 | + new String[]{"$[0]", "$[1]", "$[2]"}, |
| 95 | + new Type[]{Long.class, String.class, BigDecimal.class} |
| 96 | + ); |
| 97 | + |
| 98 | + // --- fastjson2 paths --- |
| 99 | + |
| 100 | + static final com.alibaba.fastjson2.JSONPath FJ2_SMALL = com.alibaba.fastjson2.JSONPath.of( |
| 101 | + new String[]{"$.id", "$.name", "$.score"}, |
| 102 | + new Type[]{Long.class, String.class, BigDecimal.class} |
| 103 | + ); |
| 104 | + |
| 105 | + static final com.alibaba.fastjson2.JSONPath FJ2_MEDIUM = com.alibaba.fastjson2.JSONPath.of( |
| 106 | + new String[]{"$.id", "$.name", "$.score", "$.active", "$.version", |
| 107 | + "$.email", "$.phone", "$.age"}, |
| 108 | + new Type[]{Long.class, String.class, BigDecimal.class, Boolean.class, |
| 109 | + Long.class, String.class, String.class, Integer.class} |
| 110 | + ); |
| 111 | + |
| 112 | + static final com.alibaba.fastjson2.JSONPath FJ2_LARGE = com.alibaba.fastjson2.JSONPath.of( |
| 113 | + new String[]{"$.id", "$.name", "$.score"}, |
| 114 | + new Type[]{Long.class, String.class, BigDecimal.class} |
| 115 | + ); |
| 116 | + |
| 117 | + static final com.alibaba.fastjson2.JSONPath FJ2_NESTED = com.alibaba.fastjson2.JSONPath.of( |
| 118 | + new String[]{"$.data.id", "$.data.name", "$.data.score"}, |
| 119 | + new Type[]{Long.class, String.class, BigDecimal.class} |
| 120 | + ); |
| 121 | + |
| 122 | + static final com.alibaba.fastjson2.JSONPath FJ2_ARRAY_NESTED = com.alibaba.fastjson2.JSONPath.of( |
| 123 | + new String[]{"$[0].id", "$[0].name", "$[0].score"}, |
| 124 | + new Type[]{Long.class, String.class, BigDecimal.class} |
| 125 | + ); |
| 126 | + |
| 127 | + static final com.alibaba.fastjson2.JSONPath FJ2_INDEX = com.alibaba.fastjson2.JSONPath.of( |
| 128 | + new String[]{"$[0]", "$[1]", "$[2]"}, |
| 129 | + new Type[]{Long.class, String.class, BigDecimal.class} |
| 130 | + ); |
| 131 | + |
| 132 | + // ==================== SingleName: 3 fields, small JSON ==================== |
| 133 | + |
| 134 | + @Benchmark |
| 135 | + public void singleName_small_fj3(Blackhole bh) { |
| 136 | + bh.consume(FJ3_SMALL.extract(SMALL_JSON)); |
| 137 | + } |
| 138 | + |
| 139 | + @Benchmark |
| 140 | + public void singleName_small_fj2(Blackhole bh) { |
| 141 | + bh.consume(FJ2_SMALL.extract(SMALL_JSON)); |
| 142 | + } |
| 143 | + |
| 144 | + // ==================== SingleName: 8 fields, medium JSON ==================== |
| 145 | + |
| 146 | + @Benchmark |
| 147 | + public void singleName_medium_fj3(Blackhole bh) { |
| 148 | + bh.consume(FJ3_MEDIUM.extract(MEDIUM_JSON)); |
| 149 | + } |
| 150 | + |
| 151 | + @Benchmark |
| 152 | + public void singleName_medium_fj2(Blackhole bh) { |
| 153 | + bh.consume(FJ2_MEDIUM.extract(MEDIUM_JSON)); |
| 154 | + } |
| 155 | + |
| 156 | + // ==================== SingleName: 3 fields, large JSON (50 padding fields) ==================== |
| 157 | + |
| 158 | + @Benchmark |
| 159 | + public void singleName_large_fj3(Blackhole bh) { |
| 160 | + bh.consume(FJ3_LARGE.extract(LARGE_JSON)); |
| 161 | + } |
| 162 | + |
| 163 | + @Benchmark |
| 164 | + public void singleName_large_fj2(Blackhole bh) { |
| 165 | + bh.consume(FJ2_LARGE.extract(LARGE_JSON)); |
| 166 | + } |
| 167 | + |
| 168 | + // ==================== SingleName: small JSON, byte[] input ==================== |
| 169 | + |
| 170 | + @Benchmark |
| 171 | + public void singleName_bytes_fj3(Blackhole bh) { |
| 172 | + try (JSONParser parser = JSONParser.of(SMALL_JSON_BYTES)) { |
| 173 | + bh.consume(FJ3_SMALL.extract(parser)); |
| 174 | + } |
| 175 | + } |
| 176 | + |
| 177 | + @Benchmark |
| 178 | + public void singleName_bytes_fj2(Blackhole bh) { |
| 179 | + bh.consume(FJ2_SMALL.extract( |
| 180 | + com.alibaba.fastjson2.JSONReader.of(SMALL_JSON_BYTES))); |
| 181 | + } |
| 182 | + |
| 183 | + // ==================== PrefixName: $.data.x ==================== |
| 184 | + |
| 185 | + @Benchmark |
| 186 | + public void prefixName_fj3(Blackhole bh) { |
| 187 | + bh.consume(FJ3_NESTED.extract(NESTED_JSON)); |
| 188 | + } |
| 189 | + |
| 190 | + @Benchmark |
| 191 | + public void prefixName_fj2(Blackhole bh) { |
| 192 | + bh.consume(FJ2_NESTED.extract(NESTED_JSON)); |
| 193 | + } |
| 194 | + |
| 195 | + // ==================== PrefixIndex: $[0].x ==================== |
| 196 | + |
| 197 | + @Benchmark |
| 198 | + public void prefixIndex_fj3(Blackhole bh) { |
| 199 | + bh.consume(FJ3_ARRAY_NESTED.extract(ARRAY_NESTED_JSON)); |
| 200 | + } |
| 201 | + |
| 202 | + @Benchmark |
| 203 | + public void prefixIndex_fj2(Blackhole bh) { |
| 204 | + bh.consume(FJ2_ARRAY_NESTED.extract(ARRAY_NESTED_JSON)); |
| 205 | + } |
| 206 | + |
| 207 | + // ==================== SingleIndex: $[0], $[1], $[2] ==================== |
| 208 | + |
| 209 | + @Benchmark |
| 210 | + public void singleIndex_fj3(Blackhole bh) { |
| 211 | + bh.consume(FJ3_INDEX.extract(INDEX_JSON)); |
| 212 | + } |
| 213 | + |
| 214 | + @Benchmark |
| 215 | + public void singleIndex_fj2(Blackhole bh) { |
| 216 | + bh.consume(FJ2_INDEX.extract(INDEX_JSON)); |
| 217 | + } |
| 218 | + |
| 219 | + // ==================== Runner ==================== |
| 220 | + |
| 221 | + public static void main(String[] args) throws RunnerException { |
| 222 | + Options opt = new OptionsBuilder() |
| 223 | + .include(JSONPathTypedMultiBenchmark.class.getSimpleName()) |
| 224 | + .build(); |
| 225 | + new Runner(opt).run(); |
| 226 | + } |
| 227 | +} |
0 commit comments