Skip to content
Merged
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
1 change: 1 addition & 0 deletions benchmark/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ For the command line, use:

The `largesample` Json parsing data is taken from https://zacsweers.github.io/json-serialization-benchmarking/.
The `calendar_response` data was provided by @sebj and is a real-life use case.
The `tracks` data is inspired by the Deezer API.

## Current results:

Expand Down
4 changes: 4 additions & 0 deletions benchmark/microbenchmark/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,8 @@ configure<com.apollographql.apollo.gradle.api.ApolloExtension> {
argument("packageName", packageName.get())
}
}
service("tracks") {
srcDir("src/main/graphql/tracks")
packageName.set("com.apollographql.apollo.tracks")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import com.apollographql.apollo.api.json.jsonReader
import com.apollographql.apollo.api.parseJsonResponse
import com.apollographql.apollo.benchmark.Utils.dbFile
import com.apollographql.apollo.benchmark.Utils.dbName
import com.apollographql.apollo.benchmark.Utils.largeListQuery
import com.apollographql.apollo.benchmark.Utils.operationBasedQuery
import com.apollographql.apollo.benchmark.Utils.registerCacheSize
import com.apollographql.apollo.benchmark.Utils.resource
Expand All @@ -25,25 +26,35 @@ class CacheIncubatingTests {

@Test
fun cacheOperationMemory() {
readFromCache("cacheOperationMemory", operationBasedQuery, sql = false, Utils::checkOperationBased)
readFromCache("cacheOperationMemory", operationBasedQuery, R.raw.calendar_response, sql = false, Utils::checkOperationBased)
}

@Test
fun cacheOperationSql() {
readFromCache("cacheOperationSql", operationBasedQuery, sql = true, Utils::checkOperationBased)
readFromCache("cacheOperationSql", operationBasedQuery, R.raw.calendar_response, sql = true, Utils::checkOperationBased)
}

@Test
fun cacheResponseMemory() {
readFromCache("cacheResponseMemory", responseBasedQuery, sql = false, Utils::checkResponseBased)
readFromCache("cacheResponseMemory", responseBasedQuery, R.raw.calendar_response, sql = false, Utils::checkResponseBased)
}

@Test
fun cacheResponseSql() {
readFromCache("cacheResponseSql", responseBasedQuery, sql = true, Utils::checkResponseBased)
readFromCache("cacheResponseSql", responseBasedQuery, R.raw.calendar_response, sql = true, Utils::checkResponseBased)
}

private fun <D : Query.Data> readFromCache(testName: String, query: Query<D>, sql: Boolean, check: (D) -> Unit) {
@Test
fun cacheLargeListMemory() {
readFromCache("cacheLargeListMemory", largeListQuery, R.raw.tracks_playlist_response, sql = false, Utils::checkLargeList)
}

@Test
fun cacheLargeListSql() {
readFromCache("cacheLargeListSql", largeListQuery, R.raw.tracks_playlist_response, sql = true, Utils::checkLargeList)
}

private fun <D : Query.Data> readFromCache(testName: String, query: Query<D>, jsonResponseResId: Int, sql: Boolean, check: (D) -> Unit) {
val store = ApolloStore(
if (sql) {
dbFile.delete()
Expand All @@ -53,7 +64,7 @@ class CacheIncubatingTests {
}
)

val data = query.parseJsonResponse(resource(R.raw.calendar_response).jsonReader()).data!!
val data = query.parseJsonResponse(resource(jsonResponseResId).jsonReader()).data!!
runBlocking {
store.writeOperation(query, data)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import com.apollographql.apollo.api.json.jsonReader
import com.apollographql.apollo.api.parseJsonResponse
import com.apollographql.apollo.benchmark.Utils.dbFile
import com.apollographql.apollo.benchmark.Utils.dbName
import com.apollographql.apollo.benchmark.Utils.largeListQuery
import com.apollographql.apollo.benchmark.Utils.operationBasedQuery
import com.apollographql.apollo.benchmark.Utils.registerCacheSize
import com.apollographql.apollo.benchmark.Utils.resource
Expand All @@ -25,25 +26,35 @@ class CacheTests {

@Test
fun cacheOperationMemory() {
readFromCache("cacheOperationMemory", operationBasedQuery, sql = false, Utils::checkOperationBased)
readFromCache("cacheOperationMemory", operationBasedQuery, R.raw.calendar_response, sql = false, Utils::checkOperationBased)
}

@Test
fun cacheOperationSql() {
readFromCache("cacheOperationSql", operationBasedQuery, sql = true, Utils::checkOperationBased)
readFromCache("cacheOperationSql", operationBasedQuery, R.raw.calendar_response, sql = true, Utils::checkOperationBased)
}

@Test
fun cacheResponseMemory() {
readFromCache("cacheResponseMemory", responseBasedQuery, sql = false, Utils::checkResponseBased)
readFromCache("cacheResponseMemory", responseBasedQuery, R.raw.calendar_response, sql = false, Utils::checkResponseBased)
}

@Test
fun cacheResponseSql() {
readFromCache("cacheResponseSql", responseBasedQuery, sql = true, Utils::checkResponseBased)
readFromCache("cacheResponseSql", responseBasedQuery, R.raw.calendar_response, sql = true, Utils::checkResponseBased)
}

private fun <D : Query.Data> readFromCache(testName: String, query: Query<D>, sql: Boolean, check: (D) -> Unit) {
@Test
fun cacheLargeListMemory() {
readFromCache("cacheLargeListMemory", largeListQuery, R.raw.tracks_playlist_response, sql = false, Utils::checkLargeList)
}

@Test
fun cacheLargeListSql() {
readFromCache("cacheLargeListSql", largeListQuery, R.raw.tracks_playlist_response, sql = true, Utils::checkLargeList)
}

private fun <D : Query.Data> readFromCache(testName: String, query: Query<D>, jsonResponseResId: Int, sql: Boolean, check: (D) -> Unit) {
val store = ApolloStore(
if (sql) {
dbFile.delete()
Expand All @@ -53,7 +64,7 @@ class CacheTests {
}
)

val data = query.parseJsonResponse(resource(R.raw.calendar_response).jsonReader()).data!!
val data = query.parseJsonResponse(resource(jsonResponseResId).jsonReader()).data!!
runBlocking {
store.writeOperation(query, data)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import com.apollographql.apollo.calendar.operation.ItemsQuery
import com.apollographql.apollo.calendar.response.ItemsQuery.Data.Items.Edge.Node.Companion.itemFragment
import com.apollographql.apollo.calendar.response.fragment.CalendarFragment.Provider.Node.Companion.calendarProviderFragment
import com.apollographql.apollo.calendar.response.fragment.ItemFragment.Calendar.Node.Companion.calendarFragment
import com.apollographql.apollo.tracks.PlaylistRawTracksQuery
import okio.Buffer
import okio.BufferedSource
import okio.source
Expand All @@ -21,6 +22,7 @@ object Utils {
val dbFile: File = InstrumentationRegistry.getInstrumentation().context.getDatabasePath(dbName)
val responseBasedQuery = com.apollographql.apollo.calendar.response.ItemsQuery(endingAfter = "", startingBefore = "")
val operationBasedQuery = ItemsQuery(endingAfter = "", startingBefore = "")
val largeListQuery = PlaylistRawTracksQuery("42")

/**
* Reads a resource into a fully buffered [BufferedSource]. This function returns a peeked [BufferedSource]
Expand Down Expand Up @@ -52,7 +54,7 @@ object Utils {
)
)
)
Outputs.writeFile("extraMetrics.json", "extraMetrics", true) {
Outputs.writeFile("extraMetrics.json", true) {
it.writeText(
buildJsonString {
AnyAdapter.toJson(this, CustomScalarAdapters.Empty, extraMetrics)
Expand All @@ -69,4 +71,7 @@ object Utils {
check(data.items!!.edges[248].node.itemFragment()!!.calendar!!.node.calendarFragment()!!.provider.node.calendarProviderFragment()!!.id == "cc8e4c28-f178-11ec-8ea0-0242ac120002")
}

internal fun checkLargeList(data: PlaylistRawTracksQuery.Data) {
check(data.playlist!!.rawTracks.size == 10000 && data.playlist.rawTracks[9999]!!.trackId == "142429999")
}
}
Loading
Loading