Skip to content

Commit d605cb2

Browse files
committed
fix(lsp/java): add benchmarking module and more unit tests for JavaJarModelBuilder
1 parent 21a1708 commit d605cb2

File tree

35 files changed

+1069
-89
lines changed

35 files changed

+1069
-89
lines changed

.idea/compiler.xml

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ plugins {
3535
alias(libs.plugins.maven.publish) apply false
3636
alias(libs.plugins.gradle.publish) apply false
3737
alias(libs.plugins.protobuf) apply false
38+
alias(libs.plugins.benchmark) apply false
3839
}
3940

4041
buildscript {

common/build.gradle.kts

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,25 @@
1+
/*
2+
* This file is part of AndroidIDE.
3+
*
4+
* AndroidIDE is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation, either version 3 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* AndroidIDE is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with AndroidIDE. If not, see <https://www.gnu.org/licenses/>.
16+
*/
17+
118
import com.itsaky.androidide.build.config.BuildConfig
219

320
plugins {
4-
id("com.android.library")
5-
id("kotlin-android")
21+
alias(libs.plugins.android.library)
22+
alias(libs.plugins.kotlin.android)
623
}
724

825
android {

gradle/libs.versions.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ markwon = "4.6.2"
1919
maven-publish-plugin = "0.27.0"
2020
logback = "1.5.3"
2121
realm = "10.18.0"
22+
benchmark = "1.2.4"
23+
benchmark-junit4 = "1.2.4"
2224

2325
[libraries]
2426

@@ -166,6 +168,7 @@ android-gradle-plugin = { module = "com.android.tools.build:gradle", version.ref
166168
kotlin-gradle-plugin = { module = "org.jetbrains.kotlin:kotlin-gradle-plugin", version.ref = "kotlin" }
167169
nav-safe-args-gradle-plugin = { module = "androidx.navigation:navigation-safe-args-gradle-plugin", version.ref = "androidx-navigation" }
168170
maven-publish = { module = "com.vanniktech:gradle-maven-publish-plugin", version.ref = "maven-publish-plugin" }
171+
androidx-benchmark-junit4 = { group = "androidx.benchmark", name = "benchmark-junit4", version.ref = "benchmark-junit4" }
169172

170173
[plugins]
171174
android-application = { id = "com.android.application", version.ref = "agp" }
@@ -175,3 +178,4 @@ kotlin-jvm = { id = "org.jetbrains.kotlin.jvm", version.ref = "kotlin" }
175178
maven-publish = { id = "com.vanniktech.maven.publish.base", version.ref = "maven-publish-plugin" }
176179
gradle-publish = { id = "com.gradle.plugin-publish", version = "1.2.1" }
177180
protobuf = { id = "com.google.protobuf", version = "0.9.4" }
181+
benchmark = { id = "androidx.benchmark", version.ref = "benchmark" }

lsp/java/src/androidTest/java/com/itsaky/androidide/lsp/java/indexing/JavaClassCRUDTest.kt

Lines changed: 10 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,13 @@ package com.itsaky.androidide.lsp.java.indexing
2020
import androidx.test.ext.junit.runners.AndroidJUnit4
2121
import com.google.common.truth.Truth.assertThat
2222
import com.itsaky.androidide.lsp.java.indexing.models.JavaClass
23-
import com.itsaky.androidide.lsp.java.indexing.models.JavaField
24-
import com.itsaky.androidide.lsp.java.indexing.models.JavaMethod
23+
import com.itsaky.androidide.lsp.java.indexing.models.JavaIndexingRealmModule
2524
import com.itsaky.androidide.lsp.java.indexing.models.JavaType
25+
import com.itsaky.androidide.testing.android.rules.RealmDBTestRule
2626
import io.realm.RealmList
2727
import io.realm.RealmObject
2828
import openjdk.tools.classfile.AccessFlags
29+
import org.junit.Rule
2930
import org.junit.Test
3031
import org.junit.runner.RunWith
3132

@@ -35,33 +36,18 @@ import org.junit.runner.RunWith
3536
@RunWith(AndroidJUnit4::class)
3637
class JavaClassCRUDTest {
3738

39+
@Rule
40+
@JvmField
41+
val dbTestRule = RealmDBTestRule(JavaIndexingRealmModule())
42+
3843
@Test
3944
fun testSimpleJavaClassCRUD() {
40-
IndexingHelper("java-class-create").doWithRealm {
41-
val klass = JavaClass.newInstance(
42-
fqn = "com/itsaky/androidide/indexing/TestClass",
43-
name = "TestClass",
44-
packageName = "com/itsaky/androidide/indexing",
45-
accessFlags = AccessFlags.ACC_PUBLIC or AccessFlags.ACC_FINAL,
46-
superClassFqn = "java/lang/Object",
47-
superInterfacesFqn = RealmList(),
48-
fields = RealmList<JavaField>().apply {
49-
add(JavaField.newField("someString", JavaType.STRING, AccessFlags.ACC_PRIVATE))
50-
},
51-
methods = RealmList<JavaMethod>().apply {
52-
add(
53-
JavaMethod.newInstance(
54-
name = "getSomeString",
55-
paramsTypes = RealmList(),
56-
returnType = JavaType.STRING,
57-
accessFlags = AccessFlags.ACC_PUBLIC
58-
)
59-
)
60-
})
45+
dbTestRule.withDb("java-class-CRUD") {
46+
val klass = ModelBuilder.createJavaClass()
6147

6248
// CREATE
6349
executeTransaction {
64-
insert(klass)
50+
it.insert(klass)
6551
}
6652

6753
var dbKlass: JavaClass? = null

lsp/java/src/androidTest/java/com/itsaky/androidide/lsp/java/indexing/JavaIndexerTest.kt

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,28 +17,50 @@
1717

1818
package com.itsaky.androidide.lsp.java.indexing
1919

20+
import android.content.Context
21+
import androidx.test.core.app.ApplicationProvider
2022
import androidx.test.ext.junit.runners.AndroidJUnit4
2123
import com.blankj.utilcode.util.ConvertUtils
24+
import com.google.common.truth.Truth
25+
import com.itsaky.androidide.lsp.java.indexing.models.JavaIndexingRealmModule
26+
import com.itsaky.androidide.testing.android.rules.RealmDBTestRule
2227
import io.realm.RealmModel
28+
import org.junit.Rule
2329
import org.junit.Test
2430
import org.junit.runner.RunWith
31+
import java.io.File
2532

2633
/**
2734
* @author Akash Yadav
2835
*/
2936
@RunWith(AndroidJUnit4::class)
3037
class JavaIndexerTest {
3138

39+
@Rule
40+
@JvmField
41+
val dbTestRule = RealmDBTestRule(JavaIndexingRealmModule())
42+
43+
val androidJar: File by lazy {
44+
val context = ApplicationProvider.getApplicationContext<Context>()
45+
val file = File.createTempFile("ajar", null, context.cacheDir)
46+
context.assets.open("android.jar").use { asset ->
47+
Truth.assertThat(asset).isNotNull()
48+
file.outputStream().buffered().use { out ->
49+
asset.copyTo(out)
50+
out.flush()
51+
}
52+
}
53+
file
54+
}
55+
3256
@Test
3357
fun testSimpleAndroidJarIndexingDurationCheck() {
34-
val helper = IndexingHelper("android-jar-classes.realm")
35-
val worker = JavaIndexModelBuilder(helper.androidJar)
58+
val worker = JavaJarModelBuilder(androidJar)
3659
val batches = mutableMapOf<Class<*>, MutableList<RealmModel>>()
3760

38-
val totalDuration = 0L
3961
var dbWriteDuration = 0L
4062

41-
helper.doWithRealm {
63+
dbTestRule.withDb("android-jar-classes") {
4264
val totalStart = System.currentTimeMillis()
4365
worker.consumeTypes { type ->
4466
val batched = batches.computeIfAbsent(type.javaClass) { mutableListOf() }
@@ -68,7 +90,7 @@ class JavaIndexerTest {
6890
println(
6991
"Took ${dbWriteDuration}ms to write android.jar (${
7092
ConvertUtils.byte2FitMemorySize(
71-
helper.androidJar.length()
93+
androidJar.length()
7294
)
7395
}) classes to Realm"
7496
)
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* This file is part of AndroidIDE.
3+
*
4+
* AndroidIDE is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation, either version 3 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* AndroidIDE is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with AndroidIDE. If not, see <https://www.gnu.org/licenses/>.
16+
*/
17+
18+
package com.itsaky.androidide.lsp.java.indexing
19+
20+
import com.itsaky.androidide.lsp.java.indexing.models.JavaClass
21+
import com.itsaky.androidide.lsp.java.indexing.models.JavaField
22+
import com.itsaky.androidide.lsp.java.indexing.models.JavaMethod
23+
import com.itsaky.androidide.lsp.java.indexing.models.JavaType
24+
import io.realm.RealmList
25+
import openjdk.tools.classfile.AccessFlags
26+
27+
/**
28+
* @author Akash Yadav
29+
*/
30+
object ModelBuilder {
31+
32+
fun createJavaClass() = JavaClass.newInstance(
33+
fqn = "com/itsaky/androidide/indexing/TestClass",
34+
name = "TestClass",
35+
packageName = "com/itsaky/androidide/indexing",
36+
accessFlags = AccessFlags.ACC_PUBLIC or AccessFlags.ACC_FINAL,
37+
superClassFqn = "java/lang/Object",
38+
superInterfacesFqn = RealmList(),
39+
fields = RealmList<JavaField>().apply {
40+
add(JavaField.newField("someString", JavaType.STRING, AccessFlags.ACC_PRIVATE))
41+
},
42+
methods = RealmList<JavaMethod>().apply {
43+
add(
44+
JavaMethod.newInstance(
45+
name = "getSomeString",
46+
paramsTypes = RealmList(),
47+
returnType = JavaType.STRING,
48+
accessFlags = AccessFlags.ACC_PUBLIC
49+
)
50+
)
51+
})
52+
}

0 commit comments

Comments
 (0)