Skip to content

Commit 2d8f64a

Browse files
author
Abduqodiri Qurbonzoda
committed
Introduce docs/separate-benchmark-module.md
1 parent 1869c67 commit 2d8f64a

File tree

2 files changed

+106
-4
lines changed

2 files changed

+106
-4
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ kotlinx-benchmark is a toolkit for running benchmarks for multiplatform code wri
3030
- [Writing Benchmarks](#writing-benchmarks)
3131
- [Running Benchmarks](#running-benchmarks)
3232
- [Benchmark Configuration Profiles](#benchmark-configuration-profiles)
33-
- [Separate source sets for benchmarks](#separate-source-sets-for-benchmarks)
33+
- [Separate module for benchmarks](#separate-module-for-benchmarks)
3434
- [Examples](#examples)
3535
- [Contributing](#contributing)
3636

@@ -41,7 +41,7 @@ kotlinx-benchmark is a toolkit for running benchmarks for multiplatform code wri
4141
- [Understanding Benchmark Runtime](docs/benchmark-runtime.md)
4242
- [Configuring kotlinx-benchmark](docs/configuration-options.md)
4343
- [Interpreting and Analyzing Results](docs/interpreting-results.md)
44-
- [Creating Separate Source Sets](docs/separate-source-sets.md)
44+
- [Creating a Separate Benchmark Module](docs/separate-benchmark-module.md)
4545
- [Tasks Overview](docs/tasks-overview.md)
4646
- [Compatibility Guide](docs/compatibility.md)
4747
- [Submitting issues and PRs](CONTRIBUTING.md)
@@ -425,10 +425,10 @@ benchmark {
425425

426426
Refer to our [comprehensive guide](docs/configuration-options.md) to learn about configuration options and how they affect benchmark execution.
427427

428-
### Separate source sets for benchmarks
428+
### Separate module for benchmarks
429429

430430
Often you want to have benchmarks in the same project, but separated from main code, much like tests.
431-
Refer to our [detailed documentation](docs/separate-source-sets.md) on configuring your project to add a separate source set for benchmarks.
431+
Refer to our [detailed documentation](docs/separate-benchmark-module.md) on configuring your project to add a separate module for benchmarks.
432432

433433
## Examples
434434

docs/separate-benchmark-module.md

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
# Creating Separate Module for Benchmarks
2+
3+
This guide will walk you through the process of creating a separate module for benchmarks in your Kotlin project.
4+
Creating a separate module for benchmarks is especially beneficial when you are integrating benchmarks into an existing project.
5+
Here are a few advantages of doing so:
6+
7+
1. **Organization**: It helps maintain a clean and organized project structure. Segregating benchmarks from the main code makes it easier to navigate and locate specific code segments.
8+
9+
2. **Isolation**: Separating benchmarks ensures that the benchmarking code does not interfere with your main code or test code. This isolation guarantees accurate measurements without unintentional side effects.
10+
11+
3. **Flexibility**: Creating a separate module allows you to manage your benchmarking code independently. You can compile, test, and run benchmarks without impacting your main source code.
12+
13+
## Step-by-step Setup Guide
14+
15+
Below are the step-by-step instructions to set up a separate module for benchmarks in both Kotlin Multiplatform and Kotlin/JVM projects:
16+
17+
### Kotlin Multiplatform Project
18+
19+
Follow these steps to set up a separate compilation for benchmarks:
20+
21+
1. **Define New Compilation**:
22+
23+
Start by creating a new compilation in your target of choice (e.g. jvm, js, native, wasm etc.).
24+
In this example, we're associating the new compilation `benchmark` with the `main` compilation of the `jvm` target.
25+
This association allows the benchmark compilation to access the internal API of the main compilation,
26+
which is particularly useful when benchmarks need to measure the performance of specific components
27+
or functionalities within the main codebase.
28+
29+
```kotlin
30+
// build.gradle.kts
31+
kotlin {
32+
jvm {
33+
compilations.create('benchmark') {
34+
associateWith(compilations.main)
35+
}
36+
}
37+
}
38+
```
39+
40+
2. **Register Benchmark Compilation**:
41+
42+
Conclude by registering your new benchmark compilation using its default source set name.
43+
In this instance, `jvmBenchmark` is the name of the default source set of the `benchmark` compilation.
44+
45+
```kotlin
46+
// build.gradle.kts
47+
benchmark {
48+
targets {
49+
register("jvmBenchmark")
50+
}
51+
}
52+
```
53+
54+
For additional information, refer to the [Kotlin documentation on creating a custom compilation](https://kotlinlang.org/docs/multiplatform-configure-compilations.html#create-a-custom-compilation).
55+
and the [documentation on associating compiler tasks](https://kotlinlang.org/docs/gradle-configure-project.html#associate-compiler-tasks).
56+
[Here is a sample Kotlin Multiplatform project](/examples/kotlin-multiplatform) with a separate compilation for benchmarks.
57+
58+
### Kotlin/JVM Project
59+
60+
Set up a separate benchmark source set by following these simple steps:
61+
62+
1. **Define Source Set**:
63+
64+
Begin by defining a new source set. We'll use `benchmark` as the name for the source set.
65+
66+
```kotlin
67+
// build.gradle.kts
68+
sourceSets {
69+
create("benchmark")
70+
}
71+
```
72+
73+
2. **Propagate Dependencies**:
74+
75+
Next, propagate dependencies and output from the `main` source set to your `benchmark` source set.
76+
This ensures the `benchmark` source set has access to classes and resources from the `main` source set.
77+
78+
```kotlin
79+
// build.gradle.kts
80+
dependencies {
81+
add("benchmarkImplementation", sourceSets.main.get().output + sourceSets.main.get().runtimeClasspath)
82+
}
83+
```
84+
85+
You can also add output and `compileClasspath` from `sourceSets.test` in the same way
86+
if you wish to reuse some of the test infrastructure.
87+
88+
3. **Register Benchmark Source Set**:
89+
90+
Finally, register your benchmark source set. This informs the kotlinx-benchmark tool
91+
that benchmarks reside within this source set and need to be executed accordingly.
92+
93+
```kotlin
94+
// build.gradle.kts
95+
benchmark {
96+
targets {
97+
register("benchmark")
98+
}
99+
}
100+
```
101+
102+
[Here is a sample Kotlin/JVM project](/examples/kotlin) with custom source set for benchmarks.

0 commit comments

Comments
 (0)