|
| 1 | +package com.alibaba.fastjson2.benchmark.reflect; |
| 2 | + |
| 3 | +import com.alibaba.fastjson2.reflect.PropertyAccessor; |
| 4 | +import com.alibaba.fastjson2.reflect.PropertyAccessorFactory; |
| 5 | +import com.alibaba.fastjson2.reflect.PropertyAccessorFactoryMethodHandle; |
| 6 | +import com.alibaba.fastjson2.reflect.PropertyAccessorFactoryUnsafe; |
| 7 | +import com.alibaba.fastjson2.reflect.PropertyAccessorFactoryVarHandle; |
| 8 | +import lombok.Getter; |
| 9 | +import lombok.Setter; |
| 10 | +import org.openjdk.jmh.annotations.*; |
| 11 | +import org.openjdk.jmh.infra.Blackhole; |
| 12 | +import org.openjdk.jmh.runner.Runner; |
| 13 | +import org.openjdk.jmh.runner.options.Options; |
| 14 | +import org.openjdk.jmh.runner.options.OptionsBuilder; |
| 15 | + |
| 16 | +import java.util.concurrent.TimeUnit; |
| 17 | + |
| 18 | +@BenchmarkMode(Mode.Throughput) |
| 19 | +@OutputTimeUnit(TimeUnit.MILLISECONDS) |
| 20 | +@State(Scope.Benchmark) |
| 21 | +public class PropertyAccessorBenchmark { |
| 22 | + // Test class with fields and methods of different types |
| 23 | + @Getter |
| 24 | + @Setter |
| 25 | + public static class TestClass { |
| 26 | + private int intField = 1000; |
| 27 | + private String stringField = "test"; |
| 28 | + private boolean booleanField = true; |
| 29 | + private long longField = 10000L; |
| 30 | + private float floatField = 30.5f; |
| 31 | + private double doubleField = 20.7; |
| 32 | + |
| 33 | + // Wrapper type fields |
| 34 | + private Integer intObjField = 1000; |
| 35 | + private String stringObjField = "test"; |
| 36 | + private Boolean booleanObjField = true; |
| 37 | + private Long longObjField = 10000L; |
| 38 | + private Float floatObjField = 30.5f; |
| 39 | + private Double doubleObjField = 20.7; |
| 40 | + |
| 41 | + // Additional primitive types |
| 42 | + private byte byteField = 10; |
| 43 | + private short shortField = 100; |
| 44 | + private char charField = 'A'; |
| 45 | + |
| 46 | + // Wrapper types |
| 47 | + private Byte byteObjField = 10; |
| 48 | + private Short shortObjField = 100; |
| 49 | + private Character charObjField = 'A'; |
| 50 | + } |
| 51 | + |
| 52 | + @Param({ |
| 53 | + "reflect", |
| 54 | + "unsafe & lambda", |
| 55 | + "MethodHandle", |
| 56 | + "VarHandle & lambda" |
| 57 | + }) |
| 58 | + public String factory; |
| 59 | + private PropertyAccessorFactory propertyAccessorFactory; |
| 60 | + private TestClass testObject; |
| 61 | + |
| 62 | + private PropertyAccessor |
| 63 | + byteFieldAccessor, |
| 64 | + byteGetterAccessor, |
| 65 | + byteSetterAccessor, |
| 66 | + shortFieldAccessor, |
| 67 | + shortGetterAccessor, |
| 68 | + shortSetterAccessor, |
| 69 | + charFieldAccessor, |
| 70 | + charGetterAccessor, |
| 71 | + charSetterAccessor, |
| 72 | + intFieldAccessor, |
| 73 | + intGetterAccessor, |
| 74 | + intSetterAccessor, |
| 75 | + longFieldAccessor, |
| 76 | + longGetterAccessor, |
| 77 | + longSetterAccessor, |
| 78 | + booleanFieldAccessor, |
| 79 | + booleanGetterAccessor, |
| 80 | + booleanSetterAccessor, |
| 81 | + floatFieldAccessor, |
| 82 | + floatGetterAccessor, |
| 83 | + floatSetterAccessor, |
| 84 | + doubleFieldAccessor, |
| 85 | + doubleGetterAccessor, |
| 86 | + doubleSetterAccessor; |
| 87 | + |
| 88 | + @Setup |
| 89 | + public void setup() throws Exception { |
| 90 | + propertyAccessorFactory = switch (factory) { |
| 91 | + case "unsafe & lambda" -> new PropertyAccessorFactoryUnsafe(); |
| 92 | + case "MethodHandle" -> new PropertyAccessorFactoryMethodHandle(); |
| 93 | + case "VarHandle & lambda" -> new PropertyAccessorFactoryVarHandle(); |
| 94 | + default -> new PropertyAccessorFactory(); |
| 95 | + }; |
| 96 | + |
| 97 | + Class<TestClass> clazz = TestClass.class; |
| 98 | + |
| 99 | + // Byte field accessors |
| 100 | + byteFieldAccessor = propertyAccessorFactory.create( |
| 101 | + clazz.getDeclaredField("byteField") |
| 102 | + ); |
| 103 | + byteGetterAccessor = propertyAccessorFactory.create( |
| 104 | + clazz.getDeclaredMethod("getByteField") |
| 105 | + ); |
| 106 | + byteSetterAccessor = propertyAccessorFactory.create( |
| 107 | + clazz.getDeclaredMethod("setByteField", byte.class)); |
| 108 | + |
| 109 | + // Short field accessors |
| 110 | + shortFieldAccessor = propertyAccessorFactory.create( |
| 111 | + clazz.getDeclaredField("shortField") |
| 112 | + ); |
| 113 | + shortGetterAccessor = propertyAccessorFactory.create( |
| 114 | + clazz.getDeclaredMethod("getShortField") |
| 115 | + ); |
| 116 | + shortSetterAccessor = propertyAccessorFactory.create( |
| 117 | + clazz.getDeclaredMethod("setShortField", short.class)); |
| 118 | + |
| 119 | + // Char field accessors |
| 120 | + charFieldAccessor = propertyAccessorFactory.create( |
| 121 | + clazz.getDeclaredField("charField") |
| 122 | + ); |
| 123 | + charGetterAccessor = propertyAccessorFactory.create( |
| 124 | + clazz.getDeclaredMethod("getCharField") |
| 125 | + ); |
| 126 | + charSetterAccessor = propertyAccessorFactory.create( |
| 127 | + clazz.getDeclaredMethod("setCharField", char.class)); |
| 128 | + |
| 129 | + // Int field accessors |
| 130 | + intFieldAccessor = propertyAccessorFactory.create( |
| 131 | + clazz.getDeclaredField("intField") |
| 132 | + ); |
| 133 | + intGetterAccessor = propertyAccessorFactory.create( |
| 134 | + clazz.getDeclaredMethod("getIntField") |
| 135 | + ); |
| 136 | + intSetterAccessor = propertyAccessorFactory.create( |
| 137 | + clazz.getDeclaredMethod("setIntField", int.class)); |
| 138 | + |
| 139 | + // Long field accessors |
| 140 | + longFieldAccessor = propertyAccessorFactory.create( |
| 141 | + clazz.getDeclaredField("longField") |
| 142 | + ); |
| 143 | + longGetterAccessor = propertyAccessorFactory.create( |
| 144 | + clazz.getDeclaredMethod("getLongField") |
| 145 | + ); |
| 146 | + longSetterAccessor = propertyAccessorFactory.create( |
| 147 | + clazz.getDeclaredMethod("setLongField", long.class)); |
| 148 | + |
| 149 | + // Boolean field accessors |
| 150 | + booleanFieldAccessor = propertyAccessorFactory.create( |
| 151 | + clazz.getDeclaredField("booleanField") |
| 152 | + ); |
| 153 | + booleanGetterAccessor = propertyAccessorFactory.create( |
| 154 | + clazz.getDeclaredMethod("isBooleanField") |
| 155 | + ); |
| 156 | + booleanSetterAccessor = propertyAccessorFactory.create( |
| 157 | + clazz.getDeclaredMethod("setBooleanField", boolean.class)); |
| 158 | + |
| 159 | + // Float field accessors |
| 160 | + floatFieldAccessor = propertyAccessorFactory.create( |
| 161 | + clazz.getDeclaredField("floatField") |
| 162 | + ); |
| 163 | + floatGetterAccessor = propertyAccessorFactory.create( |
| 164 | + clazz.getDeclaredMethod("getFloatField") |
| 165 | + ); |
| 166 | + floatSetterAccessor = propertyAccessorFactory.create( |
| 167 | + clazz.getDeclaredMethod("setFloatField", float.class)); |
| 168 | + |
| 169 | + // Double field accessors |
| 170 | + doubleFieldAccessor = propertyAccessorFactory.create( |
| 171 | + clazz.getDeclaredField("doubleField") |
| 172 | + ); |
| 173 | + doubleGetterAccessor = propertyAccessorFactory.create( |
| 174 | + clazz.getDeclaredMethod("getDoubleField") |
| 175 | + ); |
| 176 | + doubleSetterAccessor = propertyAccessorFactory.create( |
| 177 | + clazz.getDeclaredMethod("setDoubleField", double.class)); |
| 178 | + |
| 179 | + testObject = new TestClass(); |
| 180 | + } |
| 181 | + |
| 182 | + @Benchmark |
| 183 | + public void fieldGet(Blackhole bh) { |
| 184 | + bh.consume(byteFieldAccessor.getByteValue(testObject)); |
| 185 | + bh.consume(shortFieldAccessor.getShortValue(testObject)); |
| 186 | + bh.consume(charFieldAccessor.getCharValue(testObject)); |
| 187 | + bh.consume(intFieldAccessor.getIntValue(testObject)); |
| 188 | + bh.consume(longFieldAccessor.getLongValue(testObject)); |
| 189 | + bh.consume(booleanFieldAccessor.getBooleanValue(testObject)); |
| 190 | + bh.consume(floatFieldAccessor.getFloatValue(testObject)); |
| 191 | + bh.consume(doubleFieldAccessor.getDoubleValue(testObject)); |
| 192 | + } |
| 193 | + |
| 194 | + @Benchmark |
| 195 | + public void fieldSet(Blackhole bh) { |
| 196 | + byteFieldAccessor.setByteValue(testObject, (byte) 30); |
| 197 | + shortFieldAccessor.setShortValue(testObject, (short) 30); |
| 198 | + charFieldAccessor.setCharValue(testObject, (char) 65); // ASCII for 'A' |
| 199 | + intFieldAccessor.setLongValue(testObject, 30); |
| 200 | + longFieldAccessor.setLongValue(testObject, 30L); |
| 201 | + booleanFieldAccessor.setBooleanValue(testObject, true); |
| 202 | + floatFieldAccessor.setFloatValue(testObject, 30.5f); |
| 203 | + doubleFieldAccessor.setDoubleValue(testObject, 30.5); |
| 204 | + bh.consume(testObject); |
| 205 | + } |
| 206 | + |
| 207 | + @Benchmark |
| 208 | + public void getter(Blackhole bh) { |
| 209 | + bh.consume(byteGetterAccessor.getByteValue(testObject)); |
| 210 | + bh.consume(shortGetterAccessor.getShortValue(testObject)); |
| 211 | + bh.consume(charGetterAccessor.getCharValue(testObject)); |
| 212 | + bh.consume(intGetterAccessor.getIntValue(testObject)); |
| 213 | + bh.consume(longGetterAccessor.getLongValue(testObject)); |
| 214 | + bh.consume(booleanGetterAccessor.getBooleanValue(testObject)); |
| 215 | + bh.consume(floatGetterAccessor.getFloatValue(testObject)); |
| 216 | + bh.consume(doubleGetterAccessor.getDoubleValue(testObject)); |
| 217 | + } |
| 218 | + |
| 219 | + @Benchmark |
| 220 | + public void setter(Blackhole bh) { |
| 221 | + byteSetterAccessor.setByteValue(testObject, (byte) 30); |
| 222 | + shortSetterAccessor.setShortValue(testObject, (short) 30); |
| 223 | + charSetterAccessor.setCharValue(testObject, (char) 65); // ASCII for 'A' |
| 224 | + intSetterAccessor.setIntValue(testObject, 30); |
| 225 | + longSetterAccessor.setLongValue(testObject, 30L); |
| 226 | + booleanSetterAccessor.setBooleanValue(testObject, true); |
| 227 | + floatSetterAccessor.setFloatValue(testObject, 30.5f); |
| 228 | + doubleSetterAccessor.setDoubleValue(testObject, 30.5); |
| 229 | + bh.consume(testObject); |
| 230 | + } |
| 231 | + |
| 232 | + public static void main(String[] args) throws Exception { |
| 233 | + Options options = new OptionsBuilder() |
| 234 | + .include(PropertyAccessorBenchmark.class.getSimpleName()) |
| 235 | + .warmupIterations(1) |
| 236 | + .measurementIterations(1) |
| 237 | + .forks(1) |
| 238 | + .build(); |
| 239 | + new Runner(options).run(); |
| 240 | + } |
| 241 | +} |
0 commit comments