|
| 1 | +## Step-by-Step Guide for Multiplatform Benchmarking Setup Using kotlinx.benchmark |
| 2 | + |
| 3 | +### Prerequisites |
| 4 | + |
| 5 | +Before starting, ensure your development environment meets the following requirements: |
| 6 | + |
| 7 | +- **Kotlin**: Version 1.8.20 or newer. Install Kotlin from the [official website](https://kotlinlang.org/) or a package manager like SDKMAN! or Homebrew. |
| 8 | +- **Gradle**: Version 8.0 or newer. Download Gradle from the [official website](https://gradle.org/) or use a package manager. |
| 9 | + |
| 10 | +### Step 1: Create a New Kotlin Multiplatform Project |
| 11 | + |
| 12 | +Begin by creating a new Kotlin Multiplatform project. You can do this either manually or by using an IDE such as IntelliJ IDEA, which can generate the project structure for you. |
| 13 | + |
| 14 | +### Step 2: Configure Build |
| 15 | + |
| 16 | +In this step, you'll modify your project's build file to add necessary dependencies and plugins. |
| 17 | + |
| 18 | +<details open> |
| 19 | +<summary><strong>Kotlin DSL</strong></summary> |
| 20 | + |
| 21 | +#### 2.1 Apply the Necessary Plugins |
| 22 | + |
| 23 | +In your `build.gradle.kts` file, add the required plugins. These plugins are necessary for enabling Kotlin Multiplatform, making all classes and functions open, and using the kotlinx.benchmark plugin. |
| 24 | + |
| 25 | +```kotlin |
| 26 | +plugins { |
| 27 | + kotlin("multiplatform") |
| 28 | + kotlin("plugin.allopen") version "1.8.21" |
| 29 | + id("org.jetbrains.kotlinx.benchmark") version "0.4.8" |
| 30 | +} |
| 31 | +``` |
| 32 | + |
| 33 | +#### 2.2 Add the Dependencies |
| 34 | + |
| 35 | +Next, add the `kotlinx-benchmark-runtime` dependency to your project. This dependency contains the necessary runtime components for benchmarking. |
| 36 | + |
| 37 | +```kotlin |
| 38 | +kotlin { |
| 39 | + sourceSets { |
| 40 | + commonMain { |
| 41 | + dependencies { |
| 42 | + implementation("org.jetbrains.kotlinx:kotlinx-benchmark-runtime:0.4.8") |
| 43 | + } |
| 44 | + } |
| 45 | + } |
| 46 | +} |
| 47 | +``` |
| 48 | + |
| 49 | +#### 2.3 Apply the AllOpen Annotation |
| 50 | + |
| 51 | +Now, you need to instruct the [allopen](https://kotlinlang.org/docs/all-open-plugin.html) plugin to consider all benchmark classes and their methods as open. For that, apply the `allOpen` block and specify the JMH annotation `State`. |
| 52 | + |
| 53 | +```kotlin |
| 54 | +allOpen { |
| 55 | + annotation("org.openjdk.jmh.annotations.State") |
| 56 | +} |
| 57 | +``` |
| 58 | + |
| 59 | +#### 2.4 Define the Repositories |
| 60 | + |
| 61 | +Gradle needs to know where to find the libraries your project depends on. In this case, we're using the libraries hosted on Maven Central, so we need to specify that. |
| 62 | + |
| 63 | +In your `build.gradle.kts` file, add the following code block: |
| 64 | + |
| 65 | +```kotlin |
| 66 | +repositories { |
| 67 | + mavenCentral() |
| 68 | +} |
| 69 | +``` |
| 70 | + |
| 71 | +#### 2.5 Register the Benchmark Targets |
| 72 | + |
| 73 | +Next, we need to inform the kotlinx.benchmark plugin about our benchmarking targets. For multiplatform projects, we need to register each platform separately. |
| 74 | + |
| 75 | +In your `build.gradle.kts` file, add the following code block within the `benchmark` block: |
| 76 | + |
| 77 | +```kotlin |
| 78 | +benchmark { |
| 79 | + targets { |
| 80 | + register("jvm") |
| 81 | + register("js") |
| 82 | + register("native") |
| 83 | + // Add more platforms as needed |
| 84 | + } |
| 85 | +} |
| 86 | +``` |
| 87 | + |
| 88 | +#### 2.6 Define the Kotlin Targets and SourceSets |
| 89 | + |
| 90 | +In the `kotlin` block, you define the different platforms that your project targets and the related source sets. Within each target, you can specify the related compilations. For the JVM, you create a specific 'benchmark' compilation associated with the main compilation. |
| 91 | + |
| 92 | +```kotlin |
| 93 | +jvm { |
| 94 | + compilations.create('benchmark') { associateWith(compilations.main) } |
| 95 | +} |
| 96 | +``` |
| 97 | + |
| 98 | +The dependency `kotlinx-benchmark-runtime` is applied to the `commonMain` source set, indicating that it will be used across all platforms: |
| 99 | + |
| 100 | +```kotlin |
| 101 | +sourceSets { |
| 102 | + commonMain { |
| 103 | + dependencies { |
| 104 | + implementation project(":kotlinx-benchmark-runtime") |
| 105 | + } |
| 106 | + } |
| 107 | +} |
| 108 | +``` |
| 109 | + |
| 110 | +</details> |
| 111 | + |
| 112 | +<details> |
| 113 | +<summary><strong>Groovy DSL</strong></summary> |
| 114 | + |
| 115 | +#### 2.1 Apply the Necessary Plugins |
| 116 | + |
| 117 | +In your `build.gradle` file, apply the required plugins. These plugins are necessary for enabling Kotlin Multiplatform, making all classes and functions open, and using the kotlinx.benchmark plugin. |
| 118 | + |
| 119 | +```groovy |
| 120 | +plugins { |
| 121 | + id 'org.jetbrains.kotlin.multiplatform' |
| 122 | + id 'org.jetbrains.kotlin.plugin.allopen' version '1.8.21' |
| 123 | + id 'org.jetbrains.kotlinx.benchmark' version '0.4.8' |
| 124 | +} |
| 125 | +``` |
| 126 | + |
| 127 | +#### 2.2 Add the Dependencies |
| 128 | + |
| 129 | +Next, add the `kotlinx-benchmark-runtime` dependency to your project. This dependency contains the necessary runtime components for benchmarking. |
| 130 | + |
| 131 | +```groovy |
| 132 | +dependencies { |
| 133 | + implementation 'org.jetbrains.kotlinx:kotlinx-benchmark-runtime:0.4.8' |
| 134 | +} |
| 135 | +``` |
| 136 | + |
| 137 | +#### 2.3 Apply the AllOpen Annotation |
| 138 | + |
| 139 | +Now, you need to instruct the [allopen](https://kotlinlang.org/docs/all-open-plugin.html) plugin to consider all benchmark classes and their methods as open. For that, apply the `allOpen` block and specify the JMH annotation `State`. |
| 140 | + |
| 141 | +```groovy |
| 142 | +allOpen { |
| 143 | + annotation("org.openjdk.jmh.annotations.State") |
| 144 | +} |
| 145 | +``` |
| 146 | + |
| 147 | +#### 2.4 Define the Repositories |
| 148 | + |
| 149 | +Gradle needs to know where to find the libraries your project depends on. In this case, we're using the libraries hosted on Maven Central, so we need to specify that. |
| 150 | + |
| 151 | +In your `build.gradle` file, add the following code block: |
| 152 | + |
| 153 | +```groovy |
| 154 | +repositories { |
| 155 | + mavenCentral() |
| 156 | +} |
| 157 | +``` |
| 158 | + |
| 159 | +#### 2.5 Register the Benchmark Targets |
| 160 | + |
| 161 | +Next, we need to inform the kotlinx.benchmark plugin about our benchmarking targets. For multiplatform projects, we need to register each platform separately. |
| 162 | + |
| 163 | +In your `build.gradle` file, add the following code block within the `benchmark` block: |
| 164 | + |
| 165 | +```groovy |
| 166 | +benchmark { |
| 167 | + targets { |
| 168 | + register("jvm") |
| 169 | + register("js") |
| 170 | + register("native") |
| 171 | + // Add more platforms as needed |
| 172 | + } |
| 173 | +} |
| 174 | +``` |
| 175 | + |
| 176 | +#### 2.6 Define the Kotlin Targets and SourceSets |
| 177 | + |
| 178 | +In the `kotlin` block, you define the different platforms that your project targets and the related source sets. Within each target, you can specify the related compilations. For the JVM, you create a specific 'benchmark' compilation associated with the main compilation. |
| 179 | + |
| 180 | +```kotlin |
| 181 | +jvm { |
| 182 | + compilations.create('benchmark') { associateWith(compilations.main) } |
| 183 | +} |
| 184 | +``` |
| 185 | + |
| 186 | +The dependency `kotlinx-benchmark-runtime` is applied to the `commonMain` source set, indicating that it will be used across all platforms: |
| 187 | + |
| 188 | +```kotlin |
| 189 | +sourceSets { |
| 190 | + commonMain { |
| 191 | + dependencies { |
| 192 | + implementation project(":kotlinx-benchmark-runtime") |
| 193 | + } |
| 194 | + } |
| 195 | +} |
| 196 | +``` |
| 197 | + |
| 198 | +</details> |
| 199 | + |
| 200 | +### Step 3: Writing Benchmarks |
| 201 | + |
| 202 | +Create a new Kotlin source file in your `src/main/kotlin` directory to write your benchmarks. Each benchmark is a class or object with methods annotated with `@Benchmark`. Here's a simple example: |
| 203 | + |
| 204 | +```kotlin |
| 205 | +import org.openjdk.jmh.annotations.Benchmark |
| 206 | + |
| 207 | +open class ListBenchmark { |
| 208 | + @Benchmark |
| 209 | + fun listOfBenchmark() { |
| 210 | + listOf(1, 2, 3, 4, 5) |
| 211 | + } |
| 212 | +} |
| 213 | +``` |
| 214 | + |
| 215 | +Ensure that your benchmark class and methods are `open`, as JMH creates subclasses during the benchmarking process. The `allopen` plugin we added earlier enforces this. |
| 216 | + |
| 217 | +### Step 4: Running Your Benchmarks |
| 218 | + |
| 219 | +Executing your benchmarks is an important part of the process. This will allow you to gather performance data about your code. There are two primary ways to run your benchmarks: through the command line or using your IDE. |
| 220 | + |
| 221 | +#### 4.1 Running Benchmarks From the Command Line |
| 222 | + |
| 223 | +The simplest way to run your benchmarks is by using the Gradle task `benchmark`. You can do this by opening a terminal, navigating to the root of your project, and entering the following command: |
| 224 | + |
| 225 | +```bash |
| 226 | +./gradlew benchmark |
| 227 | +``` |
| 228 | + |
| 229 | +This command instructs Gradle to execute the `benchmark` task, which in turn runs your benchmarks. |
| 230 | + |
| 231 | +#### 4.2 Understanding Benchmark Execution |
| 232 | + |
| 233 | +The execution of your benchmarks might take some time. This is normal and necessary: benchmarks must be run for a sufficient length of time to produce reliable, statistically significant results. |
| 234 | + |
| 235 | +For more details regarding the available Gradle tasks, refer to this [document](docs/tasks-overview.md). |
| 236 | + |
| 237 | +### Step 5: Analyze the Results |
| 238 | + |
| 239 | +To fully understand and make the best use of these results, it's important to know how to interpret and analyze them properly. For a comprehensive guide on interpreting and analyzing benchmarking results, please refer to this dedicated document: [Interpreting and Analyzing Results](docs/interpreting-results.md). |
| 240 | + |
| 241 | +Congratulations! You have successfully set up a Kotlin Multiplatform benchmark project using kotlinx-benchmark. |
0 commit comments