Skip to content

Commit d615cf3

Browse files
Abduqodiri QurbonzodafzhinkinGoooler
committed
Introduce a guide to setting up a separate source set for benchmarks: docs/separate-benchmark-module.md
Co-authored-by: Filipp Zhinkin <[email protected]> Co-authored-by: Zongle Wang <[email protected]>
1 parent fb9db8a commit d615cf3

File tree

2 files changed

+115
-3
lines changed

2 files changed

+115
-3
lines changed

README.md

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

@@ -423,10 +423,10 @@ benchmark {
423423

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

426-
### Separate source sets for benchmarks
426+
### Separate source set for benchmarks
427427

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

431431
## Examples
432432

docs/separate-benchmark-source-set.md

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

0 commit comments

Comments
 (0)