Skip to content

Commit

Permalink
code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
ekigamba committed Jul 17, 2023
1 parent fef6ac9 commit 5ae5279
Show file tree
Hide file tree
Showing 8 changed files with 128 additions and 98 deletions.
32 changes: 4 additions & 28 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -184,33 +184,9 @@ jobs:
- name: Copy CI gradle.properties
run: mkdir -p ~/.gradle ; cp .github/ci-gradle.properties ~/.gradle/gradle.properties && cat ~/.gradle/gradle.properties

- name: Setup Gradle cache
uses: gradle/gradle-build-action@v2

- name: Spotless check quest application
run: ./gradlew -PlocalPropertiesFile=local.properties :quest:spotlessCheck --stacktrace :quest:ktlintCheck --stacktrace
working-directory: android

- name: Load AVD cache
uses: actions/cache@v2
id: avd-cache
with:
path: |
~/.android/avd/*
~/.android/adb*
key: avd-${{ matrix.api-level }}

- name: Create AVD and generate snapshot for caching
if: steps.avd-cache.outputs.cache-hit != 'true'
uses: reactivecircus/android-emulator-runner@v2
with:
working-directory: android
api-level: ${{ matrix.api-level }}
arch: x86_64
force-avd-creation: false
emulator-options: -no-window -gpu swiftshader_indirect -noaudio -no-boot-anim -camera-back none
disable-animations: false
script: echo "Generated AVD snapshot for caching."
- name: Download sample database for performance tests
working-directory: android
run: wget -q https://github.com/ekigamba/debug-jacoco-reports/releases/download/v0.0.1/resources.db -P quest/src/androidTest/assets/ && ls -lah && ls -lah ../ && ls -lah quest/src/androidTest/assets/

- name: Run Quest module unit and instrumentation tests and generate coverage report
uses: reactivecircus/android-emulator-runner@v2
Expand All @@ -221,7 +197,7 @@ jobs:
force-avd-creation: true
emulator-options: -no-snapshot-save -no-window -gpu swiftshader_indirect -noaudio -no-boot-anim -camera-back none
disable-animations: true
script: wget https://github.com/ekigamba/debug-jacoco-reports/releases/download/v0.0.1/resources.db && mv resources.db quest/src/androidTest/assets/ && ./gradlew -PlocalPropertiesFile=local.properties :quest:clean && ./gradlew -PlocalPropertiesFile=local.properties :quest:fhircoreJacocoReport --stacktrace
script: ./gradlew -PlocalPropertiesFile=local.properties :quest:clean && ./gradlew -PlocalPropertiesFile=local.properties :quest:fhircoreJacocoReport --stacktrace

- name: Upload Quest module test coverage report to Codecov
if: matrix.api-level == 30 # Only upload coverage on API level 30
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,9 @@ constructor(
withContext(dispatcherProvider.io()) { fhirEngine.search<Resource>(search) }
}
.onFailure {
Timber.e(it, "Error retrieving resources. Empty list returned by default")
System.out.println("Error occurred")
it.printStackTrace()
Timber.e(it, "Error retrieving resources. Empty list returned by default")
System.out.println("Error occurred")
it.printStackTrace()
}
.getOrDefault(emptyList())

Expand Down
42 changes: 21 additions & 21 deletions android/quest/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import com.android.build.api.variant.FilterConfiguration.FilterType
import java.io.FileReader
import org.gradle.api.tasks.testing.logging.TestLogEvent
import org.json.JSONObject
import kotlin.collections.hashMapOf

buildscript {
apply(from = "../jacoco.gradle.kts")
Expand Down Expand Up @@ -76,7 +73,8 @@ android {

testInstrumentationRunner = "org.smartregister.fhircore.quest.QuestTestRunner"
testInstrumentationRunnerArguments["additionalTestOutputDir"] = "/sdcard/Download"
testInstrumentationRunnerArguments["androidx.benchmark.suppressErrors"] = "ACTIVITY-MISSING,CODE-COVERAGE,DEBUGGABLE,UNLOCKED,EMULATOR"
testInstrumentationRunnerArguments["androidx.benchmark.suppressErrors"] =
"ACTIVITY-MISSING,CODE-COVERAGE,DEBUGGABLE,UNLOCKED,EMULATOR"
}

signingConfigs {
Expand Down Expand Up @@ -377,24 +375,26 @@ dependencies {
ktlint(project(":linting"))
}

task("checkPerformanceLimits") {
val resultsFile = File("performance-results.json")
val expectationsFile = File("expected-results.json")
/*
// Read the expectations file
val expectedResultsMap : HashMap<String, HashMap<String, Float>> = hashMapOf()
task("checkPerformanceLimits") {
val resultsFile = File("performance-results.json")
val expectationsFile = File("expected-results.json")
JSONObject(FileReader(expectationsFile).readText()).run {
keys().forEach { key ->
val resultMaxDeltaMap :HashMap<String, Float> = hashMapOf()
val methodExpectedResults = this.getJSONObject("key")
// Read the expectations file
val expectedResultsMap : HashMap<String, HashMap<String, Float>> = hashMapOf()
methodExpectedResults.keys().forEach { expectedResultsKey ->
resultMaxDeltaMap.put(expectedResultsKey, methodExpectedResults.getFloat(expectedResultsKey))
}
}
}
JSONObject(FileReader(expectationsFile).readText()).run {
keys().forEach { key ->
val resultMaxDeltaMap :HashMap<String, Float> = hashMapOf()
val methodExpectedResults = this.getJSONObject("key")
methodExpectedResults.keys().forEach { expectedResultsKey ->
resultMaxDeltaMap.put(expectedResultsKey, methodExpectedResults.getFloat(expectedResultsKey))
}
}
}
// Loop through the results file updating the results
JSONObject
}
// Loop through the results file updating the results
JSONObject
}*/
2 changes: 1 addition & 1 deletion android/quest/src/androidTest/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,4 @@

