fix(lsp/java): add benchmarking module and more unit tests for JavaJarModelBuilder

This commit is contained in:
Akash Yadav
2024-05-26 22:19:52 +05:30
parent 21a1708c19
commit d605cb21be
35 changed files with 1069 additions and 89 deletions
+9 -4
View File
@@ -18,8 +18,8 @@
import com.itsaky.androidide.build.config.BuildConfig
plugins {
id("com.android.library")
kotlin("android")
alias(libs.plugins.android.library)
alias(libs.plugins.kotlin.android)
}
android {
@@ -38,6 +38,12 @@ configurations.api {
}
dependencies {
// compileOnly so that we can provide the APIs for the related test rules
// if a module needs to use those APIs, it needs to explicitly add the database module as a
// dependency
compileOnly(projects.subprojects.database)
api(libs.google.protobuf)
api(libs.tests.androidx.espresso.core)
api(libs.tests.androidx.espresso.contrib)
@@ -52,9 +58,8 @@ dependencies {
exclude("org.jetbrains.kotlin")
}
api(projects.testing.common)
api(projects.buildInfo)
api(projects.common)
api(projects.shared)
api(projects.testing.common)
}
@@ -0,0 +1,37 @@
/*
* This file is part of AndroidIDE.
*
* AndroidIDE is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* AndroidIDE is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with AndroidIDE. If not, see <https://www.gnu.org/licenses/>.
*/
package com.itsaky.androidide.testing.android.rules
import android.content.Context
import androidx.test.core.app.ApplicationProvider
import com.itsaky.androidide.utils.Environment
import org.junit.rules.TestRule
/**
* Abstract class for Android testing rules.
*
* @author Akash Yadav
*/
abstract class AbstractAndroidTestRule : TestRule {
init {
Environment.init(ApplicationProvider.getApplicationContext())
}
protected val context: Context
get() = ApplicationProvider.getApplicationContext()
}
@@ -0,0 +1,61 @@
/*
* This file is part of AndroidIDE.
*
* AndroidIDE is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* AndroidIDE is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with AndroidIDE. If not, see <https://www.gnu.org/licenses/>.
*/
package com.itsaky.androidide.testing.android.rules
import com.itsaky.androidide.db.IRealmProvider
import com.itsaky.androidide.testing.android.util.NoOpStatement
import io.realm.Realm
import io.realm.log.LogLevel
import io.realm.log.RealmLog
import org.junit.runner.Description
import org.junit.runners.model.Statement
/**
* Rule for Realm DB tests.
*
* @author Akash Yadav
*/
class RealmDBTestRule(
val baseModule: Any? = null,
vararg val additionalModules: Array<Any>,
) : AbstractAndroidTestRule() {
inline fun withDb(dbName: String, deleteDbAfterTest: Boolean = true, action: Realm.() -> Unit) {
val name = dbName.replace(IRealmProvider.PATH_SEPARATOR, '-')
val realm = IRealmProvider.instance().get("/indexing/java/$name") {
baseModule?.let { baseModule ->
modules(baseModule, *additionalModules)
}
}
try {
realm.action()
} finally {
if (deleteDbAfterTest) {
realm.configuration.realmDirectory.deleteRecursively()
}
}
}
override fun apply(base: Statement?, description: Description?): Statement {
Realm.init(context.applicationContext)
RealmLog.setLevel(LogLevel.ALL)
base?.evaluate()
return NoOpStatement()
}
}
@@ -0,0 +1,30 @@
/*
* This file is part of AndroidIDE.
*
* AndroidIDE is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* AndroidIDE is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with AndroidIDE. If not, see <https://www.gnu.org/licenses/>.
*/
package com.itsaky.androidide.testing.android.util
import org.junit.runners.model.Statement
/**
* A no-op statement.
*
* @author Akash Yadav
*/
class NoOpStatement : Statement() {
override fun evaluate() {
}
}
+1
View File
@@ -0,0 +1 @@
/build
@@ -0,0 +1,37 @@
# Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html
# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}
# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable
# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile
-dontobfuscate
-ignorewarnings
-keepattributes *Annotation*
-dontnote junit.framework.**
-dontnote junit.runner.**
-dontwarn androidx.test.**
-dontwarn org.junit.**
-dontwarn org.hamcrest.**
-dontwarn com.squareup.javawriter.JavaWriter
-keepclasseswithmembers @org.junit.runner.RunWith public class *
+70
View File
@@ -0,0 +1,70 @@
/*
* This file is part of AndroidIDE.
*
* AndroidIDE is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* AndroidIDE is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with AndroidIDE. If not, see <https://www.gnu.org/licenses/>.
*/
import com.itsaky.androidide.build.config.BuildConfig
plugins {
alias(libs.plugins.android.library)
alias(libs.plugins.kotlin.android)
alias(libs.plugins.benchmark)
}
android {
namespace = "${BuildConfig.packageName}.benchmark"
sourceSets {
getByName("androidTest") {
assets.srcDirs(rootProject.file("subprojects/framework-stubs/libs"))
}
}
defaultConfig {
testInstrumentationRunner = "androidx.benchmark.junit4.AndroidBenchmarkRunner"
}
testBuildType = "release"
buildTypes {
debug {
// Since isDebuggable can"t be modified by gradle for library modules,
// it must be done in a manifest - see src/androidTest/AndroidManifest.xml
isMinifyEnabled = true
proguardFiles(
getDefaultProguardFile("proguard-android-optimize.txt"),
"benchmark-proguard-rules.pro"
)
}
release {
isDefault = true
}
}
}
dependencies {
androidTestImplementation(libs.tests.androidx.test.runner)
androidTestImplementation(libs.tests.androidx.junit)
androidTestImplementation(libs.tests.junit)
androidTestImplementation(libs.tests.google.truth)
androidTestImplementation(libs.androidx.benchmark.junit4)
androidTestImplementation(projects.common)
androidTestImplementation(projects.lsp.java)
androidTestImplementation(projects.shared)
androidTestImplementation(projects.subprojects.database)
androidTestImplementation(projects.subprojects.indexing)
}
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<!--
Important: disable debugging for accurate performance results
In a com.android.library project, this flag must be disabled from this
manifest, as it is not possible to override this flag from Gradle.
-->
<application
android:debuggable="false"
tools:ignore="HardcodedDebugMode"
tools:replace="android:debuggable" />
</manifest>
@@ -0,0 +1,61 @@
/*
* This file is part of AndroidIDE.
*
* AndroidIDE is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* AndroidIDE is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with AndroidIDE. If not, see <https://www.gnu.org/licenses/>.
*/
package com.itsaky.androidide.benchmark.lsp.java
import android.content.Context
import androidx.benchmark.junit4.BenchmarkRule
import androidx.benchmark.junit4.measureRepeated
import androidx.test.core.app.ApplicationProvider
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.google.common.truth.Truth.assertThat
import com.itsaky.androidide.lsp.java.indexing.JavaJarModelBuilder
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import java.io.File
/**
* @author Akash Yadav
*/
@RunWith(AndroidJUnit4::class)
class JavaIndexingWorkerBenchmark {
@get:Rule
val benchmarkRule = BenchmarkRule()
val androidJar: File by lazy {
val context = ApplicationProvider.getApplicationContext<Context>()
val file = File.createTempFile("ajar", null, context.cacheDir)
context.assets.open("android.jar").use { asset ->
assertThat(asset).isNotNull()
file.outputStream().buffered().use { out ->
asset.copyTo(out)
out.flush()
}
}
file
}
@Test
fun benchmarkJavaIndexingWorker() {
val worker = JavaJarModelBuilder(androidJar)
benchmarkRule.measureRepeated {
worker.buildTypes()
}
}
}
+2 -1
View File
@@ -23,10 +23,11 @@ plugins {
}
dependencies {
implementation(project(":subprojects:tooling-api-impl"))
kapt(libs.google.auto.service)
implementation(libs.google.auto.service.annotations)
api(libs.tests.junit)
api(projects.logger)
implementation(projects.subprojects.toolingApiImpl)
}
+3 -3
View File
@@ -18,8 +18,8 @@
import com.itsaky.androidide.build.config.BuildConfig
plugins {
id("com.android.library")
id("kotlin-android")
alias(libs.plugins.android.library)
alias(libs.plugins.kotlin.android)
}
android {
@@ -37,8 +37,8 @@ dependencies {
api(projects.common)
api(projects.eventbusAndroid)
api(projects.lsp.api)
api(projects.testing.common)
api(projects.testing.android)
api(projects.testing.tooling)
api(projects.testing.unit)
}
@@ -15,7 +15,7 @@
* along with AndroidIDE. If not, see <https://www.gnu.org/licenses/>.
*/
package com.itsaky.androidide.lsp.api
package com.itsaky.androidide.lsp.testing
import android.content.Context
import com.google.common.truth.Truth.assertThat
@@ -27,6 +27,7 @@ import com.itsaky.androidide.eventbus.events.editor.DocumentOpenEvent
import com.itsaky.androidide.eventbus.events.file.FileDeletionEvent
import com.itsaky.androidide.eventbus.events.file.FileRenameEvent
import com.itsaky.androidide.lookup.Lookup
import com.itsaky.androidide.lsp.api.ILanguageServerRegistry
import com.itsaky.androidide.managers.PreferenceManager
import com.itsaky.androidide.models.Position
import com.itsaky.androidide.models.Range
@@ -14,7 +14,7 @@
* You should have received a copy of the GNU General Public License
* along with AndroidIDE. If not, see <https://www.gnu.org/licenses/>.
*/
package com.itsaky.androidide.lsp.api.util
package com.itsaky.androidide.lsp.testing.util
import com.itsaky.androidide.utils.ILogger
import com.itsaky.androidide.utils.ILogger.Level