Skip to content

Commit

Permalink
feat: Add reflection benchmarks
Browse files Browse the repository at this point in the history
  • Loading branch information
haroldadmin committed Sep 20, 2019
1 parent 3e140b3 commit c801702
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 6 deletions.
5 changes: 3 additions & 2 deletions benchmark/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ android {
targetSdkVersion buildConfig.targetSdk
versionCode buildConfig.versionCode
versionName buildConfig.versionName
testInstrumentationRunner 'androidx.benchmark.AndroidBenchmarkRunner'
testInstrumentationRunner 'androidx.benchmark.junit4.AndroidBenchmarkRunner'
}

buildTypes {
Expand All @@ -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"
}
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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
}
Original file line number Diff line number Diff line change
@@ -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
}
Original file line number Diff line number Diff line change
@@ -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
}
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit c801702

Please sign in to comment.