Skip to content

Instantly share code, notes, and snippets.

@kota1921
Created August 23, 2021 07:24
Show Gist options
  • Save kota1921/fd79a85d08dda6f998414d48a8b4acbc to your computer and use it in GitHub Desktop.
Save kota1921/fd79a85d08dda6f998414d48a8b4acbc to your computer and use it in GitHub Desktop.
@RunWith(AndroidJUnit4::class)
class KotlinSerializationBenchmark {
private val context = ApplicationProvider.getApplicationContext<Context>()
private val simpleJsonString = Utils.readJsonAsStringFromDisk(context, R.raw.simple)
@get:Rule val benchmarkRule = BenchmarkRule()
@Before
fun init() {
val field = androidx.benchmark.BenchmarkState::class.java.getDeclaredField("REPEAT_COUNT")
field.isAccessible = true
field.set(benchmarkRule, Utils.GLOBAL_REPEAT_COUNT)
}
@Test
fun testRead() {
benchmarkRule.measureRepeated {
Json.decodeFromString<List<SmallObject>>(simpleJsonString ?: "")
}
}
@Test
fun testWrite() {
val testObjects = Json.decodeFromString<List<SmallObject>>(simpleJsonString ?: "")
benchmarkRule.measureRepeated {
Json.encodeToString(testObjects)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment