ProductPromotion
Logo

Kotlin

made by https://0x3d.site

GitHub - Kotlin/kotlinx-benchmark: Kotlin multiplatform benchmarking toolkit
Kotlin multiplatform benchmarking toolkit. Contribute to Kotlin/kotlinx-benchmark development by creating an account on GitHub.
Visit Site

GitHub - Kotlin/kotlinx-benchmark: Kotlin multiplatform benchmarking toolkit

GitHub - Kotlin/kotlinx-benchmark: Kotlin multiplatform benchmarking toolkit

kotlinx-benchmark

Kotlin Alpha JetBrains incubator project GitHub license Build status Maven Central Gradle Plugin Portal

kotlinx-benchmark is a toolkit for running benchmarks for multiplatform code written in Kotlin. It is designed to work with Kotlin/JVM, Kotlin/JS, Kotlin/Native, and Kotlin/Wasm (experimental) targets.

To get started, ensure you're using Kotlin 2.0.0 or newer and Gradle 7.4 or newer.

Features

  • Low noise and reliable results
  • Statistical analysis
  • Detailed performance reports

Table of contents

Setting Up a Kotlin Multiplatform Project for Benchmarking

To configure a Kotlin Multiplatform project for benchmarking, follow the steps below. If you want to benchmark only Kotlin/JVM and Java code, you may refer to our comprehensive guide dedicated to setting up benchmarking in those specific project types.

  1. Applying Benchmark Plugin: Apply the benchmark plugin.

    // build.gradle.kts
    plugins {
        id("org.jetbrains.kotlinx.benchmark") version "0.4.12"
    }
    
  2. Specifying Plugin Repository: Ensure you have the Gradle Plugin Portal for plugin lookup in the list of repositories:

    // settings.gradle.kts
    pluginManagement {
        repositories {
            gradlePluginPortal()
        }
    }
    
  3. Adding Runtime Dependency: Next, add the kotlinx-benchmark-runtime dependency to the common source set:

    // build.gradle.kts
    kotlin {
        sourceSets {
            commonMain {
                dependencies {
                    implementation("org.jetbrains.kotlinx:kotlinx-benchmark-runtime:0.4.12")
                }
            }
        }
    }
    
  4. Specifying Runtime Repository: Ensure you have mavenCentral() for dependencies lookup in the list of repositories:

    // build.gradle.kts
    repositories {
        mavenCentral()
    }
    
  1. Applying Benchmark Plugin: Apply the benchmark plugin.

    // build.gradle
    plugins {
        id 'org.jetbrains.kotlinx.benchmark' version '0.4.12'
    }
    
  2. Specifying Plugin Repository: Ensure you have the Gradle Plugin Portal for plugin lookup in the list of repositories:

    // settings.gradle
    pluginManagement {
        repositories {
            gradlePluginPortal()
        }
    }
    
  3. Adding Runtime Dependency: Next, add the kotlinx-benchmark-runtime dependency to the common source set:

    // build.gradle
    kotlin {
        sourceSets {
            commonMain {
                dependencies {
                    implementation 'org.jetbrains.kotlinx:kotlinx-benchmark-runtime:0.4.12'
                }
            }
        }
    }
    
  4. Specifying Runtime Repository: Ensure you have mavenCentral() for dependencies lookup in the list of repositories:

    // build.gradle
    repositories {
        mavenCentral()
    }
    

Target-specific configurations

To run benchmarks on a platform ensure your Kotlin Multiplatform project targets that platform. For different platforms, there may be distinct requirements and settings that need to be configured. The guide below contains the steps needed to configure each supported platform for benchmarking.

Kotlin/JVM

To run benchmarks in Kotlin/JVM:

  1. Create a JVM target:

    // build.gradle.kts
    kotlin {
        jvm()
    }
    
  2. Register jvm as a benchmark target:

    // build.gradle.kts
    benchmark {
        targets { 
            register("jvm")
        }
    }
    
  3. Apply allopen plugin to ensure your benchmark classes and methods are open.

    // build.gradle.kts
    plugins {
        kotlin("plugin.allopen") version "2.0.20"
    }
    
    allOpen {
        annotation("org.openjdk.jmh.annotations.State")
    }
    

    Assume that you've annotated each of your benchmark classes with @State(Scope.Benchmark):

    // MyBenchmark.kt
    @State(Scope.Benchmark)
    class MyBenchmark {
        // Benchmarking-related methods and variables
        @Benchmark
        fun benchmarkMethod() {
            // benchmarking logic
        }
    }
    

    In Kotlin, classes are final by default, which means they can't be overridden. This conflicts with the Java Microbenchmark Harness (JMH) operation, which kotlinx-benchmark uses under the hood for running benchmarks on JVM. JMH requires benchmark classes and methods to be open to be able to generate subclasses and conduct the benchmark.

    This is where the allopen plugin comes into play. With the plugin applied, any class annotated with @State is treated as open, which allows JMH to work as intended:

    // build.gradle.kts
    plugins {
        kotlin("plugin.allopen") version "2.0.20"
    }
    
    allOpen {
        annotation("org.openjdk.jmh.annotations.State")
    }
    

    This configuration ensures that your MyBenchmark class and its benchmarkMethod function are treated as open.

    You can alternatively mark your benchmark classes and methods open manually, but using the allopen plugin enhances code maintainability.

Kotlin/JS

To run benchmarks in Kotlin/JS:

  1. Create a JS target with Node.js execution environment:

    // build.gradle.kts
    kotlin {
        js { 
            nodejs() 
        }
    }
    
  2. Register js as a benchmark target:

    // build.gradle.kts
    benchmark {
        targets {
            register("js")
        }
    }
    

Kotlin/Native

