Skip to content

Commit 87ea754

Browse files
wldehAbduqodiri Qurbonzoda
authored andcommitted
Polish README.md
1 parent 24e0555 commit 87ea754

File tree

1 file changed

+76
-9
lines changed

1 file changed

+76
-9
lines changed

README.md

Lines changed: 76 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ To run benchmarks in Kotlin/JVM:
198198
<details>
199199
<summary><b>Explanation</b></summary>
200200

201-
Consider you 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)`:
202202
203203
```kotlin
204204
// MyBenchmark.kt
@@ -315,24 +315,91 @@ Note: Kotlin/WASM is an experimental compilation target for Kotlin. It may be dr
315315

316316
### Writing Benchmarks
317317

318-
Now you can write your benchmarks.
318+
After setting up your project and configuring targets, you can start writing benchmarks:
319319

320-
// A short introduction to writing benchmarks.
320+
1. **Create Benchmark Class**: Create a class in 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+
class MyBenchmark {
325+
326+
}
327+
```
328+
329+
2. **Set up Parameters and Variables**: Define variables needed for the benchmark.
330+
331+
```kotlin
332+
var param: Int = 10
333+
334+
private var list: MutableList<Int> = ArrayList()
335+
```
336+
337+
3. **Initialize Resources**: 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+
fun prepare() {
342+
for (i in 0 until size) {
343+
list.add(i)
344+
}
345+
}
346+
347+
@TearDown
348+
fun cleanup() {
349+
list.clear()
350+
}
351+
```
352+
353+
4. **Define Benchmark Method**: Next, create methods that you would like to be benchmarked within this class and annotate them with `@Benchmark`.
354+
355+
```kotlin
356+
@Benchmark
357+
fun benchmarkMethod(): Int {
358+
return list.sum()
359+
}
360+
```
361+
362+
Your final benchmark class will look something like this:
363+
364+
@State(Scope.Benchmark)
365+
class MyBenchmark {
366+
367+
var param: Int = 10
368+
369+
private var list: MutableList<Int> = ArrayList()
370+
371+
@Setup
372+
fun prepare() {
373+
for (i in 0 until size) {
374+
list.add(i)
375+
}
376+
}
377+
378+
@Benchmark
379+
fun benchmarkMethod(): Int {
380+
return list.sum()
381+
}
382+
383+
@TearDown
384+
fun cleanup() {
385+
list.clear()
386+
}
387+
}
321388

322389
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.
323390

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

326393
### Running Benchmarks
327394

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`.
330397

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).
332399

333400
### Benchmark Configuration Profiles
334401

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 the Toolkit.
336403
Additional profiles can be created as needed in the `configurations` section of the `benchmark` block:
337404

338405
```kotlin
@@ -370,4 +437,4 @@ These examples showcase various use cases and offer practical insights into the
370437

371438
## Contributing
372439

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 [Contribution Guidelines](CONTRIBUTING.md).

0 commit comments

Comments
 (0)