|
| 1 | +package com.mapk.fastkfunction |
| 2 | + |
| 3 | +import com.mapk.fastkfunction.argumentbucket.ArgumentBucket |
| 4 | +import com.mapk.fastkfunction.benchmarktargets.Constructor5 |
| 5 | +import org.openjdk.jmh.annotations.Benchmark |
| 6 | +import org.openjdk.jmh.annotations.Scope |
| 7 | +import org.openjdk.jmh.annotations.State |
| 8 | +import java.lang.reflect.Method |
| 9 | +import kotlin.reflect.KFunction |
| 10 | +import kotlin.reflect.KParameter |
| 11 | +import kotlin.reflect.full.companionObjectInstance |
| 12 | +import kotlin.reflect.full.functions |
| 13 | +import kotlin.reflect.jvm.javaMethod |
| 14 | + |
| 15 | +@State(Scope.Benchmark) |
| 16 | +open class CallObjectMethodBenchmark { |
| 17 | + private val objectInstance = Constructor5::class.companionObjectInstance!! |
| 18 | + |
| 19 | + private val functionByMethodReference: KFunction<Constructor5> = (Constructor5)::companionObjectFun5 |
| 20 | + @Suppress("UNCHECKED_CAST") |
| 21 | + private val functionByReflection: KFunction<Constructor5> = objectInstance::class |
| 22 | + .functions |
| 23 | + .first { it.name == "companionObjectFun5" } as KFunction<Constructor5> |
| 24 | + |
| 25 | + private val argumentMap: Map<KParameter, Any?> = functionByMethodReference.parameters.associateWith { it.index + 1 } |
| 26 | + |
| 27 | + private val javaMethod: Method = functionByMethodReference.javaMethod!! |
| 28 | + |
| 29 | + private val fastKFunctionByMethodReferenceWithoutInstance: FastKFunction<Constructor5> = |
| 30 | + FastKFunction(functionByMethodReference) |
| 31 | + private val fastKFunctionByMethodReferenceWithInstance: FastKFunction<Constructor5> = |
| 32 | + FastKFunction(functionByMethodReference, objectInstance) |
| 33 | + |
| 34 | + private val fastKFunctionByReflectionWithoutInstance: FastKFunction<Constructor5> = |
| 35 | + FastKFunction(functionByReflection) |
| 36 | + private val fastKFunctionByReflectionWithInstance: FastKFunction<Constructor5> = |
| 37 | + FastKFunction(functionByReflection, objectInstance) |
| 38 | + |
| 39 | + private val argumentBucket: ArgumentBucket = fastKFunctionByMethodReferenceWithoutInstance.generateBucket() |
| 40 | + .apply { (0 until 5).forEach { this[it] = it + 1 } } |
| 41 | + |
| 42 | + @Benchmark |
| 43 | + fun normalCall(): Constructor5 = Constructor5.companionObjectFun5(1, 2, 3, 4, 5) |
| 44 | + |
| 45 | + @Benchmark |
| 46 | + fun functionByMethodReferenceCall(): Constructor5 = functionByMethodReference.call(1, 2, 3, 4, 5) |
| 47 | + |
| 48 | + @Benchmark |
| 49 | + fun functionByMethodReferenceCallBy(): Constructor5 = functionByMethodReference.callBy(argumentMap) |
| 50 | + |
| 51 | + @Benchmark |
| 52 | + fun functionByReflectionCall(): Constructor5 = functionByReflection.call(1, 2, 3, 4, 5) |
| 53 | + |
| 54 | + @Benchmark |
| 55 | + fun functionByReflectionCallBy(): Constructor5 = functionByReflection.callBy(argumentMap) |
| 56 | + |
| 57 | + @Benchmark |
| 58 | + fun javaMethod(): Constructor5 = javaMethod.invoke(objectInstance, 1, 2, 3, 4, 5) as Constructor5 |
| 59 | + |
| 60 | + @Benchmark |
| 61 | + fun fastKFunctionByMethodReferenceWithoutInstanceCall(): Constructor5 = |
| 62 | + fastKFunctionByMethodReferenceWithoutInstance.call(1, 2, 3, 4, 5) |
| 63 | + |
| 64 | + @Benchmark |
| 65 | + fun fastKFunctionByMethodReferenceWithoutInstanceCallBy(): Constructor5 = |
| 66 | + fastKFunctionByMethodReferenceWithoutInstance.callBy(argumentBucket) |
| 67 | + |
| 68 | + @Benchmark |
| 69 | + fun fastKFunctionByMethodReferenceWithInstanceCall(): Constructor5 = |
| 70 | + fastKFunctionByMethodReferenceWithInstance.call(1, 2, 3, 4, 5) |
| 71 | + |
| 72 | + @Benchmark |
| 73 | + fun fastKFunctionByMethodReferenceWithInstanceCallBy(): Constructor5 = |
| 74 | + fastKFunctionByMethodReferenceWithInstance.callBy(argumentBucket) |
| 75 | + |
| 76 | + @Benchmark |
| 77 | + fun fastKFunctionByReflectionWithoutInstanceCall(): Constructor5 = |
| 78 | + fastKFunctionByReflectionWithoutInstance.call(1, 2, 3, 4, 5) |
| 79 | + |
| 80 | + @Benchmark |
| 81 | + fun fastKFunctionByReflectionWithoutInstanceCallBy(): Constructor5 = |
| 82 | + fastKFunctionByReflectionWithoutInstance.callBy(argumentBucket) |
| 83 | + |
| 84 | + @Benchmark |
| 85 | + fun fastKFunctionByReflectionWithInstanceCall(): Constructor5 = |
| 86 | + fastKFunctionByReflectionWithInstance.call(1, 2, 3, 4, 5) |
| 87 | + |
| 88 | + @Benchmark |
| 89 | + fun fastKFunctionByReflectionWithInstanceCallBy(): Constructor5 = |
| 90 | + fastKFunctionByReflectionWithInstance.callBy(argumentBucket) |
| 91 | +} |
0 commit comments