Kotlin / kotlinx-benchmark

Kotlin multiplatform benchmarking toolkit
Apache License 2.0
499 stars 40 forks source link

Unclear how to use this lib on android projects (for kotlin code) #206

Open carstenhag opened 5 months ago

carstenhag commented 5 months ago

I tried to follow the Kotlin/JVM setup https://github.com/Kotlin/kotlinx-benchmark/blob/master/docs/kotlin-jvm-project-setup.md but unfortunately I could not get it working.

I have added the plugin & benchmark runtime dependency and added this to the build.gradle:

benchmark {
    targets {
        register("main") // also tried app.main
    }
}

I can import the annotations, but they are giving errors.

Screenshot 2024-05-03 at 08 54 00

Running ./gradlew benchmark returns > Configure project :app Warning: Cannot find a benchmark sourceSet 'main', ignoring.

carstenhag commented 5 months ago

I found at https://kotlinlang.org/docs/gsoc-2024.html#support-android-target-in-kotlinx-benchmark-medium-175-hrs that Android as target is not supported, but the benchmark I want to run is JVM/Kotlin-only.

fzhinkin commented 4 months ago

but the benchmark I want to run is JVM/Kotlin-only.

@carstenhag, do you mean it's a separate module with either Kotlin/JVM (kotlin("jvm")) or Java (id("java")) plugin applied to it?

I'm clarifying because android projects usually mean a project with the Android Gradle plugin applied to it, and unfortunately, kotlinx-benchmark does not currently support such projects at the moment.

carstenhag commented 4 months ago

I mean a project with AGP applied :). Oh, I see, thought the android support only referred to Android Instrumented stuff.

fzhinkin commented 4 months ago

Then it seems like, for now, the only option you have is to use androidx.microbenchmark library (sample project).

// Below is the advice you never asked for, but just in case:

Note that despite the fact that a code written in Kotlin can target both JVM and Android (which is, roughly speaking, JVM bytecode further compiled to DEX), performance characteristics may vary significantly on these platforms, and benchmarking results achieved on JVM may differ from what you may get on Android device (some function A could perform better than some other function B on JVM, but on Android B could become faster than A).

So, if you're interested in measuring the performance of code that will be used in an Android app, I recommend benchmarking it on an Android device.