-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6372 from seadowg/optimize-download
Optimize form download with entities
- Loading branch information
Showing
10 changed files
with
199 additions
and
94 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
collect_app/src/androidTest/java/org/odk/collect/android/benchmark/SearchBenchmarkTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package org.odk.collect.android.benchmark | ||
|
||
import androidx.test.ext.junit.runners.AndroidJUnit4 | ||
import org.hamcrest.MatcherAssert.assertThat | ||
import org.hamcrest.Matchers.blankOrNullString | ||
import org.hamcrest.Matchers.not | ||
import org.junit.Rule | ||
import org.junit.Test | ||
import org.junit.rules.RuleChain | ||
import org.junit.runner.RunWith | ||
import org.odk.collect.android.benchmark.support.Benchmarker | ||
import org.odk.collect.android.benchmark.support.benchmark | ||
import org.odk.collect.android.support.TestDependencies | ||
import org.odk.collect.android.support.pages.MainMenuPage | ||
import org.odk.collect.android.support.rules.CollectTestRule | ||
import org.odk.collect.android.support.rules.TestRuleChain.chain | ||
import org.odk.collect.android.test.BuildConfig.ENTITIES_FILTER_SEARCH_TEST_PROJECT_URL | ||
|
||
/** | ||
* Benchmarks the performance of search() forms. [ENTITIES_FILTER_SEARCH_TEST_PROJECT_URL] should be | ||
* set to a project that contains the "100k Entities Filter search()" form. | ||
* | ||
* Devices that currently pass: | ||
* - Fairphone 3 | ||
* | ||
*/ | ||
|
||
@RunWith(AndroidJUnit4::class) | ||
class SearchBenchmarkTest { | ||
|
||
private val rule = CollectTestRule(useDemoProject = false) | ||
|
||
@get:Rule | ||
var chain: RuleChain = chain(TestDependencies(true)).around(rule) | ||
|
||
@Test | ||
fun run() { | ||
assertThat( | ||
"Need to set ENTITIES_FILTER_SEARCH_TEST_PROJECT_URL before running!", | ||
ENTITIES_FILTER_SEARCH_TEST_PROJECT_URL, | ||
not(blankOrNullString()) | ||
) | ||
|
||
val benchmarker = Benchmarker() | ||
|
||
rule.startAtFirstLaunch() | ||
.clickManuallyEnterProjectDetails() | ||
.inputUrl(ENTITIES_FILTER_SEARCH_TEST_PROJECT_URL) | ||
.addProject() | ||
|
||
.clickGetBlankForm() | ||
.clickGetSelected() | ||
.clickOK(MainMenuPage()) | ||
|
||
.clickFillBlankForm() | ||
.benchmark("Loading form first time", 20, benchmarker) { | ||
it.clickOnForm("100k Entities Filter search()") | ||
} | ||
|
||
.pressBackAndDiscardForm() | ||
.clickFillBlankForm() | ||
.benchmark("Loading form second time", 2, benchmarker) { | ||
it.clickOnForm("100k Entities Filter search()") | ||
} | ||
|
||
.answerQuestion("Which value do you want to filter by?", "1024") | ||
.benchmark("Filtering select", 2, benchmarker) { | ||
it.swipeToNextQuestion("Filtered select") | ||
} | ||
|
||
benchmarker.assertResults() | ||
} | ||
} |
63 changes: 63 additions & 0 deletions
63
collect_app/src/androidTest/java/org/odk/collect/android/benchmark/support/Benchmarker.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package org.odk.collect.android.benchmark.support | ||
|
||
import org.hamcrest.MatcherAssert.assertThat | ||
import org.hamcrest.Matchers.lessThan | ||
import org.odk.collect.android.support.pages.Page | ||
import org.odk.collect.shared.TimeInMs | ||
|
||
class Benchmarker { | ||
private val stopwatch = Stopwatch() | ||
private val targets = mutableMapOf<String, Long>() | ||
|
||
fun <T> benchmark(name: String, target: Long, action: () -> T): T { | ||
targets[name] = target | ||
return stopwatch.time(name) { | ||
action() | ||
} | ||
} | ||
|
||
fun assertResults() { | ||
printResults() | ||
|
||
targets.entries.forEach { | ||
val time = stopwatch.getTime(it.key) | ||
assertThat("\"${it.key}\" took ${time}s!", time, lessThan(it.value)) | ||
} | ||
} | ||
|
||
private fun printResults() { | ||
println("Benchmark results:") | ||
targets.keys.forEach { | ||
println("$it: ${stopwatch.getTime(it)}s") | ||
} | ||
} | ||
} | ||
|
||
fun <T : Page<T>, Y : Page<Y>> Y.benchmark( | ||
name: String, | ||
target: Long, | ||
benchmarker: Benchmarker, | ||
action: (Y) -> T | ||
): T { | ||
return benchmarker.benchmark(name, target) { | ||
action(this) | ||
} | ||
} | ||
|
||
private class Stopwatch { | ||
|
||
private val times = mutableMapOf<String, Long>() | ||
|
||
fun <T> time(name: String, action: () -> T): T { | ||
val startTime = System.currentTimeMillis() | ||
val result = action() | ||
val endTime = System.currentTimeMillis() | ||
|
||
times[name] = (endTime - startTime) / TimeInMs.ONE_SECOND | ||
return result | ||
} | ||
|
||
fun getTime(name: String): Long { | ||
return times[name]!! | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.