|
| 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 CallInstanceMethodBenchmark { |
| 16 | + private val argument = 1 |
| 17 | + |
| 18 | + private val instance = Constructor1(1) |
| 19 | + private val function: KFunction<Constructor1> = instance::instanceFun1 |
| 20 | + private val argumentMap: Map<KParameter, Any?> = mapOf(function.parameters.single() to argument) |
| 21 | + |
| 22 | + private val javaMethod: Method = function.javaMethod!! |
| 23 | + private val fastKFunctionWithoutInstance: FastKFunction<Constructor1> = FastKFunction.of(function, null) |
| 24 | + private val fastKFunctionWithInstance: FastKFunction<Constructor1> = FastKFunction.of(function, instance) |
| 25 | + private val singleArgFastKFunctionWithoutInstance: SingleArgFastKFunction<Constructor1> = |
| 26 | + SingleArgFastKFunction.of(function, null) |
| 27 | + private val singleArgFastKFunctionWithInstance: SingleArgFastKFunction<Constructor1> = |
| 28 | + SingleArgFastKFunction.of(function, instance) |
| 29 | + |
| 30 | + @Benchmark |
| 31 | + fun normalCall(): Constructor1 = instance.instanceFun1(argument) |
| 32 | + |
| 33 | + @Benchmark |
| 34 | + fun kFunctionCall(): Constructor1 = function.call(argument) |
| 35 | + |
| 36 | + @Benchmark |
| 37 | + fun kFunctionCallBy(): Constructor1 = function.callBy(argumentMap) |
| 38 | + |
| 39 | + @Benchmark |
| 40 | + fun javaMethod(): Constructor1 = javaMethod.invoke(instance, argument) as Constructor1 |
| 41 | + |
| 42 | + @Benchmark |
| 43 | + fun fastKFunctionWithoutInstanceCall(): Constructor1 = fastKFunctionWithoutInstance.call(argument) |
| 44 | + |
| 45 | + @Benchmark |
| 46 | + fun fastKFunctionWithInstanceCall(): Constructor1 = fastKFunctionWithInstance.call(argument) |
| 47 | + |
| 48 | + @Benchmark |
| 49 | + fun singleArgFastKFunctionWithoutInstanceCall(): Constructor1 = singleArgFastKFunctionWithoutInstance.call(argument) |
| 50 | + |
| 51 | + @Benchmark |
| 52 | + fun singleArgFastKFunctionWithInstanceCall(): Constructor1 = singleArgFastKFunctionWithInstance.call(argument) |
| 53 | +} |
0 commit comments