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

introduce Kotlinx Benchmark tests (copied from #763) #904

Merged
merged 7 commits into from Aug 25, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -11,8 +11,6 @@ plugins {
id("buildsrc.convention.toolchain-jvm")
}

// note: all subprojects are currently Kotlin Multiplatform, so this convention plugin is unused

java {
withJavadocJar()
withSourcesJar()
Expand Down
3 changes: 2 additions & 1 deletion settings.gradle.kts
Expand Up @@ -16,7 +16,8 @@ include(
":modules:mockk-agent",
":modules:mockk-dsl",

":modules:client-tests",
":test-modules:client-tests",
":test-modules:performance-tests",
)

val androidSdkDetected: Boolean? by extra
Expand Down
31 changes: 31 additions & 0 deletions test-modules/performance-tests/README.md
@@ -0,0 +1,31 @@
This is a performance testing setup for `mockk` using [JMH](https://github.com/openjdk/jmh).

### How to run

There are two ways to execute the benchmark code

#### 1. Basic with Gradle

```shell
./gradlew benchmark
```

It will use default configuration set in [build.gradle.kts](build.gradle.kts) and print out the results

#### 2. Advanced with JMH binary

If you want access to more JMH features you can

```shell
./gradlew mainBenchmarkJar
```

to build the `.jar` file containing the benchmark code. It will be saved to `./build/benchmarks/main/jars` directory.

From there you can run JMH with all the JMH-specific configuration options form CLI (even on different machine).

```shell
java -jar ./build/benchmarks/main/jars/performance-tests-main-jmh-<version>.jar -w 1 -i 2 -r 60s -prof jfr
```

which will attach the JFR profiler (not possible with `kontlinx-benchmark` executed from Gradle)
aSemy marked this conversation as resolved.
Show resolved Hide resolved
30 changes: 30 additions & 0 deletions test-modules/performance-tests/build.gradle.kts
@@ -0,0 +1,30 @@
import kotlinx.benchmark.gradle.JvmBenchmarkTarget
import kotlinx.benchmark.gradle.benchmark

plugins {
buildsrc.convention.`kotlin-jvm`
id("org.jetbrains.kotlinx.benchmark") version "0.4.5"
}

dependencies {
implementation("org.jetbrains.kotlinx:kotlinx-benchmark-runtime:0.4.5")
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.4")
implementation(projects.modules.mockk)
}

benchmark {
configurations {
named("main") {
iterationTime = 60
iterationTimeUnit = "sec"
iterations = 2
warmups = 1
}
}
targets {
register("main") {
this as JvmBenchmarkTarget
jmhVersion = "1.35"
}
}
}
@@ -0,0 +1,23 @@
package io.mockk.performance

import io.mockk.every
import io.mockk.mockk
import org.openjdk.jmh.annotations.Benchmark
import org.openjdk.jmh.annotations.BenchmarkMode
import org.openjdk.jmh.annotations.Mode
import org.openjdk.jmh.infra.Blackhole

@BenchmarkMode(Mode.Throughput)
open class JmhTest {

@Benchmark
fun simpleMockAndStub(blackhole: Blackhole) {
val mockedClass: MockedClass = mockk()
every { mockedClass.mockedFun() } returns "Hello, mockk!"
blackhole.consume(mockedClass)
}

class MockedClass {
fun mockedFun(): String = "Hello, world!"
}
}