diff --git a/benchmark/build.gradle b/benchmark/build.gradle index 4885873..f28db34 100644 --- a/benchmark/build.gradle +++ b/benchmark/build.gradle @@ -9,7 +9,7 @@ android { targetSdkVersion buildConfig.targetSdk versionCode buildConfig.versionCode versionName buildConfig.versionName - testInstrumentationRunner 'androidx.benchmark.AndroidBenchmarkRunner' + testInstrumentationRunner 'androidx.benchmark.junit4.AndroidBenchmarkRunner' } buildTypes { @@ -33,10 +33,11 @@ dependencies { implementation project(path: ':vector') implementation libs.kotlinStdLib + implementation libs.kotlinReflect implementation libs.coroutinesCore implementation libs.coroutinesAndroid androidTestImplementation libs.androidxTestRunner androidTestImplementation libs.androidxTestExt androidTestImplementation libs.junit - androidTestImplementation 'androidx.benchmark:benchmark:1.0.0-alpha01' + androidTestImplementation "androidx.benchmark:benchmark-junit4:1.0.0-alpha06" } diff --git a/benchmark/src/androidTest/java/com/haroldadmin/vector/benchmark/actorStateStore/Benchmark.kt b/benchmark/src/androidTest/java/com/haroldadmin/vector/benchmark/actorStateStore/Benchmark.kt index db7b845..020c25b 100644 --- a/benchmark/src/androidTest/java/com/haroldadmin/vector/benchmark/actorStateStore/Benchmark.kt +++ b/benchmark/src/androidTest/java/com/haroldadmin/vector/benchmark/actorStateStore/Benchmark.kt @@ -1,7 +1,7 @@ package com.haroldadmin.vector.benchmark.actorStateStore -import androidx.benchmark.BenchmarkRule -import androidx.benchmark.measureRepeated +import androidx.benchmark.junit4.BenchmarkRule +import androidx.benchmark.junit4.measureRepeated import androidx.test.ext.junit.runners.AndroidJUnit4 import com.haroldadmin.vector.VectorState import org.junit.Ignore diff --git a/benchmark/src/androidTest/java/com/haroldadmin/vector/benchmark/reflection/NewInstanceCreation.kt b/benchmark/src/androidTest/java/com/haroldadmin/vector/benchmark/reflection/NewInstanceCreation.kt new file mode 100644 index 0000000..f26f42e --- /dev/null +++ b/benchmark/src/androidTest/java/com/haroldadmin/vector/benchmark/reflection/NewInstanceCreation.kt @@ -0,0 +1,29 @@ +package com.haroldadmin.vector.benchmark.reflection + +import androidx.benchmark.junit4.BenchmarkRule +import androidx.benchmark.junit4.measureRepeated +import androidx.test.ext.junit.runners.AndroidJUnit4 +import org.junit.Ignore +import org.junit.Rule +import org.junit.Test +import org.junit.runner.RunWith +import kotlin.reflect.full.createInstance + +@RunWith(AndroidJUnit4::class) +@Ignore("Don't run benchmarks with regular builds") +internal class NewInstanceCreation { + + @get:Rule val benchmarkRule = BenchmarkRule() + + @Test + fun kotlinReflectionNewInstanceCreation() = benchmarkRule.measureRepeated { + val instance = ImplementingClass::class.createInstance() + } + + @Test + fun javaReflectionNewInstanceCreation() = benchmarkRule.measureRepeated { + val instance = ImplementingClass::class.java.newInstance() + } + + // Java Reflection version is 6x-7x faster than the kotlin version +} \ No newline at end of file diff --git a/benchmark/src/androidTest/java/com/haroldadmin/vector/benchmark/reflection/SuperClassCheckBenchMark.kt b/benchmark/src/androidTest/java/com/haroldadmin/vector/benchmark/reflection/SuperClassCheckBenchMark.kt new file mode 100644 index 0000000..ba491cc --- /dev/null +++ b/benchmark/src/androidTest/java/com/haroldadmin/vector/benchmark/reflection/SuperClassCheckBenchMark.kt @@ -0,0 +1,40 @@ +package com.haroldadmin.vector.benchmark.reflection + +import androidx.benchmark.junit4.BenchmarkRule +import androidx.benchmark.junit4.measureRepeated +import androidx.test.ext.junit.runners.AndroidJUnit4 +import org.junit.Rule +import org.junit.Test +import org.junit.runner.RunWith +import kotlin.reflect.full.isSuperclassOf + +internal interface SuperTypeInterface +internal class ImplementingClass : SuperTypeInterface + +// @Ignore("Don't run benchmarks for regular builds") +@RunWith(AndroidJUnit4::class) +internal class SuperClassCheckBenchMark { + + @get:Rule + val benchmarkRule = BenchmarkRule() + + @Test + fun kotlinReflectionSuperClassCheck() { + benchmarkRule.measureRepeated { + val superClass = SuperTypeInterface::class + val subClass = ImplementingClass::class + superClass.isSuperclassOf(subClass) + } + } + + @Test + fun javaReflectionSuperClassCheck() { + benchmarkRule.measureRepeated { + val superClass = SuperTypeInterface::class.java + val subClass = ImplementingClass::class.java + superClass.isAssignableFrom(subClass) + } + } + + // Result is that Kotlin Reflection is around 150x-200x slower than Java Reflection for this particular check +} \ No newline at end of file diff --git a/benchmark/src/androidTest/java/com/haroldadmin/vector/benchmark/reflection/UnderlyingClassAccess.kt b/benchmark/src/androidTest/java/com/haroldadmin/vector/benchmark/reflection/UnderlyingClassAccess.kt new file mode 100644 index 0000000..66b7721 --- /dev/null +++ b/benchmark/src/androidTest/java/com/haroldadmin/vector/benchmark/reflection/UnderlyingClassAccess.kt @@ -0,0 +1,38 @@ +package com.haroldadmin.vector.benchmark.reflection + +import androidx.benchmark.junit4.BenchmarkRule +import androidx.benchmark.junit4.measureRepeated +import androidx.test.ext.junit.runners.AndroidJUnit4 +import org.junit.Rule +import org.junit.Test +import org.junit.runner.RunWith + +// @Ignore("Don't run benchmarks with regular builds") +@RunWith(AndroidJUnit4::class) +class UnderlyingClassAccess { + + @get:Rule val benchmarkRule = BenchmarkRule() + + @Test + fun kotlinClassAccess() { + benchmarkRule.measureRepeated { + val klass = ImplementingClass::class + } + } + + @Test + fun javaClassAccess() { + benchmarkRule.measureRepeated { + val clazz = ImplementingClass::class.java + } + } + + @Test + fun javaClassToKotlin() { + benchmarkRule.measureRepeated { + val klass = ImplementingClass::class.java.kotlin + } + } + + // Accessing the underlying java class or kotlin class is equivalent in performance +} \ No newline at end of file diff --git a/benchmark/src/androidTest/java/com/haroldadmin/vector/benchmark/regularStateStore/StateStoreBenchmark.kt b/benchmark/src/androidTest/java/com/haroldadmin/vector/benchmark/regularStateStore/StateStoreBenchmark.kt index 4edc432..3c489e7 100644 --- a/benchmark/src/androidTest/java/com/haroldadmin/vector/benchmark/regularStateStore/StateStoreBenchmark.kt +++ b/benchmark/src/androidTest/java/com/haroldadmin/vector/benchmark/regularStateStore/StateStoreBenchmark.kt @@ -1,7 +1,7 @@ package com.haroldadmin.vector.benchmark.regularStateStore -import androidx.benchmark.BenchmarkRule -import androidx.benchmark.measureRepeated +import androidx.benchmark.junit4.BenchmarkRule +import androidx.benchmark.junit4.measureRepeated import androidx.test.ext.junit.runners.AndroidJUnit4 import com.haroldadmin.vector.VectorState import kotlinx.coroutines.runBlocking