Skip to content

Commit 621383d

Browse files
authored
Fix database paths in benchmarks (#6835)
* Fix database paths in benchmarks * Bump incubating cache version
1 parent 898dbad commit 621383d

File tree

10 files changed

+67
-59
lines changed

10 files changed

+67
-59
lines changed

benchmark/microbenchmark/src/androidTest/java/com/apollographql/apollo/benchmark/ApolloStoreIncubatingTests.kt

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ import androidx.benchmark.junit4.BenchmarkRule
44
import androidx.benchmark.junit4.measureRepeated
55
import com.apollographql.apollo.api.json.jsonReader
66
import com.apollographql.apollo.api.parseJsonResponse
7-
import com.apollographql.apollo.benchmark.Utils.getDbName
7+
import com.apollographql.apollo.benchmark.Utils.getIncubatingMemoryThenSqlCacheFactory
8+
import com.apollographql.apollo.benchmark.Utils.getIncubatingSqlCacheFactory
89
import com.apollographql.apollo.benchmark.Utils.operationBasedQuery
910
import com.apollographql.apollo.benchmark.Utils.resource
1011
import com.apollographql.apollo.benchmark.test.R
@@ -13,7 +14,6 @@ import com.apollographql.cache.normalized.api.DefaultCacheKeyGenerator
1314
import com.apollographql.cache.normalized.api.DefaultCacheResolver
1415
import com.apollographql.cache.normalized.api.NormalizedCacheFactory
1516
import com.apollographql.cache.normalized.memory.MemoryCacheFactory
16-
import com.apollographql.cache.normalized.sql.SqlNormalizedCacheFactory
1717
import kotlinx.coroutines.runBlocking
1818
import org.junit.Assert
1919
import org.junit.Rule
@@ -31,16 +31,12 @@ class ApolloStoreIncubatingTests {
3131

3232
@Test
3333
fun concurrentReadWritesSql() {
34-
Utils.dbFile.delete()
35-
val cacheFactory = SqlNormalizedCacheFactory(getDbName())
36-
concurrentReadWrites(cacheFactory)
34+
concurrentReadWrites(getIncubatingSqlCacheFactory())
3735
}
3836

3937
@Test
4038
fun concurrentReadWritesMemoryThenSql() {
41-
Utils.dbFile.delete()
42-
val cacheFactory = MemoryCacheFactory().chain(SqlNormalizedCacheFactory(getDbName()))
43-
concurrentReadWrites(cacheFactory)
39+
concurrentReadWrites(getIncubatingMemoryThenSqlCacheFactory())
4440
}
4541

4642
private fun concurrentReadWrites(cacheFactory: NormalizedCacheFactory) {

benchmark/microbenchmark/src/androidTest/java/com/apollographql/apollo/benchmark/ApolloStoreTests.kt

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@ import androidx.benchmark.junit4.BenchmarkRule
44
import androidx.benchmark.junit4.measureRepeated
55
import com.apollographql.apollo.api.json.jsonReader
66
import com.apollographql.apollo.api.parseJsonResponse
7-
import com.apollographql.apollo.benchmark.Utils.getDbName
7+
import com.apollographql.apollo.benchmark.Utils.getMemoryThenSqlCacheFactory
8+
import com.apollographql.apollo.benchmark.Utils.getSqlCacheFactory
89
import com.apollographql.apollo.benchmark.Utils.operationBasedQuery
910
import com.apollographql.apollo.benchmark.Utils.resource
1011
import com.apollographql.apollo.benchmark.test.R
1112
import com.apollographql.apollo.cache.normalized.ApolloStore
1213
import com.apollographql.apollo.cache.normalized.api.MemoryCacheFactory
1314
import com.apollographql.apollo.cache.normalized.api.NormalizedCacheFactory
14-
import com.apollographql.apollo.cache.normalized.sql.SqlNormalizedCacheFactory
1515
import org.junit.Assert
1616
import org.junit.Rule
1717
import org.junit.Test
@@ -28,16 +28,12 @@ class ApolloStoreTests {
2828

2929
@Test
3030
fun concurrentReadWritesSql() {
31-
Utils.dbFile.delete()
32-
val cacheFactory = SqlNormalizedCacheFactory(getDbName())
33-
concurrentReadWrites(cacheFactory)
31+
concurrentReadWrites(getSqlCacheFactory())
3432
}
3533

3634
@Test
3735
fun concurrentReadWritesMemoryThenSql() {
38-
Utils.dbFile.delete()
39-
val cacheFactory = MemoryCacheFactory().chain(SqlNormalizedCacheFactory(getDbName()))
40-
concurrentReadWrites(cacheFactory)
36+
concurrentReadWrites(getMemoryThenSqlCacheFactory())
4137
}
4238

4339
private fun concurrentReadWrites(cacheFactory: NormalizedCacheFactory) {

benchmark/microbenchmark/src/androidTest/java/com/apollographql/apollo/benchmark/CacheIncubatingIntegrationTests.kt

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ import androidx.benchmark.junit4.measureRepeated
55
import com.apollographql.apollo.ApolloClient
66
import com.apollographql.apollo.api.json.jsonReader
77
import com.apollographql.apollo.api.parseJsonResponse
8-
import com.apollographql.apollo.benchmark.Utils.getDbName
8+
import com.apollographql.apollo.benchmark.Utils.getIncubatingMemoryThenSqlCacheFactory
9+
import com.apollographql.apollo.benchmark.Utils.getIncubatingSqlCacheFactory
910
import com.apollographql.apollo.benchmark.Utils.operationBasedQuery
1011
import com.apollographql.apollo.benchmark.Utils.resource
1112
import com.apollographql.apollo.benchmark.test.R
@@ -19,7 +20,6 @@ import com.apollographql.cache.normalized.api.NormalizedCacheFactory
1920
import com.apollographql.cache.normalized.cacheManager
2021
import com.apollographql.cache.normalized.fetchPolicy
2122
import com.apollographql.cache.normalized.memory.MemoryCacheFactory
22-
import com.apollographql.cache.normalized.sql.SqlNormalizedCacheFactory
2323
import com.apollographql.mockserver.MockRequestBase
2424
import com.apollographql.mockserver.MockResponse
2525
import com.apollographql.mockserver.MockServer
@@ -41,16 +41,12 @@ class CacheIncubatingIntegrationTests {
4141

4242
@Test
4343
fun concurrentQueriesTestNetworkTransportSql() {
44-
Utils.dbFile.delete()
45-
val cacheFactory = SqlNormalizedCacheFactory(getDbName())
46-
concurrentQueries(cacheFactory, withMockServer = false)
44+
concurrentQueries(getIncubatingSqlCacheFactory(), withMockServer = false)
4745
}
4846

4947
@Test
5048
fun concurrentQueriesTestNetworkTransportMemoryThenSql() {
51-
Utils.dbFile.delete()
52-
val cacheFactory = MemoryCacheFactory().chain(SqlNormalizedCacheFactory(getDbName()))
53-
concurrentQueries(cacheFactory, withMockServer = false)
49+
concurrentQueries(getIncubatingMemoryThenSqlCacheFactory(), withMockServer = false)
5450
}
5551

5652

benchmark/microbenchmark/src/androidTest/java/com/apollographql/apollo/benchmark/CacheIncubatingPaginationTests.kt

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ import com.apollographql.apollo.annotations.ApolloExperimental
99
import com.apollographql.apollo.api.Optional
1010
import com.apollographql.apollo.api.json.jsonReader
1111
import com.apollographql.apollo.api.parseResponse
12-
import com.apollographql.apollo.benchmark.Utils.getDbName
12+
import com.apollographql.apollo.benchmark.Utils.getIncubatingMemoryThenSqlCacheFactory
13+
import com.apollographql.apollo.benchmark.Utils.getIncubatingSqlCacheFactory
1314
import com.apollographql.apollo.benchmark.Utils.resource
1415
import com.apollographql.apollo.benchmark.test.R
1516
import com.apollographql.apollo.pagination.UsersPageQuery
@@ -24,7 +25,6 @@ import com.apollographql.cache.normalized.apolloStore
2425
import com.apollographql.cache.normalized.fetchPolicy
2526
import com.apollographql.cache.normalized.memory.MemoryCacheFactory
2627
import com.apollographql.cache.normalized.normalizedCache
27-
import com.apollographql.cache.normalized.sql.SqlNormalizedCacheFactory
2828
import kotlinx.coroutines.runBlocking
2929
import org.junit.Rule
3030
import org.junit.Test
@@ -41,16 +41,12 @@ class CacheIncubatingPaginationTests {
4141

4242
@Test
4343
fun paginationSql() {
44-
Utils.dbFile.delete()
45-
val cacheFactory = SqlNormalizedCacheFactory(getDbName())
46-
paginationWritesAndRead(cacheFactory, paginationEnabled = true)
44+
paginationWritesAndRead(getIncubatingSqlCacheFactory(), paginationEnabled = true)
4745
}
4846

4947
@Test
5048
fun paginationMemoryThenSql() {
51-
Utils.dbFile.delete()
52-
val cacheFactory = MemoryCacheFactory().chain(SqlNormalizedCacheFactory(getDbName()))
53-
paginationWritesAndRead(cacheFactory, paginationEnabled = true)
49+
paginationWritesAndRead(getIncubatingMemoryThenSqlCacheFactory(), paginationEnabled = true)
5450
}
5551

5652
@Test
@@ -60,16 +56,12 @@ class CacheIncubatingPaginationTests {
6056

6157
@Test
6258
fun noPaginationSql() {
63-
Utils.dbFile.delete()
64-
val cacheFactory = SqlNormalizedCacheFactory(getDbName())
65-
paginationWritesAndRead(cacheFactory, paginationEnabled = false)
59+
paginationWritesAndRead(getIncubatingSqlCacheFactory(), paginationEnabled = false)
6660
}
6761

6862
@Test
6963
fun noPaginationMemoryThenSql() {
70-
Utils.dbFile.delete()
71-
val cacheFactory = MemoryCacheFactory().chain(SqlNormalizedCacheFactory(getDbName()))
72-
paginationWritesAndRead(cacheFactory, paginationEnabled = false)
64+
paginationWritesAndRead(getIncubatingMemoryThenSqlCacheFactory(), paginationEnabled = false)
7365
}
7466

7567
private fun paginationWritesAndRead(cacheFactory: NormalizedCacheFactory, paginationEnabled: Boolean) {

benchmark/microbenchmark/src/androidTest/java/com/apollographql/apollo/benchmark/CacheIncubatingTests.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import androidx.benchmark.junit4.measureRepeated
55
import com.apollographql.apollo.api.Query
66
import com.apollographql.apollo.api.json.jsonReader
77
import com.apollographql.apollo.api.parseJsonResponse
8-
import com.apollographql.apollo.benchmark.Utils.dbFile
8+
import com.apollographql.apollo.benchmark.Utils.getDbFile
99
import com.apollographql.apollo.benchmark.Utils.getDbName
1010
import com.apollographql.apollo.benchmark.Utils.largeListQuery
1111
import com.apollographql.apollo.benchmark.Utils.operationBasedQuery
@@ -57,10 +57,11 @@ class CacheIncubatingTests {
5757
}
5858

5959
private fun <D : Query.Data> readFromCache(testName: String, query: Query<D>, jsonResponseResId: Int, sql: Boolean, check: (D) -> Unit) {
60+
val dbFile = getDbFile(getDbName())
6061
val cacheManager = CacheManager(
6162
if (sql) {
6263
dbFile.delete()
63-
SqlNormalizedCacheFactory(name = getDbName())
64+
SqlNormalizedCacheFactory(dbFile.absolutePath)
6465
} else {
6566
MemoryCacheFactory()
6667
},

benchmark/microbenchmark/src/androidTest/java/com/apollographql/apollo/benchmark/CacheIntegrationTests.kt

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
1+
@file:OptIn(ApolloExperimental::class)
2+
13
package com.apollographql.apollo.benchmark
24

35
import androidx.benchmark.junit4.BenchmarkRule
46
import androidx.benchmark.junit4.measureRepeated
57
import com.apollographql.apollo.ApolloClient
8+
import com.apollographql.apollo.annotations.ApolloExperimental
69
import com.apollographql.apollo.api.json.jsonReader
710
import com.apollographql.apollo.api.parseJsonResponse
8-
import com.apollographql.apollo.benchmark.Utils.getDbName
11+
import com.apollographql.apollo.benchmark.Utils.getMemoryThenSqlCacheFactory
12+
import com.apollographql.apollo.benchmark.Utils.getSqlCacheFactory
913
import com.apollographql.apollo.benchmark.Utils.operationBasedQuery
1014
import com.apollographql.apollo.benchmark.Utils.resource
1115
import com.apollographql.apollo.benchmark.test.R
@@ -14,7 +18,6 @@ import com.apollographql.apollo.cache.normalized.FetchPolicy
1418
import com.apollographql.apollo.cache.normalized.api.MemoryCacheFactory
1519
import com.apollographql.apollo.cache.normalized.api.NormalizedCacheFactory
1620
import com.apollographql.apollo.cache.normalized.fetchPolicy
17-
import com.apollographql.apollo.cache.normalized.sql.SqlNormalizedCacheFactory
1821
import com.apollographql.apollo.cache.normalized.store
1922
import com.apollographql.apollo.testing.MapTestNetworkTransport
2023
import com.apollographql.apollo.testing.registerTestResponse
@@ -39,16 +42,12 @@ class CacheIntegrationTests {
3942

4043
@Test
4144
fun concurrentQueriesTestNetworkTransportSql() {
42-
Utils.dbFile.delete()
43-
val cacheFactory = SqlNormalizedCacheFactory(getDbName())
44-
concurrentQueries(cacheFactory, withMockServer = false)
45+
concurrentQueries(getSqlCacheFactory(), withMockServer = false)
4546
}
4647

4748
@Test
4849
fun concurrentQueriesTestNetworkTransportMemoryThenSql() {
49-
Utils.dbFile.delete()
50-
val cacheFactory = MemoryCacheFactory().chain(SqlNormalizedCacheFactory(getDbName()))
51-
concurrentQueries(cacheFactory, withMockServer = false)
50+
concurrentQueries(getMemoryThenSqlCacheFactory(), withMockServer = false)
5251
}
5352

5453

benchmark/microbenchmark/src/androidTest/java/com/apollographql/apollo/benchmark/CacheTests.kt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import androidx.benchmark.junit4.measureRepeated
55
import com.apollographql.apollo.api.Query
66
import com.apollographql.apollo.api.json.jsonReader
77
import com.apollographql.apollo.api.parseJsonResponse
8-
import com.apollographql.apollo.benchmark.Utils.dbFile
8+
import com.apollographql.apollo.benchmark.Utils.getDbFile
99
import com.apollographql.apollo.benchmark.Utils.getDbName
1010
import com.apollographql.apollo.benchmark.Utils.largeListQuery
1111
import com.apollographql.apollo.benchmark.Utils.operationBasedQuery
@@ -55,10 +55,12 @@ class CacheTests {
5555
}
5656

5757
private fun <D : Query.Data> readFromCache(testName: String, query: Query<D>, jsonResponseResId: Int, sql: Boolean, check: (D) -> Unit) {
58+
val dbName = getDbName()
59+
val dbFile = getDbFile(dbName)
5860
val store = ApolloStore(
5961
if (sql) {
6062
dbFile.delete()
61-
SqlNormalizedCacheFactory(name = getDbName())
63+
SqlNormalizedCacheFactory(name = dbName)
6264
} else {
6365
MemoryCacheFactory()
6466
}

benchmark/microbenchmark/src/androidTest/java/com/apollographql/apollo/benchmark/GarbageCollectTests.kt

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ package com.apollographql.apollo.benchmark
33
import androidx.benchmark.junit4.BenchmarkRule
44
import androidx.benchmark.junit4.measureRepeated
55
import com.apollographql.apollo.ApolloClient
6-
import com.apollographql.apollo.benchmark.Utils.dbFile
7-
import com.apollographql.apollo.benchmark.Utils.getDbName
6+
import com.apollographql.apollo.benchmark.Utils.getIncubatingMemoryThenSqlCacheFactory
7+
import com.apollographql.apollo.benchmark.Utils.getIncubatingSqlCacheFactory
88
import com.apollographql.apollo.conferences.cache.Cache
99
import com.apollographql.apollo.conferences.cache.Cache.cache
1010
import com.apollographql.cache.normalized.CacheManager
@@ -13,7 +13,6 @@ import com.apollographql.cache.normalized.api.SchemaCoordinatesMaxAgeProvider
1313
import com.apollographql.cache.normalized.apolloStore
1414
import com.apollographql.cache.normalized.garbageCollect
1515
import com.apollographql.cache.normalized.memory.MemoryCacheFactory
16-
import com.apollographql.cache.normalized.sql.SqlNormalizedCacheFactory
1716
import kotlinx.coroutines.runBlocking
1817
import org.junit.Rule
1918
import org.junit.Test
@@ -44,8 +43,7 @@ class GarbageCollectTests {
4443
lateinit var cacheManager: CacheManager
4544
benchmarkRule.measureRepeated {
4645
runWithTimingDisabled {
47-
dbFile.delete()
48-
cacheManager = cacheManager(SqlNormalizedCacheFactory(getDbName()))
46+
cacheManager = cacheManager(getIncubatingSqlCacheFactory())
4947
primeCache(cacheManager)
5048
}
5149
runBlocking {
@@ -61,8 +59,7 @@ class GarbageCollectTests {
6159
lateinit var cacheManager: CacheManager
6260
benchmarkRule.measureRepeated {
6361
runWithTimingDisabled {
64-
dbFile.delete()
65-
cacheManager = cacheManager(MemoryCacheFactory().chain(SqlNormalizedCacheFactory(getDbName())))
62+
cacheManager = cacheManager(getIncubatingMemoryThenSqlCacheFactory())
6663
primeCache(cacheManager)
6764
}
6865
runBlocking {

benchmark/microbenchmark/src/androidTest/java/com/apollographql/apollo/benchmark/Utils.kt

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ import androidx.test.platform.app.InstrumentationRegistry
55
import com.apollographql.apollo.api.AnyAdapter
66
import com.apollographql.apollo.api.CustomScalarAdapters
77
import com.apollographql.apollo.api.json.buildJsonString
8+
import com.apollographql.apollo.cache.normalized.api.MemoryCacheFactory
9+
import com.apollographql.apollo.cache.normalized.api.NormalizedCacheFactory
10+
import com.apollographql.apollo.cache.normalized.sql.SqlNormalizedCacheFactory
811
import com.apollographql.apollo.calendar.operation.ItemsQuery
912
import com.apollographql.apollo.calendar.response.ItemsQuery.Data.Items.Edge.Node.Companion.itemFragment
1013
import com.apollographql.apollo.calendar.response.fragment.CalendarFragment.Provider.Node.Companion.calendarProviderFragment
@@ -19,7 +22,8 @@ object Utils {
1922
private val cache = mutableMapOf<Int, BufferedSource>()
2023

2124
fun getDbName(): String = "testDb-${System.currentTimeMillis()}"
22-
val dbFile: File = InstrumentationRegistry.getInstrumentation().context.getDatabasePath(getDbName())
25+
fun getDbFile(dbName: String): File = InstrumentationRegistry.getInstrumentation().context.getDatabasePath(dbName)
26+
2327
val responseBasedQuery = com.apollographql.apollo.calendar.response.ItemsQuery(endingAfter = "", startingBefore = "")
2428
val operationBasedQuery = ItemsQuery(endingAfter = "", startingBefore = "")
2529
val largeListQuery = PlaylistRawTracksQuery("42")
@@ -74,4 +78,29 @@ object Utils {
7478
internal fun checkLargeList(data: PlaylistRawTracksQuery.Data) {
7579
check(data.playlist!!.rawTracks.size == 10000 && data.playlist.rawTracks[9999]!!.trackId == "142429999")
7680
}
81+
82+
fun getSqlCacheFactory(): NormalizedCacheFactory {
83+
val dbName = getDbName()
84+
getDbFile(dbName).delete()
85+
return SqlNormalizedCacheFactory(dbName)
86+
}
87+
88+
fun getMemoryThenSqlCacheFactory(): NormalizedCacheFactory {
89+
val dbName = getDbName()
90+
getDbFile(dbName).delete()
91+
return MemoryCacheFactory().chain(SqlNormalizedCacheFactory(dbName))
92+
}
93+
94+
fun getIncubatingSqlCacheFactory(): com.apollographql.cache.normalized.api.NormalizedCacheFactory {
95+
val dbFile = getDbFile(getDbName())
96+
dbFile.delete()
97+
return com.apollographql.cache.normalized.sql.SqlNormalizedCacheFactory(dbFile.absolutePath)
98+
}
99+
100+
fun getIncubatingMemoryThenSqlCacheFactory(): com.apollographql.cache.normalized.api.NormalizedCacheFactory {
101+
val dbFile = getDbFile(getDbName())
102+
dbFile.delete()
103+
return com.apollographql.cache.normalized.memory.MemoryCacheFactory()
104+
.chain(com.apollographql.cache.normalized.sql.SqlNormalizedCacheFactory(dbFile.absolutePath))
105+
}
77106
}

gradle/libraries.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ androidx-sqlite = "2.6.0"
1313
# This is used by the Gradle integration tests to get the artifacts locally
1414
apollo = "5.0.0-alpha.4-SNAPSHOT"
1515
apollo-execution = "0.1.1"
16-
apollo-normalizedcache-incubating = "1.0.0-beta.1"
16+
apollo-normalizedcache-incubating = "1.0.0-beta.3"
1717
# Used by the apollo-tooling project which uses a published version of Apollo
1818
apollo-published = "4.3.3"
1919
#noinspection NewerVersionAvailable requires KGP 2.2

0 commit comments

Comments
 (0)