</application>
<!-- [END benchmark_manifest] -->
</manifest>
</manifest>
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/*
* Copyright 2021-2023 Ona Systems, 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 org.smartregister.fhircore.performance

import androidx.benchmark.junit4.BenchmarkRule
Expand All @@ -23,6 +39,7 @@ class ChildRegisterPerformanceTests {
@get:Rule val benchmarkRule = BenchmarkRule()

@Inject lateinit var defaultRepository: DefaultRepository

@Inject lateinit var registerRepository: RegisterRepository

@get:Rule val hiltRule = HiltAndroidRule(this)
Expand All @@ -41,7 +58,7 @@ class ChildRegisterPerformanceTests {
runBlocking {
defaultRepository.configurationRegistry.loadConfigurations(
"app/debug",
InstrumentationRegistry.getInstrumentation().targetContext
InstrumentationRegistry.getInstrumentation().targetContext,
) { loadConfigSuccessful ->
}
}
Expand All @@ -53,14 +70,14 @@ class ChildRegisterPerformanceTests {
val resourcesDbInputStream = testContext.assets.open("resources.db")

// Delete the database files
"/data/data/${appContext.packageName}/databases/resources.db".deleteFileIsExists()
"/data/data/${appContext.packageName}/databases/resources.db-shm".deleteFileIsExists()
"/data/data/${appContext.packageName}/databases/resources.db-wal".deleteFileIsExists()
"/data/data/${appContext.packageName}/databases/resources.db".deleteFileIsExists()
"/data/data/${appContext.packageName}/databases/resources.db-shm".deleteFileIsExists()
"/data/data/${appContext.packageName}/databases/resources.db-wal".deleteFileIsExists()

// Copy over the db
Files.copy(
resourcesDbInputStream,
Path("/data/data/${appContext.packageName}/databases/resources.db")
Path("/data/data/${appContext.packageName}/databases/resources.db"),
)
}

Expand Down Expand Up @@ -108,9 +125,9 @@ class ChildRegisterPerformanceTests {
}
}

fun String.deleteFileIsExists() {
try {
if (File(this).exists()) Files.delete(Path(this))
} catch (ex: NoSuchFileException) {}
}
fun String.deleteFileIsExists() {
try {
if (File(this).exists()) Files.delete(Path(this))
} catch (ex: NoSuchFileException) {}
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/*
* Copyright 2021-2023 Ona Systems, 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 org.smartregister.fhircore.performance

import androidx.benchmark.junit4.BenchmarkRule
Expand All @@ -17,13 +33,13 @@ import org.smartregister.fhircore.engine.data.local.DefaultRepository
import org.smartregister.fhircore.engine.data.local.register.RegisterRepository
import timber.log.Timber


@HiltAndroidTest
class HouseholdRegisterPerformanceTests {

@get:Rule val benchmarkRule = BenchmarkRule()

@Inject lateinit var defaultRepository: DefaultRepository

@Inject lateinit var registerRepository: RegisterRepository

@get:Rule val hiltRule = HiltAndroidRule(this)
Expand All @@ -42,7 +58,7 @@ class HouseholdRegisterPerformanceTests {
runBlocking {
defaultRepository.configurationRegistry.loadConfigurations(
"app/debug",
InstrumentationRegistry.getInstrumentation().targetContext
InstrumentationRegistry.getInstrumentation().targetContext,
) { loadConfigSuccessful ->
}
}
Expand All @@ -54,19 +70,17 @@ class HouseholdRegisterPerformanceTests {
val resourcesDbInputStream = testContext.assets.open("resources.db")

// Delete the database files
"/data/data/${appContext.packageName}/databases/resources.db".deleteFileIsExists()
"/data/data/${appContext.packageName}/databases/resources.db-shm".deleteFileIsExists()
"/data/data/${appContext.packageName}/databases/resources.db-wal".deleteFileIsExists()
"/data/data/${appContext.packageName}/databases/resources.db".deleteFileIsExists()
"/data/data/${appContext.packageName}/databases/resources.db-shm".deleteFileIsExists()
"/data/data/${appContext.packageName}/databases/resources.db-wal".deleteFileIsExists()

// Copy over the db
Files.copy(
resourcesDbInputStream,
Path("/data/data/${appContext.packageName}/databases/resources.db")
Path("/data/data/${appContext.packageName}/databases/resources.db"),
)
}



@Test
fun benchmarkPage0() {
benchmarkingFunctionality(0)
Expand All @@ -77,13 +91,11 @@ class HouseholdRegisterPerformanceTests {
benchmarkingFunctionality(0)
}


@Test
fun benchmarkPage2() {
benchmarkingFunctionality(1)
}


@Test
fun benchmarkPage3() {
benchmarkingFunctionality(2)
Expand All @@ -106,17 +118,16 @@ class HouseholdRegisterPerformanceTests {

fun benchmarkingFunctionality(page: Int) {
benchmarkRule.measureRepeated {

runBlocking {
val repoData = registerRepository.loadRegisterData(page, "householdRegister")
System.out.println("Records fetched ${repoData.size}")
}
}
}

fun String.deleteFileIsExists() {
try {
if (File(this).exists()) Files.delete(Path(this))
} catch (ex: NoSuchFileException) {}
}
fun String.deleteFileIsExists() {
try {
if (File(this).exists()) Files.delete(Path(this))
} catch (ex: NoSuchFileException) {}
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/*
* Copyright 2021-2023 Ona Systems, 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 org.smartregister.fhircore.performance

import androidx.benchmark.junit4.BenchmarkRule
Expand All @@ -17,13 +33,13 @@ import org.smartregister.fhircore.engine.data.local.DefaultRepository
import org.smartregister.fhircore.engine.data.local.register.RegisterRepository
import timber.log.Timber


@HiltAndroidTest
class HouseholdRegisterPerformanceTests2 {

@get:Rule val benchmarkRule = BenchmarkRule()

@Inject lateinit var defaultRepository: DefaultRepository

@Inject lateinit var registerRepository: RegisterRepository

@get:Rule val hiltRule = HiltAndroidRule(this)
Expand All @@ -42,7 +58,7 @@ class HouseholdRegisterPerformanceTests2 {
runBlocking {
defaultRepository.configurationRegistry.loadConfigurations(
"app/debug",
InstrumentationRegistry.getInstrumentation().targetContext
InstrumentationRegistry.getInstrumentation().targetContext,
) { loadConfigSuccessful ->
}
}
Expand All @@ -54,37 +70,34 @@ class HouseholdRegisterPerformanceTests2 {
val resourcesDbInputStream = testContext.assets.open("resources.db")

// Delete the database files
"/data/data/${appContext.packageName}/databases/resources.db".deleteFileIsExists()
"/data/data/${appContext.packageName}/databases/resources.db-shm".deleteFileIsExists()
"/data/data/${appContext.packageName}/databases/resources.db-wal".deleteFileIsExists()
"/data/data/${appContext.packageName}/databases/resources.db".deleteFileIsExists()
"/data/data/${appContext.packageName}/databases/resources.db-shm".deleteFileIsExists()
"/data/data/${appContext.packageName}/databases/resources.db-wal".deleteFileIsExists()

// Copy over the db
Files.copy(
resourcesDbInputStream,
Path("/data/data/${appContext.packageName}/databases/resources.db")
Path("/data/data/${appContext.packageName}/databases/resources.db"),
)
}



@Test
fun benchmarkPage0() {
benchmarkingFunctionality(0)
}

fun benchmarkingFunctionality(page: Int) {
benchmarkRule.measureRepeated {

runBlocking {
val repoData = registerRepository.loadRegisterData(page, "householdRegister")
System.out.println("Records fetched ${repoData.size}")
}
}
}

fun String.deleteFileIsExists() {
try {
if (File(this).exists()) Files.delete(Path(this))
} catch (ex: NoSuchFileException) {}
}
fun String.deleteFileIsExists() {
try {
if (File(this).exists()) Files.delete(Path(this))
} catch (ex: NoSuchFileException) {}
}
}
Loading

0 comments on commit 5ae5279

Please sign in to comment.