Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement level configuration at which fixture method should run #10

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package test

import kotlinx.benchmark.*
import kotlin.math.*

@State(Scope.Benchmark)
@Measurement(iterations = 3, time = 1, timeUnit = BenchmarkTimeUnit.SECONDS)
@OutputTimeUnit(BenchmarkTimeUnit.MILLISECONDS)
@BenchmarkMode(Mode.Throughput)
class FixtureLevelBenchmark {
private var data: Double = 0.0

private var iterations: Int = 0

@Setup(Level.Trial)
fun trialSetup() {
data = 3.0
iterations = 0
}


@Setup(Level.Iteration)
fun iterationSetup() {
iterations++
}

@TearDown(Level.Trial)
fun trialTearDown() {
// println("Total iterations = $iterations")
}

@Benchmark
fun mathBenchmark(): Double {
return log(sqrt(data) * cos(data), 2.0)
}
}
91 changes: 68 additions & 23 deletions plugin/main/src/kotlinx/benchmark/gradle/SuiteSourceGenerator.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,11 @@ enum class Platform {

class SuiteSourceGenerator(val title: String, val module: ModuleDescriptor, val output: File, val platform: Platform) {
companion object {
val setupFunctionName = "setUp"
val teardownFunctionName = "tearDown"
val fixtureFunctionLevels = listOf("Invocation", "Iteration", "Trial")
val defaultFixtureFunctionLevel = "Trial"
val setupFunctionSuffix = "Setup"
val teardownFunctionSuffix = "TearDown"

val parametersFunctionName = "parametrize"

val externalConfigurationFQN = "kotlinx.benchmark.ExternalConfiguration"
Expand Down Expand Up @@ -136,31 +139,41 @@ class SuiteSourceGenerator(val title: String, val module: ModuleDescriptor, val
val benchmarkFunctions =
functions.filter { it.annotations.any { it.fqName.toString() == benchmarkAnnotationFQN } }

val setupFunctions = functions
.filter { it.annotations.any { it.fqName.toString() == setupAnnotationFQN } }
val levelToSetupFunctions = fixtureFunctionLevels.associateWith { level ->
functions.filterFixtureFunctions(setupAnnotationFQN, level, level == defaultFixtureFunctionLevel, false)
}

val teardownFunctions = functions
.filter { it.annotations.any { it.fqName.toString() == teardownAnnotationFQN } }.reversed()
val levelToTeardownFunctions = fixtureFunctionLevels.associateWith { level ->
functions.filterFixtureFunctions(teardownAnnotationFQN, level, level == defaultFixtureFunctionLevel, true)
}

val file = FileSpec.builder(mainBenchmarkPackage, benchmarkName).apply {
declareObject(benchmarkClass) {
addAnnotation(suppressUnusedParameter)

function(setupFunctionName) {
addModifiers(KModifier.PRIVATE)
addParameter("instance", originalClass)
for (fn in setupFunctions) {
val functionName = fn.name.toString()
addStatement("instance.%N()", functionName)
for ((level, setupFunctions) in levelToSetupFunctions) {
val setupFunctionName = setupFunctionName(level)

function(setupFunctionName) {
addModifiers(KModifier.PRIVATE)
addParameter("instance", originalClass)
for (fn in setupFunctions) {
val functionName = fn.name.toString()
addStatement("instance.%N()", functionName)
}
}
}

function(teardownFunctionName) {
addModifiers(KModifier.PRIVATE)
addParameter("instance", originalClass)
for (fn in teardownFunctions) {
val functionName = fn.name.toString()
addStatement("instance.%N()", functionName)
for ((level, teardownFunctions) in levelToTeardownFunctions) {
val teardownFunctionName = teardownFunctionName(level)

function(teardownFunctionName) {
addModifiers(KModifier.PRIVATE)
addParameter("instance", originalClass)
for (fn in teardownFunctions) {
val functionName = fn.name.toString()
addStatement("instance.%N()", functionName)
}
}
}

Expand Down Expand Up @@ -201,15 +214,27 @@ class SuiteSourceGenerator(val title: String, val module: ModuleDescriptor, val
function("describe") {
returns(suiteDescriptorType.parameterizedBy(originalClass))
addCode(
"«val descriptor = %T(name = %S, factory = ::%T, setup = ::%N, teardown = ::%N, parametrize = ::%N",
"«val descriptor = %T(name = %S, factory = ::%T",
suiteDescriptorType,
originalName,
originalClass,
setupFunctionName,
teardownFunctionName,
parametersFunctionName
originalClass
)

val hasInvocationFixture = listOf(levelToSetupFunctions, levelToTeardownFunctions).any {
it.getValue("Invocation").isNotEmpty()
}
val setupFunctions = fixtureFunctionLevels.joinToString(separator = ", ") { level ->
val functionName = setupFunctionName(level)
"$functionName = ::$functionName"
}
val teardownFunctions = fixtureFunctionLevels.joinToString(separator = ", ") { level ->
val functionName = teardownFunctionName(level)
"$functionName = ::$functionName"
}
addCode(", hasInvocationFixture = $hasInvocationFixture, $setupFunctions, $teardownFunctions")

addCode(", parametrize = ::%N", parametersFunctionName)

val params =
parameterProperties.joinToString(prefix = "listOf(", postfix = ")") { "\"${it.name}\"" }
addCode(", parameters = $params")
Expand Down Expand Up @@ -258,6 +283,26 @@ class SuiteSourceGenerator(val title: String, val module: ModuleDescriptor, val

file.writeTo(output)
}

private fun setupFunctionName(level: String) = fixtureFunctionName(level, setupFunctionSuffix)

private fun teardownFunctionName(level: String) = fixtureFunctionName(level, teardownFunctionSuffix)

private fun fixtureFunctionName(level: String, suffix: String) = level.decapitalize() + suffix
}

private fun List<FunctionDescriptor>.filterFixtureFunctions(
annotationFQN: String,
level: String,
isDefaultLevel: Boolean,
reversed: Boolean
): List<FunctionDescriptor> {
return let { if (reversed) it.reversed() else it }
.filter {
val annotation = it.annotations.firstOrNull { it.fqName.toString() == annotationFQN } ?: return@filter false
val levelValue = annotation.argumentValue("value") as? EnumValue
return@filter levelValue?.enumEntryName?.toString()?.let { it == level } ?: isDefaultLevel
}
}

inline fun codeBlock(builderAction: CodeBlock.Builder.() -> Unit): CodeBlock {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package kotlinx.benchmark

@Target(AnnotationTarget.FUNCTION)
expect annotation class Setup(val value: Level = Level.Trial)

expect annotation class Setup()
expect enum class Level {
Trial, Iteration, Invocation
}

@Target(AnnotationTarget.FUNCTION)
expect annotation class TearDown()
expect annotation class TearDown(val value: Level = Level.Trial)

@Target(AnnotationTarget.FUNCTION)
expect annotation class Benchmark()
Expand Down
12 changes: 10 additions & 2 deletions runtime/commonMain/src/kotlinx/benchmark/SuiteDescriptor.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,16 @@ open class SuiteDescriptor<T>(
val name: String,
val factory: () -> T,
val parametrize: (T, Map<String, String>) -> Unit,
val setup: (T) -> Unit,
val teardown: (T) -> Unit,

val hasInvocationFixture: Boolean,

val invocationSetup: (T) -> Unit,
val iterationSetup: (T) -> Unit,
val trialSetup: (T) -> Unit,

val invocationTearDown: (T) -> Unit,
val iterationTearDown: (T) -> Unit,
val trialTearDown: (T) -> Unit,

val parameters: List<String>,
val defaultParameters: Map<String, List<String>>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,13 @@ actual enum class Scope {
Benchmark
}

actual enum class Level {
Trial, Iteration, Invocation
}

actual annotation class State(actual val value: Scope)
actual annotation class Setup
actual annotation class TearDown
actual annotation class Setup(actual val value: Level)
actual annotation class TearDown(actual val value: Level)
actual annotation class Benchmark


Expand Down
13 changes: 11 additions & 2 deletions runtime/jsMain/src/kotlinx/benchmark/js/JsExecutor.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ class JsExecutor(name: String, @Suppress("UNUSED_PARAMETER") dummy_args: Array<o

benchmarks.forEach { benchmark ->
val suite = benchmark.suite

if (suite.hasInvocationFixture) {
throw UnsupportedOperationException("Fixture methods with `Invocation` level are not supported")
}

val config = BenchmarkConfiguration(runnerConfiguration, suite)
val jsDescriptor = benchmark as JsBenchmarkDescriptor

Expand Down Expand Up @@ -63,7 +68,8 @@ class JsExecutor(name: String, @Suppress("UNUSED_PARAMETER") dummy_args: Array<o

jsBenchmark.on("start") { event ->
reporter.startBenchmark(executionName, id)
suite.setup(instance)
suite.trialSetup(instance)
suite.iterationSetup(instance)
}
var iteration = 0
jsBenchmark.on("cycle") { event ->
Expand All @@ -76,9 +82,12 @@ class JsExecutor(name: String, @Suppress("UNUSED_PARAMETER") dummy_args: Array<o
id,
"Iteration #${iteration++}: $sample"
)
suite.iterationTearDown(instance)
suite.iterationSetup(instance)
}
jsBenchmark.on("complete") { event ->
suite.teardown(instance)
suite.iterationTearDown(instance)
suite.trialTearDown(instance)
val stats = event.target.stats
val samples = stats.sample
.unsafeCast<DoubleArray>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@ actual typealias Scope = org.openjdk.jmh.annotations.Scope

actual typealias State = org.openjdk.jmh.annotations.State

@Suppress("ACTUAL_ANNOTATION_CONFLICTING_DEFAULT_ARGUMENT_VALUE")
actual typealias Setup = org.openjdk.jmh.annotations.Setup

actual typealias Level = org.openjdk.jmh.annotations.Level

@Suppress("ACTUAL_ANNOTATION_CONFLICTING_DEFAULT_ARGUMENT_VALUE")
actual typealias TearDown = org.openjdk.jmh.annotations.TearDown

actual typealias Benchmark = org.openjdk.jmh.annotations.Benchmark
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,13 @@ actual enum class Scope {
Benchmark
}

actual enum class Level {
Trial, Iteration, Invocation
}

actual annotation class State(actual val value: Scope)
actual annotation class Setup
actual annotation class TearDown
actual annotation class Setup(actual val value: Level)
actual annotation class TearDown(actual val value: Level)
actual annotation class Benchmark


Expand Down
Loading