|
| 1 | +/* |
| 2 | + * Copyright 2002-present the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.cglib.core; |
| 18 | + |
| 19 | +import java.util.concurrent.TimeUnit; |
| 20 | + |
| 21 | +import org.openjdk.jmh.annotations.Benchmark; |
| 22 | +import org.openjdk.jmh.annotations.BenchmarkMode; |
| 23 | +import org.openjdk.jmh.annotations.Level; |
| 24 | +import org.openjdk.jmh.annotations.Measurement; |
| 25 | +import org.openjdk.jmh.annotations.Mode; |
| 26 | +import org.openjdk.jmh.annotations.OutputTimeUnit; |
| 27 | +import org.openjdk.jmh.annotations.Scope; |
| 28 | +import org.openjdk.jmh.annotations.Setup; |
| 29 | +import org.openjdk.jmh.annotations.State; |
| 30 | +import org.openjdk.jmh.annotations.Warmup; |
| 31 | + |
| 32 | +import org.springframework.asm.ClassReader; |
| 33 | +import org.springframework.cglib.proxy.Enhancer; |
| 34 | +import org.springframework.cglib.proxy.MethodInterceptor; |
| 35 | + |
| 36 | +/** |
| 37 | + * Benchmarks for {@link ClassNameReader#getClassName(ClassReader)}. |
| 38 | + * Comparing the legacy {@code getClassInfo(r)[0]} delegation against a direct {@code ClassReader.getClassName()} call. |
| 39 | + * |
| 40 | + * @author Daehyeon Kim |
| 41 | + */ |
| 42 | +public class ClassNameReaderBenchmark { |
| 43 | + |
| 44 | + @BenchmarkMode(Mode.AverageTime) |
| 45 | + @OutputTimeUnit(TimeUnit.NANOSECONDS) |
| 46 | + @Warmup(iterations = 5, time = 2) |
| 47 | + @Measurement(iterations = 5, time = 2) |
| 48 | + public static class WithoutInterfaces { |
| 49 | + |
| 50 | + @State(Scope.Benchmark) |
| 51 | + public static class BenchmarkState { |
| 52 | + |
| 53 | + public ClassReader classReader; |
| 54 | + |
| 55 | + @Setup(Level.Trial) |
| 56 | + public void setup() { |
| 57 | + this.classReader = generateProxyReader(new Class<?>[0]); |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + @Benchmark |
| 62 | + public String viaGetClassInfo(BenchmarkState state) { |
| 63 | + return ClassNameReader.getClassInfo(state.classReader)[0]; |
| 64 | + } |
| 65 | + |
| 66 | + @Benchmark |
| 67 | + public String viaGetClassName(BenchmarkState state) { |
| 68 | + return state.classReader.getClassName().replace('/', '.'); |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + @BenchmarkMode(Mode.AverageTime) |
| 73 | + @OutputTimeUnit(TimeUnit.NANOSECONDS) |
| 74 | + @Warmup(iterations = 5, time = 2) |
| 75 | + @Measurement(iterations = 5, time = 2) |
| 76 | + public static class WithInterfaces { |
| 77 | + |
| 78 | + @State(Scope.Benchmark) |
| 79 | + public static class BenchmarkState { |
| 80 | + |
| 81 | + public ClassReader classReader; |
| 82 | + |
| 83 | + @Setup(Level.Trial) |
| 84 | + public void setup() { |
| 85 | + this.classReader = generateProxyReader( |
| 86 | + new Class<?>[]{Cloneable.class, Comparable.class, Runnable.class} |
| 87 | + ); |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + @Benchmark |
| 92 | + public String viaGetClassInfo(BenchmarkState state) { |
| 93 | + return ClassNameReader.getClassInfo(state.classReader)[0]; |
| 94 | + } |
| 95 | + |
| 96 | + @Benchmark |
| 97 | + public String viaGetClassName(BenchmarkState state) { |
| 98 | + return state.classReader.getClassName().replace('/', '.'); |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + private static ClassReader generateProxyReader(Class<?>[] interfaces) { |
| 103 | + CapturingGeneratorStrategy strategy = new CapturingGeneratorStrategy(); |
| 104 | + Enhancer enhancer = new Enhancer(); |
| 105 | + enhancer.setSuperclass(ProxyTarget.class); |
| 106 | + enhancer.setCallbackType(MethodInterceptor.class); |
| 107 | + enhancer.setUseCache(false); |
| 108 | + enhancer.setInterfaces(interfaces); |
| 109 | + enhancer.setStrategy(strategy); |
| 110 | + enhancer.createClass(); |
| 111 | + return strategy.createClassReader(); |
| 112 | + } |
| 113 | + |
| 114 | + public static class ProxyTarget { |
| 115 | + } |
| 116 | + |
| 117 | + private static class CapturingGeneratorStrategy implements GeneratorStrategy { |
| 118 | + |
| 119 | + private static final GeneratorStrategy delegate = DefaultGeneratorStrategy.INSTANCE; |
| 120 | + |
| 121 | + private byte[] captured; |
| 122 | + |
| 123 | + @Override |
| 124 | + public byte[] generate(ClassGenerator cg) throws Exception { |
| 125 | + this.captured = delegate.generate(cg); |
| 126 | + return this.captured; |
| 127 | + } |
| 128 | + |
| 129 | + private ClassReader createClassReader() { |
| 130 | + return new ClassReader(this.captured); |
| 131 | + } |
| 132 | + } |
| 133 | +} |
0 commit comments