Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #904 from aSemy/feat/jmh_benchmark_tests
introduce Kotlinx Benchmark tests (copied from #763)
- Loading branch information
Showing
9 changed files
with
100 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 `kotlinx-benchmark` executed from Gradle) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import buildsrc.config.Deps | ||
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(platform(Deps.Libs.kotlinCoroutinesBom)) | ||
implementation(Deps.Libs.kotlinCoroutinesCore) | ||
implementation(projects.modules.mockk) | ||
} | ||
|
||
benchmark { | ||
configurations { | ||
named("main") { | ||
iterationTime = 10 | ||
iterationTimeUnit = "sec" | ||
iterations = 3 | ||
warmups = 1 | ||
} | ||
} | ||
targets { | ||
register("main") { | ||
this as JvmBenchmarkTarget | ||
jmhVersion = "1.35" | ||
} | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
test-modules/performance-tests/src/main/kotlin/io/mockk/performance/JmhTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
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 noMockOrStub(blackhole: Blackhole) { | ||
val mockedClass = MockedClass() | ||
blackhole.consume(mockedClass) | ||
} | ||
|
||
@Benchmark | ||
fun simpleMock(blackhole: Blackhole) { | ||
val mockedClass: MockedClass = mockk() | ||
blackhole.consume(mockedClass) | ||
} | ||
|
||
@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!" | ||
} | ||
} |