|
| 1 | +package com.mapk.fastkfunction |
| 2 | + |
| 3 | +import com.mapk.fastkfunction.argumentbucket.ArgumentBucket |
| 4 | +import com.mapk.fastkfunction.benchmarktargets.Constructor5 |
| 5 | +import com.mapk.fastkfunction.benchmarktargets.topLevelFun5 |
| 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 CallTopLevelFunBenchmark { |
| 16 | + private val function: KFunction<Constructor5> = ::topLevelFun5 |
| 17 | + private val argumentMap: Map<KParameter, Any?> = function.parameters.associateWith { it.index + 1 } |
| 18 | + |
| 19 | + private val javaMethod: Method = function.javaMethod!! |
| 20 | + private val fastKFunction: FastKFunction<Constructor5> = FastKFunction(function, null) |
| 21 | + private val argumentBucket: ArgumentBucket = fastKFunction.generateBucket() |
| 22 | + .apply { (0 until 5).forEach { this[it] = it + 1 } } |
| 23 | + |
| 24 | + @Benchmark |
| 25 | + fun normalCall(): Constructor5 = topLevelFun5(1, 2, 3, 4, 5) |
| 26 | + |
| 27 | + @Benchmark |
| 28 | + fun kFunctionCall(): Constructor5 = function.call(1, 2, 3, 4, 5) |
| 29 | + |
| 30 | + @Benchmark |
| 31 | + fun kFunctionCallBy(): Constructor5 = function.callBy(argumentMap) |
| 32 | + |
| 33 | + @Benchmark |
| 34 | + fun javaMethod(): Constructor5 = javaMethod.invoke(null, 1, 2, 3, 4, 5) as Constructor5 |
| 35 | + |
| 36 | + @Benchmark |
| 37 | + fun fastKFunctionCall(): Constructor5 = fastKFunction.call(1, 2, 3, 4, 5) |
| 38 | + |
| 39 | + @Benchmark |
| 40 | + fun fastKFunctionCallBy(): Constructor5 = fastKFunction.callBy(argumentBucket) |
| 41 | +} |
0 commit comments