|
| 1 | +## Step-by-Step Setup Guide for Single-Platform Benchmarking Project Using kotlinx-benchmark |
| 2 | + |
| 3 | +### Prerequisites |
| 4 | + |
| 5 | +Before starting, ensure your development environment meets the following [requirements](docs/compatibility.md): |
| 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 Project |
| 11 | + |
| 12 | +If you're starting from scratch, you can begin by creating a new Kotlin project with Gradle. This can be done either manually, through the command line, or by using an IDE like IntelliJ IDEA, which offers built-in support for project generation. |
| 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/JVM, making all classes and functions open, and using the kotlinx.benchmark plugin. |
| 24 | + |
| 25 | +```kotlin |
| 26 | +plugins { |
| 27 | + kotlin("jvm") version "1.8.21" |
| 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 | +dependencies { |
| 39 | + implementation("org.jetbrains.kotlinx:kotlinx-benchmark-runtime:0.4.8") |
| 40 | +} |
| 41 | +``` |
| 42 | + |
| 43 | +#### 2.3 Apply the AllOpen Annotation |
| 44 | + |
| 45 | +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`. |
| 46 | + |
| 47 | +```kotlin |
| 48 | +allOpen { |
| 49 | + annotation("org.openjdk.jmh.annotations.State") |
| 50 | +} |
| 51 | +``` |
| 52 | + |
| 53 | +#### 2.4 Define the Repositories |
| 54 | + |
| 55 | +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. |
| 56 | + |
| 57 | +In your `build.gradle.kts` file, add the following code block: |
| 58 | + |
| 59 | +```kotlin |
| 60 | +repositories { |
| 61 | + mavenCentral() |
| 62 | +} |
| 63 | +``` |
| 64 | + |
| 65 | +#### 2.5 Register the Benchmark Targets |
| 66 | + |
| 67 | +Next, we need to inform the kotlinx.benchmark plugin about our benchmarking target. In this case, we are targeting JVM. |
| 68 | + |
| 69 | +In your `build.gradle.kts` file, add the following code block within the `benchmark` block: |
| 70 | + |
| 71 | +```kotlin |
| 72 | +benchmark { |
| 73 | + targets { |
| 74 | + register("jvm") |
| 75 | + } |
| 76 | +} |
| 77 | +``` |
| 78 | + |
| 79 | +</details> |
| 80 | + |
| 81 | +<details> |
| 82 | +<summary><strong>Groovy DSL</strong></summary> |
| 83 | + |
| 84 | +#### 2.1 Apply the Necessary Plugins |
| 85 | + |
| 86 | +In your `build.gradle` file, apply the required plugins. These plugins are necessary for enabling Kotlin/JVM, making all classes and functions open, and using the kotlinx.benchmark plugin. |
| 87 | + |
| 88 | +```groovy |
| 89 | +plugins { |
| 90 | + id 'org.jetbrains.kotlin.jvm' version '1.8.21' |
| 91 | + id 'org.jetbrains.kotlin.plugin.allopen' version '1.8.21' |
| 92 | + id 'org.jetbrains.kotlinx.benchmark' version '0.4.8' |
| 93 | +} |
| 94 | +``` |
| 95 | + |
| 96 | +#### 2.2 Add the Dependencies |
| 97 | + |
| 98 | +Next, add the `kotlinx-benchmark-runtime` dependency to your project. This dependency contains the necessary runtime components for benchmarking. |
| 99 | + |
| 100 | +```groovy |
| 101 | +dependencies { |
| 102 | + implementation 'org.jetbrains.kotlinx:kotlinx-benchmark-runtime:0.4.8' |
| 103 | +} |
| 104 | +``` |
| 105 | + |
| 106 | +#### 2.3 Apply the AllOpen Annotation |
| 107 | + |
| 108 | +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`. |
| 109 | + |
| 110 | +```groovy |
| 111 | +allOpen { |
| 112 | + annotation("org.openjdk.jmh.annotations.State") |
| 113 | +} |
| 114 | +``` |
| 115 | + |
| 116 | +#### 2.4 Define the Repositories |
| 117 | + |
| 118 | +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. |
| 119 | + |
| 120 | +In your `build.gradle` file, add the following code block: |
| 121 | + |
| 122 | +```groovy |
| 123 | +repositories { |
| 124 | + mavenCentral() |
| 125 | +} |
| 126 | +``` |
| 127 | + |
| 128 | +#### 2.5 Register the Benchmark Targets |
| 129 | + |
| 130 | +Next, we need to inform the kotlinx.benchmark plugin about our benchmarking target. In this case, we are targeting JVM. |
| 131 | + |
| 132 | +In your `build.gradle` file, add the following code block within the `benchmark` block: |
| 133 | + |
| 134 | +```groovy |
| 135 | +benchmark { |
| 136 | + targets { |
| 137 | + register("jvm") |
| 138 | + } |
| 139 | +} |
| 140 | +``` |
| 141 | + |
| 142 | +</details> |
| 143 | + |
| 144 | +### Step 3: Writing Benchmarks |
| 145 | + |
| 146 | +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: |
| 147 | + |
| 148 | +```kotlin |
| 149 | +import org.openjdk.jmh.annotations.Benchmark |
| 150 | + |
| 151 | +open class ListBenchmark { |
| 152 | + @Benchmark |
| 153 | + fun listOfBenchmark() { |
| 154 | + listOf(1, 2, 3, 4, 5) |
| 155 | + } |
| 156 | +} |
| 157 | +``` |
| 158 | + |
| 159 | +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. |
| 160 | + |
| 161 | +### Step 4: Running Your Benchmarks |
| 162 | + |
| 163 | +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. |
| 164 | + |
| 165 | +#### 4.1 Running Benchmarks From the Command Line |
| 166 | + |
| 167 | +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: |
| 168 | + |
| 169 | +```bash |
| 170 | +./gradlew benchmark |
| 171 | +``` |
| 172 | + |
| 173 | +This command instructs Gradle to execute the `benchmark` task, which in turn runs your benchmarks. |
| 174 | + |
| 175 | +#### 4.2 Understanding Benchmark Execution |
| 176 | + |
| 177 | +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. |
| 178 | + |
| 179 | +For more details regarding the available Gradle tasks, refer to this [document](docs/tasks-overview.md). |
| 180 | + |
| 181 | +### Step 5: Analyze the Results |
| 182 | + |
| 183 | +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). |
| 184 | + |
| 185 | +Congratulations! You have successfully set up a Kotlin/JVM benchmark project using kotlinx-benchmark. |
0 commit comments