To run benchmarks in Kotlin/Native:

  1. Create a Native target:

    // build.gradle.kts
    kotlin {
        linuxX64()
    }
    
  2. Register linuxX64 as a benchmark target:

    // build.gradle.kts
    benchmark {
        targets {
            register("linuxX64")
        }
    }
    

It is possible to register multiple native targets. However, benchmarks can be executed only for the host target. This library supports all targets supported by the Kotlin/Native compiler.

Kotlin/Wasm

To run benchmarks in Kotlin/Wasm:

  1. Create a Wasm target with Node.js execution environment:

    // build.gradle.kts
    kotlin {
        wasm { 
            nodejs() 
        }
    }
    
  2. Register wasm as a benchmark target:

    // build.gradle.kts
    benchmark {
        targets {
            register("wasm")
        }
    }
    

Note: Kotlin/Wasm is an experimental compilation target for Kotlin. It may be dropped or changed at any time. Refer to Kotlin/Wasm documentation for up-to-date information about the target stability.

Writing Benchmarks

After setting up your project and configuring targets, you can start writing benchmarks. As an example, let's write a simplified benchmark that tests how fast we can add up numbers in an ArrayList:

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

    @State(Scope.Benchmark)
    class MyBenchmark {
    
    }
    
  2. Set Up Variables: Define variables needed for the benchmark.

    private val size = 10
    private val list = ArrayList<Int>()
    
  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.

    @Setup
    fun prepare() {
        for (i in 0..<size) {
            list.add(i)
        }
    }
    
    @TearDown
    fun cleanup() {
        list.clear()
    }
    
  4. Define Benchmark Methods: Next, create methods that you would like to be benchmarked within this class and annotate them with @Benchmark.

    @Benchmark
    fun benchmarkMethod(): Int {
        return list.sum()
    }
    

Your final benchmark class will look something like this:

import kotlinx.benchmark.*

@State(Scope.Benchmark)
class MyBenchmark {
    private val size = 10
    private val list = ArrayList<Int>()

    @Setup
    fun prepare() {
        for (i in 0..<size) {
            list.add(i)
        }
    }

    @TearDown
    fun cleanup() {
        list.clear()
    }

    @Benchmark
    fun benchmarkMethod(): Int {
        return list.sum()
    }
}

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.

See writing benchmarks for a complete guide for writing benchmarks.

Running Benchmarks

To run your benchmarks in all registered platforms, run benchmark Gradle task in your project. To run only on a specific platform, run <target-name>Benchmark, e.g., jvmBenchmark.

For more details about the tasks created by the kotlinx-benchmark plugin, refer to this guide.

Benchmark Configuration Profiles

The kotlinx-benchmark library provides the ability to create multiple configuration profiles. The main configuration is already created by the toolkit. Additional profiles can be created as needed in the configurations section of the benchmark block:

// build.gradle.kts
benchmark {
    configurations {
        named("main") {
            warmups = 20
            iterations = 10
            iterationTime = 3
            iterationTimeUnit = "s"
        }
        register("smoke") {
            include("<pattern of fully qualified name>")
            warmups = 5
            iterations = 3
            iterationTime = 500
            iterationTimeUnit = "ms"
        }
    }
}

Refer to our comprehensive guide to learn about configuration options and how they affect benchmark execution.

Separate source set for benchmarks

Often you want to have benchmarks in the same project, but separated from main code, much like tests. Refer to our detailed documentation on configuring your project to set up a separate source set for benchmarks.

Examples

To help you better understand how to use the kotlinx-benchmark library, we've provided an examples subproject. These examples showcase various use cases and offer practical insights into the library's functionality.

Contributing

We welcome contributions to kotlinx-benchmark! If you want to contribute, please refer to our Contribution Guidelines.

More Resources
to explore the angular.

mail [email protected] to add your project or resources here 🔥.

Related Articles
to learn about angular.

FAQ's
to learn more about Angular JS.

mail [email protected] to add more queries here 🔍.

More Sites
to check out once you're finished browsing here.

0x3d
https://www.0x3d.site/
0x3d is designed for aggregating information.
NodeJS
https://nodejs.0x3d.site/
NodeJS Online Directory
Cross Platform
https://cross-platform.0x3d.site/
Cross Platform Online Directory
Open Source
https://open-source.0x3d.site/
Open Source Online Directory
Analytics
https://analytics.0x3d.site/
Analytics Online Directory
JavaScript
https://javascript.0x3d.site/
JavaScript Online Directory
GoLang
https://golang.0x3d.site/
GoLang Online Directory
Python
https://python.0x3d.site/
Python Online Directory
Swift
https://swift.0x3d.site/
Swift Online Directory
Rust
https://rust.0x3d.site/
Rust Online Directory
Scala
https://scala.0x3d.site/
Scala Online Directory
Ruby
https://ruby.0x3d.site/
Ruby Online Directory
Clojure
https://clojure.0x3d.site/
Clojure Online Directory
Elixir
https://elixir.0x3d.site/
Elixir Online Directory
Elm
https://elm.0x3d.site/
Elm Online Directory
Lua
https://lua.0x3d.site/
Lua Online Directory
C Programming
https://c-programming.0x3d.site/
C Programming Online Directory
C++ Programming
https://cpp-programming.0x3d.site/
C++ Programming Online Directory
R Programming
https://r-programming.0x3d.site/
R Programming Online Directory
Perl
https://perl.0x3d.site/
Perl Online Directory
Java
https://java.0x3d.site/
Java Online Directory
Kotlin
https://kotlin.0x3d.site/
Kotlin Online Directory
PHP
https://php.0x3d.site/
PHP Online Directory
React JS
https://react.0x3d.site/
React JS Online Directory
Angular
https://angular.0x3d.site/
Angular JS Online Directory