Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Benchmark opening close #1524

Draft
wants to merge 15 commits into
base: releases
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions benchmarks/androidApp/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ android {
testInstrumentationRunnerArguments["androidx.benchmark.suppressErrors"] = "EMULATOR,UNLOCKED"
// Disable profiling. See https://developer.android.com/studio/profile/microbenchmark-profile
testInstrumentationRunnerArguments["androidx.benchmark.profiling.mode"] = "None"
multiDexEnabled = true
}

testBuildType = "release"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*
* Copyright 2022 Realm Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.realm.kotlin.benchmarks.android

import androidx.benchmark.junit4.BenchmarkRule
import androidx.benchmark.junit4.measureRepeated
import io.realm.generated.openCloseRealmClassesMap
import io.realm.kotlin.Realm
import io.realm.kotlin.RealmConfiguration
import io.realm.kotlin.types.RealmObject
import org.junit.After
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import org.junit.runners.Parameterized
import kotlin.reflect.KClass

@RunWith(Parameterized::class)
class OpenRealmTestsGenerated(
private val className: String,
private val schemaSize: Int,
) {

companion object {
@JvmStatic
@Parameterized.Parameters(name = "{0}-schema-size-{1}")
fun initParameters(): Collection<Array<*>> {
val schemaSizes = listOf(0, 25, 50, 75, 100)

return openCloseRealmClassesMap.keys
.flatMap { className ->
schemaSizes.map { schemaSize -> arrayOf(className, schemaSize) }
}
.toList()
}
}

@get:Rule
val benchmarkRule = BenchmarkRule()

private lateinit var config: RealmConfiguration
private var realm: Realm? = null

@Before
fun setUp() {
val schema: Set<KClass<out RealmObject>> =
openCloseRealmClassesMap[className]!!
.subList(0, schemaSize)
.toSet()

config = RealmConfiguration.Builder(schema)
.directory("./build/benchmark-realms")
.build()
}

@After
fun tearDown() {
realm?.let {
Realm.deleteRealm(config)
}
}

@Test
fun openRealm() {
benchmarkRule.measureRepeated {
realm = Realm.open(config)
runWithTimingDisabled {
realm!!.close()
}
}
}
}
2 changes: 2 additions & 0 deletions benchmarks/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ buildscript {
}
gradlePluginPortal()
google()
mavenLocal()
mavenCentral()
}
dependencies {
Expand All @@ -28,6 +29,7 @@ allprojects {
maven("file://${rootProject.rootDir.absolutePath}/../packages/build/m2-buildrepo")
}
google()
mavenLocal()
mavenCentral()
}

Expand Down
4 changes: 2 additions & 2 deletions benchmarks/gradle.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#Gradle
org.gradle.jvmargs=-Xmx2048M -Dkotlin.daemon.jvm.options\="-Xmx2048M"

org.gradle.jvmargs=-Xmx4096M
daemon.jvm.options=-Xmx4096M
#Kotlin
kotlin.code.style=official

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
* Copyright 2022 Realm Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.realm.kotlin.benchmark

import io.realm.kotlin.Realm
import io.realm.kotlin.RealmConfiguration
import io.realm.kotlin.benchmarks.SCHEMAS
import io.realm.kotlin.benchmarks.SchemaSize
import io.realm.kotlin.types.RealmObject
import org.openjdk.jmh.annotations.Benchmark
import org.openjdk.jmh.annotations.BenchmarkMode
import org.openjdk.jmh.annotations.Fork
import org.openjdk.jmh.annotations.Level
import org.openjdk.jmh.annotations.Measurement
import org.openjdk.jmh.annotations.Mode
import org.openjdk.jmh.annotations.OutputTimeUnit
import org.openjdk.jmh.annotations.Param
import org.openjdk.jmh.annotations.Scope
import org.openjdk.jmh.annotations.Setup
import org.openjdk.jmh.annotations.State
import org.openjdk.jmh.annotations.TearDown
import org.openjdk.jmh.annotations.Warmup
import java.util.concurrent.TimeUnit
import kotlin.reflect.KClass

/**
* Benchmarking how fast it is to open a Realm.
* Since this should never happen in hot path, we attempt to benchmark the cold start case.
*
* TODO Need to verify if `SingleShotTime` also include setup and teardown: http://mail.openjdk.java.net/pipermail/jmh-dev/2014-September/001364.html
*/
@Fork(1)
@Warmup(iterations = 0)
@Measurement(iterations = 100)
@BenchmarkMode(Mode.SingleShotTime)
@OutputTimeUnit(TimeUnit.MILLISECONDS)
@State(Scope.Benchmark)
open class OpenRealmTestsGenerated {

@Param("SINGLE", "SMALL", "LARGE") // Must match enum names
var schemaSize: String = SchemaSize.SINGLE.name
var realm: Realm? = null
lateinit var config: RealmConfiguration

@Setup(Level.Invocation)
fun setUp() {
val schema: Set<KClass<out RealmObject>> = SCHEMAS[schemaSize]!!.schemaObjects
config = RealmConfiguration.Builder(schema)
.directory("./build/benchmark-realms")
.build()
}

@TearDown(Level.Invocation)
fun tearDown() {
realm?.let {
it.close()
Realm.deleteRealm(config)
}
}

@Benchmark()
fun openRealm() {
realm = Realm.open(config)
}
}
1 change: 1 addition & 0 deletions benchmarks/settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ pluginManagement {
repositories {
google()
gradlePluginPortal()
mavenLocal()
mavenCentral()
}
}
Expand Down
56 changes: 55 additions & 1 deletion benchmarks/shared/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import com.android.build.gradle.internal.tasks.factory.dependsOn
import io.realm.ClassGeneratorSpec
import io.realm.BenchmarkClassSuite

plugins {
kotlin("multiplatform")
// kotlin("native.cocoapods")
Expand All @@ -24,7 +28,7 @@ kotlin {
// baseName = "shared"
// }
// }

sourceSets {
val commonMain by getting {
dependencies {
Expand Down Expand Up @@ -57,11 +61,61 @@ kotlin {
}
}

// Create a task using the task type
val genClassesTask = tasks.create("classGen") {
val output = project.file("./src/commonMain/kotlin/")

// Clear out any previous contents
project.file("./src/commonMain/kotlin/io/realm/generated").deleteRecursively()

BenchmarkClassSuite(
name = "openCloseRealm",
packageName = "io.realm.generated",
output = output,
) {
addClassGeneratorSpec(
classCount = 100,
className = "OneString",
stringFieldCount = 1,
)
addClassGeneratorSpec(
classCount = 100,
className = "OneStringRealmList",
stringRealmListCount = 1,
)
addClassGeneratorSpec(
classCount = 100,
className = "TenStrings",
stringFieldCount = 10,
)
addClassGeneratorSpec(
classCount = 100,
className = "TenStringRealmLists",
stringRealmListCount = 10,
)
addClassGeneratorSpec(
classCount = 100,
className = "HundredStrings",
stringFieldCount = 100,
)
addClassGeneratorSpec(
classCount = 100,
className = "HundredStringRealmLists",
stringRealmListCount = 100,
)
}
}

afterEvaluate {
tasks.named("assemble").dependsOn(genClassesTask.name)
}

android {
compileSdk = Versions.Android.compileSdkVersion
sourceSets["main"].manifest.srcFile("src/androidMain/AndroidManifest.xml")
defaultConfig {
minSdk = Versions.Android.minSdk
targetSdk = Versions.Android.targetSdk
multiDexEnabled = true
}
}
Loading
Loading