For #12802: add StorageStats glean metrics.
parent
70c66185d8
commit
5d8c900391
@ -0,0 +1,66 @@
|
||||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
package org.mozilla.fenix.perf
|
||||
|
||||
import android.app.usage.StorageStats
|
||||
import android.app.usage.StorageStatsManager
|
||||
import android.content.Context
|
||||
import android.os.Build
|
||||
import androidx.annotation.RequiresApi
|
||||
import androidx.annotation.VisibleForTesting
|
||||
import androidx.annotation.VisibleForTesting.PRIVATE
|
||||
import androidx.annotation.WorkerThread
|
||||
import androidx.core.content.getSystemService
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.GlobalScope
|
||||
import kotlinx.coroutines.launch
|
||||
import org.mozilla.fenix.GleanMetrics.StorageStats as Metrics
|
||||
|
||||
/**
|
||||
* A collection of functions related to measuring the [StorageStats] of the application such as data
|
||||
* dir size.
|
||||
*
|
||||
* Unfortunately, this API is only available on API 26+ so the data will only be reported for those
|
||||
* platforms.
|
||||
*/
|
||||
@RequiresApi(Build.VERSION_CODES.O) // StorageStatsManager
|
||||
object StorageStatsMetrics {
|
||||
|
||||
fun report(context: Context) {
|
||||
GlobalScope.launch(Dispatchers.IO) {
|
||||
reportSync(context)
|
||||
}
|
||||
}
|
||||
|
||||
// I couldn't get runBlockingTest to work correctly so I moved the functionality under test to
|
||||
// a synchronous function.
|
||||
@VisibleForTesting(otherwise = PRIVATE)
|
||||
@WorkerThread // queryStatsForUid
|
||||
fun reportSync(context: Context) {
|
||||
// I don't expect this to ever be null so we don't report if so.
|
||||
context.getSystemService<StorageStatsManager>()?.let { storageStatsManager ->
|
||||
val appInfo = context.applicationInfo
|
||||
val storageStats = Metrics.queryStatsDuration.measure {
|
||||
// The docs say queryStatsForPackage may be slower if the app uses
|
||||
// android:sharedUserId so we the suggested alternative.
|
||||
//
|
||||
// The docs say this may be slow:
|
||||
// > This method may take several seconds to complete, so it should only be called
|
||||
// > from a worker thread.
|
||||
//
|
||||
// So we call from a worker thread and measure the duration to make sure it's not
|
||||
// too slow.
|
||||
storageStatsManager.queryStatsForUid(appInfo.storageUuid, appInfo.uid)
|
||||
}
|
||||
|
||||
// dataBytes includes the cache so we subtract it.
|
||||
val justDataDirBytes = storageStats.dataBytes - storageStats.cacheBytes
|
||||
|
||||
Metrics.dataDirBytes.accumulate(justDataDirBytes)
|
||||
Metrics.appBytes.accumulate(storageStats.appBytes)
|
||||
Metrics.cacheBytes.accumulate(storageStats.cacheBytes)
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,61 @@
|
||||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
package org.mozilla.fenix.perf
|
||||
|
||||
import android.app.usage.StorageStats
|
||||
import android.app.usage.StorageStatsManager
|
||||
import android.content.Context
|
||||
import androidx.core.content.getSystemService
|
||||
import io.mockk.MockKAnnotations
|
||||
import io.mockk.every
|
||||
import io.mockk.impl.annotations.RelaxedMockK
|
||||
import mozilla.components.service.glean.testing.GleanTestRule
|
||||
import mozilla.components.support.test.robolectric.testContext
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Assert.assertTrue
|
||||
import org.junit.Before
|
||||
import org.junit.Rule
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
import org.mozilla.fenix.helpers.FenixRobolectricTestRunner
|
||||
import org.mozilla.fenix.GleanMetrics.StorageStats as Metrics
|
||||
|
||||
@RunWith(FenixRobolectricTestRunner::class) // gleanTestRule
|
||||
class StorageStatsMetricsTest {
|
||||
|
||||
@get:Rule
|
||||
val gleanTestRule = GleanTestRule(testContext)
|
||||
|
||||
@RelaxedMockK private lateinit var mockContext: Context
|
||||
@RelaxedMockK private lateinit var storageStats: StorageStats
|
||||
|
||||
@Before
|
||||
fun setUp() {
|
||||
MockKAnnotations.init(this)
|
||||
|
||||
every {
|
||||
mockContext.getSystemService<StorageStatsManager>()?.queryStatsForUid(any(), any())
|
||||
} returns storageStats
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `WHEN reporting THEN the values from the storageStats are accumulated`() {
|
||||
every { storageStats.appBytes } returns 100
|
||||
every { storageStats.cacheBytes } returns 200
|
||||
every { storageStats.dataBytes } returns 1000
|
||||
|
||||
StorageStatsMetrics.reportSync(mockContext)
|
||||
|
||||
assertEquals(100, Metrics.appBytes.testGetValue().sum)
|
||||
assertEquals(200, Metrics.cacheBytes.testGetValue().sum)
|
||||
assertEquals(800, Metrics.dataDirBytes.testGetValue().sum)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `WHEN reporting THEN the query duration is measured`() {
|
||||
StorageStatsMetrics.reportSync(mockContext)
|
||||
assertTrue(Metrics.queryStatsDuration.testHasValue())
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue