|
| 1 | +package com.mapk.fastkfunction.singleargfastkfunction |
| 2 | + |
| 3 | +import com.mapk.fastkfunction.FastKFunction |
| 4 | +import com.mapk.fastkfunction.SingleArgFastKFunction |
| 5 | +import com.mapk.fastkfunction.argumentbucket.ArgumentBucket |
| 6 | +import org.openjdk.jmh.annotations.Benchmark |
| 7 | +import org.openjdk.jmh.annotations.Scope |
| 8 | +import org.openjdk.jmh.annotations.State |
| 9 | +import java.lang.reflect.Method |
| 10 | +import kotlin.reflect.KFunction |
| 11 | +import kotlin.reflect.KParameter |
| 12 | +import kotlin.reflect.jvm.javaMethod |
| 13 | + |
| 14 | +@State(Scope.Benchmark) |
| 15 | +open class CallTopLevelExtensionFunBenchmark { |
| 16 | + private val argument = 1 |
| 17 | + |
| 18 | + private val receiverInstance = Constructor1(argument) |
| 19 | + |
| 20 | + private val functionByMethodReference: KFunction<Constructor1> = receiverInstance::topLevelExtensionFun1 |
| 21 | + private val functionFromClass: KFunction<Constructor1> = Constructor1::topLevelExtensionFun1 |
| 22 | + |
| 23 | + private val argumentMap: Map<KParameter, Any?> = mapOf(functionByMethodReference.parameters.single() to argument) |
| 24 | + |
| 25 | + private val javaMethod: Method = functionByMethodReference.javaMethod!! |
| 26 | + |
| 27 | + private val fastKFunctionByMethodReferenceWithoutInstance: FastKFunction<Constructor1> = |
| 28 | + FastKFunction.of(functionByMethodReference) |
| 29 | + private val fastKFunctionByMethodReferenceWithInstance: FastKFunction<Constructor1> = |
| 30 | + FastKFunction.of(functionByMethodReference, receiverInstance) |
| 31 | + |
| 32 | + private val fastKFunctionFromClass: FastKFunction<Constructor1> = |
| 33 | + FastKFunction.of(functionFromClass, receiverInstance) |
| 34 | + |
| 35 | + private val singleArgFastKFunctionByMethodReferenceWithoutInstance: SingleArgFastKFunction<Constructor1> = |
| 36 | + SingleArgFastKFunction.of(functionByMethodReference) |
| 37 | + private val singleArgFastKFunctionByMethodReferenceWithInstance: SingleArgFastKFunction<Constructor1> = |
| 38 | + SingleArgFastKFunction.of(functionByMethodReference, receiverInstance) |
| 39 | + |
| 40 | + private val singleArgFastKFunctionFromClass: SingleArgFastKFunction<Constructor1> = |
| 41 | + SingleArgFastKFunction.of(functionFromClass, receiverInstance) |
| 42 | + |
| 43 | + @Benchmark |
| 44 | + fun normalCall(): Constructor1 = receiverInstance.topLevelExtensionFun1(argument) |
| 45 | + |
| 46 | + @Benchmark |
| 47 | + fun functionByMethodReferenceCall(): Constructor1 = functionByMethodReference.call(argument) |
| 48 | + |
| 49 | + @Benchmark |
| 50 | + fun functionByMethodReferenceCallBy(): Constructor1 = functionByMethodReference.callBy(argumentMap) |
| 51 | + |
| 52 | + @Benchmark |
| 53 | + fun functionFromClassCall(): Constructor1 = functionFromClass.call(argument) |
| 54 | + |
| 55 | + @Benchmark |
| 56 | + fun functionFromClassCallBy(): Constructor1 = functionFromClass.callBy(argumentMap) |
| 57 | + |
| 58 | + @Benchmark |
| 59 | + fun javaMethod(): Constructor1 = javaMethod.invoke(null, receiverInstance, argument) as Constructor1 |
| 60 | + |
| 61 | + @Benchmark |
| 62 | + fun fastKFunctionByMethodReferenceWithoutInstanceCall(): Constructor1 = |
| 63 | + fastKFunctionByMethodReferenceWithoutInstance.call(argument) |
| 64 | + |
| 65 | + @Benchmark |
| 66 | + fun fastKFunctionByMethodReferenceWithInstanceCall(): Constructor1 = |
| 67 | + fastKFunctionByMethodReferenceWithInstance.call(argument) |
| 68 | + |
| 69 | + @Benchmark |
| 70 | + fun fastKFunctionFromClassCall(): Constructor1 = fastKFunctionFromClass.call(argument) |
| 71 | + |
| 72 | + @Benchmark |
| 73 | + fun singleArgFastKFunctionByMethodReferenceWithoutInstanceCall(): Constructor1 = |
| 74 | + singleArgFastKFunctionByMethodReferenceWithoutInstance.call(argument) |
| 75 | + |
| 76 | + @Benchmark |
| 77 | + fun singleArgFastKFunctionByMethodReferenceWithInstanceCall(): Constructor1 = |
| 78 | + singleArgFastKFunctionByMethodReferenceWithInstance.call(argument) |
| 79 | + |
| 80 | + @Benchmark |
| 81 | + fun singleArgFastKFunctionFromClassCall(): Constructor1 = singleArgFastKFunctionFromClass.call(argument) |
| 82 | +} |
0 commit comments