|
| 1 | +package com.mapk.fastkfunction.singleargfastkfunction |
| 2 | + |
| 3 | +import com.mapk.fastkfunction.FastKFunction |
| 4 | +import com.mapk.fastkfunction.SingleArgFastKFunction |
| 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 argument = 1 |
| 18 | + |
| 19 | + private val objectInstance = Constructor1::class.companionObjectInstance!! |
| 20 | + |
| 21 | + private val functionByMethodReference: KFunction<Constructor1> = (Constructor1)::companionObjectFun1 |
| 22 | + @Suppress("UNCHECKED_CAST") |
| 23 | + private val functionByReflection: KFunction<Constructor1> = objectInstance::class |
| 24 | + .functions |
| 25 | + .first { it.name == "companionObjectFun5" } as KFunction<Constructor1> |
| 26 | + |
| 27 | + private val argumentMap: Map<KParameter, Any?> = mapOf(functionByMethodReference.parameters.single() to argument) |
| 28 | + |
| 29 | + private val javaMethod: Method = functionByMethodReference.javaMethod!! |
| 30 | + |
| 31 | + private val fastKFunctionByMethodReferenceWithoutInstance: FastKFunction<Constructor1> = |
| 32 | + FastKFunction.of(functionByMethodReference) |
| 33 | + private val fastKFunctionByMethodReferenceWithInstance: FastKFunction<Constructor1> = |
| 34 | + FastKFunction.of(functionByMethodReference, objectInstance) |
| 35 | + |
| 36 | + private val fastKFunctionByReflectionWithoutInstance: FastKFunction<Constructor1> = |
| 37 | + FastKFunction.of(functionByReflection) |
| 38 | + private val fastKFunctionByReflectionWithInstance: FastKFunction<Constructor1> = |
| 39 | + FastKFunction.of(functionByReflection, objectInstance) |
| 40 | + |
| 41 | + private val singleArgFastKFunctionByMethodReferenceWithoutInstance: SingleArgFastKFunction<Constructor1> = |
| 42 | + SingleArgFastKFunction.of(functionByMethodReference) |
| 43 | + private val singleArgFastKFunctionByMethodReferenceWithInstance: SingleArgFastKFunction<Constructor1> = |
| 44 | + SingleArgFastKFunction.of(functionByMethodReference, objectInstance) |
| 45 | + |
| 46 | + private val singleArgFastKFunctionByReflectionWithoutInstance: SingleArgFastKFunction<Constructor1> = |
| 47 | + SingleArgFastKFunction.of(functionByReflection) |
| 48 | + private val singleArgFastKFunctionByReflectionWithInstance: SingleArgFastKFunction<Constructor1> = |
| 49 | + SingleArgFastKFunction.of(functionByReflection, objectInstance) |
| 50 | + |
| 51 | + @Benchmark |
| 52 | + fun normalCall(): Constructor1 = Constructor1.companionObjectFun1(argument) |
| 53 | + |
| 54 | + @Benchmark |
| 55 | + fun functionByMethodReferenceCall(): Constructor1 = functionByMethodReference.call(argument) |
| 56 | + |
| 57 | + @Benchmark |
| 58 | + fun functionByMethodReferenceCallBy(): Constructor1 = functionByMethodReference.callBy(argumentMap) |
| 59 | + |
| 60 | + @Benchmark |
| 61 | + fun functionByReflectionCall(): Constructor1 = functionByReflection.call(argument) |
| 62 | + |
| 63 | + @Benchmark |
| 64 | + fun functionByReflectionCallBy(): Constructor1 = functionByReflection.callBy(argumentMap) |
| 65 | + |
| 66 | + @Benchmark |
| 67 | + fun javaMethod(): Constructor1 = javaMethod.invoke(objectInstance, argument) as Constructor1 |
| 68 | + |
| 69 | + @Benchmark |
| 70 | + fun fastKFunctionByMethodReferenceWithoutInstanceCall(): Constructor1 = |
| 71 | + fastKFunctionByMethodReferenceWithoutInstance.call(argument) |
| 72 | + |
| 73 | + @Benchmark |
| 74 | + fun fastKFunctionByMethodReferenceWithInstanceCall(): Constructor1 = |
| 75 | + fastKFunctionByMethodReferenceWithInstance.call(argument) |
| 76 | + |
| 77 | + @Benchmark |
| 78 | + fun fastKFunctionByReflectionWithoutInstanceCall(): Constructor1 = |
| 79 | + fastKFunctionByReflectionWithoutInstance.call(argument) |
| 80 | + |
| 81 | + @Benchmark |
| 82 | + fun fastKFunctionByReflectionWithInstanceCall(): Constructor1 = |
| 83 | + fastKFunctionByReflectionWithInstance.call(argument) |
| 84 | + |
| 85 | + @Benchmark |
| 86 | + fun singleArgFastKFunctionByMethodReferenceWithoutInstanceCall(): Constructor1 = |
| 87 | + singleArgFastKFunctionByMethodReferenceWithoutInstance.call(argument) |
| 88 | + |
| 89 | + @Benchmark |
| 90 | + fun singleArgFastKFunctionByMethodReferenceWithInstanceCall(): Constructor1 = |
| 91 | + singleArgFastKFunctionByMethodReferenceWithInstance.call(argument) |
| 92 | + |
| 93 | + @Benchmark |
| 94 | + fun singleArgFastKFunctionByReflectionWithoutInstanceCall(): Constructor1 = |
| 95 | + singleArgFastKFunctionByReflectionWithoutInstance.call(argument) |
| 96 | + |
| 97 | + @Benchmark |
| 98 | + fun singleArgFastKFunctionByReflectionWithInstanceCall(): Constructor1 = |
| 99 | + singleArgFastKFunctionByReflectionWithInstance.call(argument) |
| 100 | +} |
0 commit comments