You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+76-9Lines changed: 76 additions & 9 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -198,7 +198,7 @@ To run benchmarks in Kotlin/JVM:
198
198
<details>
199
199
<summary><b>Explanation</b></summary>
200
200
201
-
Consideryou annotated each of your benchmark classes with `@State(Scope.Benchmark)`:
201
+
Assume that you've annotated each of your benchmark classes with `@State(Scope.Benchmark)`:
202
202
203
203
```kotlin
204
204
// MyBenchmark.kt
@@ -315,24 +315,91 @@ Note: Kotlin/WASM is an experimental compilation target for Kotlin. It may be dr
315
315
316
316
### WritingBenchmarks
317
317
318
-
Now you can write your benchmarks.
318
+
After setting up your project and configuring targets, you can start writing benchmarks:
319
319
320
-
// A short introduction to writing benchmarks.
320
+
1. **CreateBenchmarkClass**:Create a classin your source set where you'd like to add the benchmark. Annotate this class with `@State(Scope.Benchmark)`.
321
+
322
+
```kotlin
323
+
@State(Scope.Benchmark)
324
+
classMyBenchmark {
325
+
326
+
}
327
+
```
328
+
329
+
2. **Set up ParametersandVariables**:Define variables needed for the benchmark.
330
+
331
+
```kotlin
332
+
var param:Int=10
333
+
334
+
privatevar list:MutableList<Int> =ArrayList()
335
+
```
336
+
337
+
3. **InitializeResources**:Within the class, you can define any setup or teardown methods using `@Setup` and `@TearDown` annotations respectively. These methods will be executed before and after the entire benchmark run.
338
+
339
+
```kotlin
340
+
@Setup
341
+
funprepare() {
342
+
for (i in0 until size) {
343
+
list.add(i)
344
+
}
345
+
}
346
+
347
+
@TearDown
348
+
funcleanup() {
349
+
list.clear()
350
+
}
351
+
```
352
+
353
+
4. **DefineBenchmarkMethod**:Next, create methods that you would like to be benchmarked within thisclassand annotate them with `@Benchmark`.
354
+
355
+
```kotlin
356
+
@Benchmark
357
+
funbenchmarkMethod(): Int {
358
+
return list.sum()
359
+
}
360
+
```
361
+
362
+
Yourfinal benchmark classwill look something like this:
363
+
364
+
@State(Scope.Benchmark)
365
+
classMyBenchmark {
366
+
367
+
var param:Int=10
368
+
369
+
privatevar list:MutableList<Int> =ArrayList()
370
+
371
+
@Setup
372
+
funprepare() {
373
+
for (i in0 until size) {
374
+
list.add(i)
375
+
}
376
+
}
377
+
378
+
@Benchmark
379
+
funbenchmarkMethod(): Int {
380
+
return list.sum()
381
+
}
382
+
383
+
@TearDown
384
+
funcleanup() {
385
+
list.clear()
386
+
}
387
+
}
321
388
322
389
Note:Benchmark classes located in the common source set will be run in all platforms, while those located in a platform-specific source set will be run only in the corresponding platform.
323
390
324
-
See <TBD-document> to for a complete guide for writing benchmarks.
391
+
See[writing benchmarks](docs/writing-benchmarks.md)for a complete guide for writing benchmarks.
325
392
326
393
### RunningBenchmarks
327
394
328
-
To run your benchmarks in all registered platforms run `benchmark` Gradle task in your project.
329
-
To run in only in a specific platform run `<target-name>Benchmark`, e.g., `jvmBenchmark`.
395
+
To run your benchmarks in all registered platforms, run `benchmark` Gradle task in your project.
396
+
To run only on a specific platform, run `<target-name>Benchmark`, e.g., `jvmBenchmark`.
330
397
331
-
Learn more about the tasks kotlinx-benchmark plugin creates in [this guide](docs/tasks-overview.md).
398
+
For more details about the tasks created by the kotlinx-benchmark plugin, refer to [this guide](docs/tasks-overview.md).
332
399
333
400
### BenchmarkConfigurationProfiles
334
401
335
-
The kotlinx-benchmark library provides ability to create multiple configuration profiles. The `main` configuration is already created by Toolkit.
402
+
The kotlinx-benchmark library provides the ability to create multiple configuration profiles. The `main` configuration is already created by theToolkit.
336
403
Additional profiles can be created as needed in the `configurations` section of the `benchmark` block:
337
404
338
405
```kotlin
@@ -370,4 +437,4 @@ These examples showcase various use cases and offer practical insights into the
370
437
371
438
## Contributing
372
439
373
-
We welcome contributions to kotlinx-benchmark! If you want to contribute, please refer to our Contribution Guidelines.
440
+
We welcome contributions to kotlinx-benchmark!If you want to contribute, please refer to our [ContributionGuidelines](CONTRIBUTING.md).
0 commit comments