Skip to content

Set up benchmark app with Compose #2817

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ buildscript {
classpath(Plugins.kotlinGradlePlugin)
classpath(Plugins.navSafeArgsGradlePlugin)
classpath(Plugins.rulerGradlePlugin)
classpath(Plugins.kotlinComposePlugin)
classpath(Plugins.kotlinSerializationPlugin)
}
}

Expand Down
6 changes: 6 additions & 0 deletions buildSrc/src/main/kotlin/Plugins.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ object Plugins {
const val dokka = "org.jetbrains.dokka"
const val kotlin = "kotlin"
const val kotlinAndroid = "kotlin-android"
const val kotlinCompose = "org.jetbrains.kotlin.plugin.compose"
const val kotlinKsp = "com.google.devtools.ksp"
const val kotlinSerialization = "org.jetbrains.kotlin.plugin.serialization"
const val mavenPublish = "maven-publish"
const val fladle = "com.osacky.fladle"
const val navSafeArgs = "androidx.navigation.safeargs.kotlin"
Expand All @@ -36,6 +38,10 @@ object Plugins {
const val benchmarkGradlePlugin =
"androidx.benchmark:benchmark-gradle-plugin:${Versions.benchmarkPlugin}"
const val kotlinGradlePlugin = "org.jetbrains.kotlin:kotlin-gradle-plugin:${Versions.kotlin}"
const val kotlinComposePlugin =
"org.jetbrains.kotlin.plugin.compose:org.jetbrains.kotlin.plugin.compose.gradle.plugin:${Versions.kotlin}"
const val kotlinSerializationPlugin =
"org.jetbrains.kotlin.plugin.serialization:org.jetbrains.kotlin.plugin.serialization.gradle.plugin:${Versions.kotlin}"
const val navSafeArgsGradlePlugin = "androidx.navigation:navigation-safe-args-gradle-plugin:2.6.0"
const val rulerGradlePlugin = "com.spotify.ruler:ruler-gradle-plugin:1.2.1"
const val flankGradlePlugin = "com.osacky.flank.gradle:fladle:0.17.4"
Expand Down
2 changes: 1 addition & 1 deletion demo/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ dependencies {
implementation(libs.androidx.datastore.preferences)
implementation(libs.androidx.fragment)
implementation(libs.androidx.lifecycle.livedata)
implementation(libs.androidx.lifecycle.runtime)
implementation(libs.androidx.lifecycle.runtime.ktx)
implementation(libs.androidx.lifecycle.viewmodel)
implementation(libs.androidx.navigation.fragment)
implementation(libs.androidx.navigation.ui)
Expand Down
3 changes: 3 additions & 0 deletions engine/benchmarks/app/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/build
/src/main/assets/bulk_data/*
!*.keep
74 changes: 74 additions & 0 deletions engine/benchmarks/app/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
plugins {
id(Plugins.BuildPlugins.application)
id(Plugins.BuildPlugins.kotlinAndroid)
id(Plugins.BuildPlugins.kotlinCompose)
id(Plugins.BuildPlugins.kotlinSerialization)
}

android {
namespace = "com.google.android.fhir.engine.benchmarks.app"
compileSdk = Sdk.COMPILE_SDK

defaultConfig {
applicationId = "com.google.android.fhir.engine.benchmarks.app"
minSdk = Sdk.MIN_SDK
targetSdk = Sdk.TARGET_SDK
versionCode = 1
versionName = "1.0"

testInstrumentationRunner = Dependencies.androidJunitRunner
}

buildTypes {
release {
isMinifyEnabled = false
proguardFiles(
getDefaultProguardFile("proguard-android-optimize.txt"),
"proguard-rules.pro",
)
}

create("benchmark") {
initWith(buildTypes.getByName("release"))
signingConfig = signingConfigs.getByName("debug")
matchingFallbacks += listOf("release")
isDebuggable = false
}
}
compileOptions {
isCoreLibraryDesugaringEnabled = true
sourceCompatibility = JavaVersion.VERSION_11
targetCompatibility = JavaVersion.VERSION_11
}
kotlinOptions { jvmTarget = "11" }
buildFeatures { compose = true }

packaging { resources.excludes.addAll(listOf("META-INF/ASL-2.0.txt", "META-INF/LGPL-3.0.txt")) }

kotlin { jvmToolchain(11) }
}

dependencies {
coreLibraryDesugaring(Dependencies.desugarJdkLibs)
implementation(project(":engine"))

implementation(libs.androidx.core)
implementation(libs.androidx.lifecycle.viewmodel.savedstate)
implementation(libs.androidx.activity.compose)
implementation(platform(libs.androidx.compose.bom))
implementation(libs.androidx.compose.ui)
implementation(libs.androidx.compose.ui.graphics)
implementation(libs.androidx.compose.ui.tooling.preview)
implementation(libs.androidx.compose.material3)
implementation(libs.androidx.navigation.compose)
implementation(libs.bundles.androidx.tracing)
implementation(libs.kotlinx.serialization.json)

testImplementation(libs.junit)
androidTestImplementation(libs.androidx.test.ext.junit)
androidTestImplementation(libs.androidx.test.espresso.core)
androidTestImplementation(platform(libs.androidx.compose.bom))
androidTestImplementation(libs.androidx.compose.ui.test.junit4)
debugImplementation(libs.androidx.compose.ui.tooling)
debugImplementation(libs.androidx.compose.ui.test.manifest)
}
35 changes: 35 additions & 0 deletions engine/benchmarks/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?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.INTERNET" />

<application
android:name=".MainApplication"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:networkSecurityConfig="@xml/network_security_config"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.Androidfhir"
>

<profileable android:shell="true" tools:targetApi="29" />

<activity
android:name=".MainActivity"
android:exported="true"
android:theme="@style/Theme.Androidfhir"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright 2025 Google LLC
*
* 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 com.google.android.fhir.engine.benchmarks.app

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.google.android.fhir.FhirEngine
import com.google.android.fhir.engine.benchmarks.app.data.ResourcesDataProvider
import java.time.LocalDateTime
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.launch

@Suppress("unused")
class CrudApiViewModel(
private val resourcesDataProvider: ResourcesDataProvider,
private val fhirEngine: FhirEngine,
) : ViewModel() {
private val _detailMutableStateFlow = MutableStateFlow("")
val detailStateFlow: StateFlow<String> = _detailMutableStateFlow.asStateFlow()

init {
viewModelScope.launch(benchmarkingViewModelWorkDispatcher) {
_detailMutableStateFlow.value = "CRUD: ${LocalDateTime.now()}"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
/*
* Copyright 2025 Google LLC
*
* 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 com.google.android.fhir.engine.benchmarks.app

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.activity.viewModels
import androidx.compose.runtime.Composable
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.rememberNavController
import ca.uhn.fhir.context.FhirContext
import com.google.android.fhir.FhirEngine
import com.google.android.fhir.engine.benchmarks.app.data.ResourcesDataProvider
import com.google.android.fhir.engine.benchmarks.app.ui.CrudDetail
import com.google.android.fhir.engine.benchmarks.app.ui.Home
import com.google.android.fhir.engine.benchmarks.app.ui.Screen
import com.google.android.fhir.engine.benchmarks.app.ui.SearchApiDetail
import com.google.android.fhir.engine.benchmarks.app.ui.SyncApiDetail
import com.google.android.fhir.engine.benchmarks.app.ui.theme.AndroidfhirTheme
import kotlin.getValue

class MainActivity : ComponentActivity() {

@Suppress("unused") private val viewModel: MainViewModel by viewModels()

private val resourcesDataProvider by lazy {
val assetManager = this@MainActivity.assets
val fhirR4JsonParser = FhirContext.forR4Cached().newJsonParser()
ResourcesDataProvider(assetManager, fhirR4JsonParser)
}

private val fhirEngine by lazy { MainApplication.fhirEngine(this@MainActivity) }

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
enableEdgeToEdge()
setContent { AndroidfhirTheme { AndroidfhirApp(resourcesDataProvider, fhirEngine) } }
}
}

@Composable
fun AndroidfhirApp(resourcesDataProvider: ResourcesDataProvider, fhirEngine: FhirEngine) {
val navController = rememberNavController()
NavHost(navController, startDestination = Screen.HomeScreen) {
composable<Screen.HomeScreen> {
Home(
{ navController.navigate(Screen.CRUDDetailScreen) },
{ navController.navigate(Screen.SearchDetailScreen) },
{
navController.navigate(
Screen.SyncDetailScreen,
)
},
)
}

composable<Screen.CRUDDetailScreen> {
val viewModel: CrudApiViewModel =
viewModel(
it,
factory =
object : ViewModelProvider.Factory {
override fun <T : ViewModel> create(modelClass: Class<T>): T {
@Suppress("UNCHECKED_CAST")
return CrudApiViewModel(resourcesDataProvider, fhirEngine) as T
}
},
)
CrudDetail(viewModel) { navController.popBackStack() }
}

composable<Screen.SearchDetailScreen> {
val viewModel: SearchApiViewModel =
viewModel(
it,
factory =
object : ViewModelProvider.Factory {
override fun <T : ViewModel> create(modelClass: Class<T>): T {
@Suppress("UNCHECKED_CAST")
return SearchApiViewModel(resourcesDataProvider, fhirEngine) as T
}
},
)
SearchApiDetail(viewModel) { navController.popBackStack() }
}

composable<Screen.SyncDetailScreen> {
val viewModel: SyncApiViewModel =
viewModel(
it,
factory =
object : ViewModelProvider.Factory {
override fun <T : ViewModel> create(modelClass: Class<T>): T {
@Suppress("UNCHECKED_CAST")
return SyncApiViewModel(resourcesDataProvider, fhirEngine) as T
}
},
)
SyncApiDetail(viewModel) { navController.popBackStack() }
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* Copyright 2025 Google LLC
*
* 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 com.google.android.fhir.engine.benchmarks.app

import android.app.Application
import android.content.Context
import android.util.Log
import com.google.android.fhir.DatabaseErrorStrategy.RECREATE_AT_OPEN
import com.google.android.fhir.FhirEngine
import com.google.android.fhir.FhirEngineConfiguration
import com.google.android.fhir.FhirEngineProvider
import com.google.android.fhir.NetworkConfiguration
import com.google.android.fhir.ServerConfiguration
import com.google.android.fhir.sync.remote.HttpLogger

class MainApplication : Application() {

override fun onCreate() {
super.onCreate()
FhirEngineProvider.init(
FhirEngineConfiguration(
enableEncryptionIfSupported = true,
RECREATE_AT_OPEN,
ServerConfiguration(
"https://hapi.fhir.org/baseR4/",
httpLogger =
HttpLogger(
HttpLogger.Configuration(
HttpLogger.Level.BODY,
),
) {
Log.i(TAG, "App-HttpLog")
Log.i(TAG, it)
},
networkConfiguration = NetworkConfiguration(uploadWithGzip = false),
),
),
)
}

private fun constructFhirEngine(): FhirEngine {
return FhirEngineProvider.getInstance(this)
}

// Only initiate the FhirEngine when used for the first time, not when the app is created.
private val fhirEngine: FhirEngine by lazy { constructFhirEngine() }

companion object {
private val TAG = MainApplication::class.java.simpleName

fun fhirEngine(context: Context) = (context.applicationContext as MainApplication).fhirEngine
}
}
Loading
Loading