|
| 1 | +/* |
| 2 | + * Copyright 2025 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.google.android.fhir.benchmark |
| 18 | + |
| 19 | +import android.content.Context |
| 20 | +import androidx.benchmark.junit4.BenchmarkRule |
| 21 | +import androidx.benchmark.junit4.measureRepeated |
| 22 | +import androidx.test.core.app.ApplicationProvider |
| 23 | +import androidx.test.ext.junit.runners.AndroidJUnit4 |
| 24 | +import androidx.test.filters.LargeTest |
| 25 | +import ca.uhn.fhir.context.FhirContext |
| 26 | +import ca.uhn.fhir.context.FhirVersionEnum |
| 27 | +import com.google.android.fhir.FhirEngineConfiguration |
| 28 | +import com.google.android.fhir.FhirEngineProvider |
| 29 | +import com.google.android.fhir.search.count |
| 30 | +import com.google.common.truth.Truth.assertThat |
| 31 | +import kotlinx.coroutines.runBlocking |
| 32 | +import org.hl7.fhir.r4.model.Patient |
| 33 | +import org.hl7.fhir.r4.model.Resource |
| 34 | +import org.junit.AfterClass |
| 35 | +import org.junit.BeforeClass |
| 36 | +import org.junit.Rule |
| 37 | +import org.junit.Test |
| 38 | +import org.junit.runner.RunWith |
| 39 | + |
| 40 | +@LargeTest |
| 41 | +@RunWith(AndroidJUnit4::class) |
| 42 | +class DemoFhirEngineBenchmark { |
| 43 | + |
| 44 | + @get:Rule val benchmarkRule = BenchmarkRule() |
| 45 | + private val applicationContext = ApplicationProvider.getApplicationContext<Context>() |
| 46 | + private val assetManager = applicationContext.assets |
| 47 | + private val fhirContext = FhirContext.forCached(FhirVersionEnum.R4) |
| 48 | + |
| 49 | + @Test |
| 50 | + fun create() { |
| 51 | + val bulkFiles = |
| 52 | + assetManager.list(BULK_DATA_DIR)?.filter { it.endsWith(".ndjson") } ?: emptyList() |
| 53 | + val resources = |
| 54 | + bulkFiles |
| 55 | + .asSequence() |
| 56 | + .map { assetManager.open("$BULK_DATA_DIR/$it") } |
| 57 | + .flatMap { inputStream -> inputStream.bufferedReader().readLines() } |
| 58 | + .map { fhirContext.newJsonParser().parseResource(it) as Resource } |
| 59 | + .toList() |
| 60 | + |
| 61 | + val fhirEngine = FhirEngineProvider.getInstance(applicationContext) |
| 62 | + |
| 63 | + benchmarkRule.measureRepeated { runBlocking { fhirEngine.create(*resources.toTypedArray()) } } |
| 64 | + assertThat(runBlocking { fhirEngine.count<Patient> {} }).isGreaterThan(1L) |
| 65 | + } |
| 66 | + |
| 67 | + companion object { |
| 68 | + private const val BULK_DATA_DIR = "bulk_data" |
| 69 | + |
| 70 | + @JvmStatic |
| 71 | + @BeforeClass |
| 72 | + fun oneTimeSetup() { |
| 73 | + FhirEngineProvider.init(FhirEngineConfiguration(testMode = true)) |
| 74 | + } |
| 75 | + |
| 76 | + @JvmStatic |
| 77 | + @AfterClass |
| 78 | + fun oneTimeTearDown() { |
| 79 | + FhirEngineProvider.cleanup() |
| 80 | + } |
| 81 | + } |
| 82 | +} |
0 commit comments