|
| 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