Merge changes If7cb1cbb,I1d09ac01 into main

* changes:
  Adds more data points for benchmarking tests
  Move test zip file to assets
This commit is contained in:
Junyu Lai
2023-10-16 06:50:12 +00:00
committed by Gerrit Code Review
49 changed files with 38 additions and 28 deletions

View File

@@ -29,6 +29,7 @@ android_test {
"src/**/*.kt",
"src/**/*.aidl",
],
asset_dirs: ["assets"],
static_libs: [
"androidx.test.rules",
"mockito-target-minus-junit4",

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@@ -20,10 +20,9 @@ import android.net.NetworkStats.NonMonotonicObserver
import android.net.NetworkStatsCollection
import android.net.netstats.NetworkStatsDataMigrationUtils.PREFIX_UID
import android.os.DropBoxManager
import androidx.test.InstrumentationRegistry
import androidx.test.platform.app.InstrumentationRegistry
import com.android.internal.util.FileRotator
import com.android.internal.util.FileRotator.Reader
import com.android.server.connectivity.benchmarktests.R
import com.android.server.net.NetworkStatsRecorder
import java.io.BufferedInputStream
import java.io.DataInputStream
@@ -44,23 +43,22 @@ class NetworkStatsTest {
companion object {
private val DEFAULT_BUFFER_SIZE = 8192
private val FILE_CACHE_WARM_UP_REPEAT_COUNT = 10
private val TEST_REPEAT_COUNT = 10
private val UID_COLLECTION_BUCKET_DURATION_MS = TimeUnit.HOURS.toMillis(2)
private val UID_RECORDER_ROTATE_AGE_MS = TimeUnit.DAYS.toMillis(15)
private val UID_RECORDER_DELETE_AGE_MS = TimeUnit.DAYS.toMillis(90)
private val TEST_DATASET_SUBFOLDER = "dataset/"
private val testFilesDir by lazy {
// These file generated by using real user dataset which has many uid records
// and agreed to share the dataset for testing purpose. These dataset can be
// extracted from rooted devices by using
// "adb pull /data/misc/apexdata/com.android.tethering/netstats" command.
val zipInputStream =
ZipInputStream(getInputStreamForResource(R.raw.netstats_many_uids_zip))
unzipToTempDir(zipInputStream)
}
private val uidTestFiles: List<File> by lazy {
getSortedListForPrefix(testFilesDir, "uid")
// These files are generated by using real user dataset which has many uid records
// and agreed to share the dataset for testing purpose. These dataset can be
// extracted from rooted devices by using
// "adb pull /data/misc/apexdata/com.android.tethering/netstats" command.
private val testFilesAssets by lazy {
val zipFiles = context.assets.list(TEST_DATASET_SUBFOLDER)!!.asList()
zipFiles.map {
val zipInputStream =
ZipInputStream((TEST_DATASET_SUBFOLDER + it).toAssetInputStream())
File(unzipToTempDir(zipInputStream), "netstats")
}
}
// Test results shows the test cases who read the file first will take longer time to
@@ -72,24 +70,34 @@ class NetworkStatsTest {
@BeforeClass
fun setUpOnce() {
repeat(FILE_CACHE_WARM_UP_REPEAT_COUNT) {
val collection = NetworkStatsCollection(UID_COLLECTION_BUCKET_DURATION_MS)
for (file in uidTestFiles) {
readFile(file, collection)
testFilesAssets.forEach {
val uidTestFiles = getSortedListForPrefix(it, "uid")
val collection = NetworkStatsCollection(UID_COLLECTION_BUCKET_DURATION_MS)
for (file in uidTestFiles) {
readFile(file, collection)
}
}
}
}
private fun getInputStreamForResource(resourceId: Int): DataInputStream =
DataInputStream(
InstrumentationRegistry.getContext()
.getResources().openRawResource(resourceId)
)
val context get() = InstrumentationRegistry.getInstrumentation().getContext()
private fun String.toAssetInputStream() = DataInputStream(context.assets.open(this))
private fun unzipToTempDir(zis: ZipInputStream): File {
val statsDir =
Files.createTempDirectory(NetworkStatsTest::class.simpleName).toFile()
generateSequence { zis.nextEntry }.forEach { entry ->
FileOutputStream(File(statsDir, entry.name)).use {
val entryFile = File(statsDir, entry.name)
if (entry.isDirectory) {
entryFile.mkdirs()
return@forEach
}
// Make sure all folders exists. There is no guarantee anywhere.
entryFile.parentFile!!.mkdirs()
// If the entry is a file extract it.
FileOutputStream(entryFile).use {
zis.copyTo(it, DEFAULT_BUFFER_SIZE)
}
}
@@ -99,7 +107,7 @@ class NetworkStatsTest {
// List [xt|uid|uid_tag].<start>-<end> files under the given directory.
private fun getSortedListForPrefix(statsDir: File, prefix: String): List<File> {
assertTrue(statsDir.exists())
return statsDir.list() { dir, name -> name.startsWith("$prefix.") }
return statsDir.list { _, name -> name.startsWith("$prefix.") }
.orEmpty()
.map { it -> File(statsDir, it) }
.sorted()
@@ -115,7 +123,8 @@ class NetworkStatsTest {
fun testReadCollection_manyUids() {
// The file cache is warmed up by the @BeforeClass method, so now the test can repeat
// this a number of time to have a stable number.
repeat(TEST_REPEAT_COUNT) {
testFilesAssets.forEach {
val uidTestFiles = getSortedListForPrefix(it, "uid")
val collection = NetworkStatsCollection(UID_COLLECTION_BUCKET_DURATION_MS)
for (file in uidTestFiles) {
readFile(file, collection)
@@ -127,10 +136,10 @@ class NetworkStatsTest {
fun testReadFromRecorder_manyUids() {
val mockObserver = mock<NonMonotonicObserver<String>>()
val mockDropBox = mock<DropBoxManager>()
repeat(TEST_REPEAT_COUNT) {
testFilesAssets.forEach {
val recorder = NetworkStatsRecorder(
FileRotator(
testFilesDir, PREFIX_UID, UID_RECORDER_ROTATE_AGE_MS, UID_RECORDER_DELETE_AGE_MS
it, PREFIX_UID, UID_RECORDER_ROTATE_AGE_MS, UID_RECORDER_DELETE_AGE_MS
),
mockObserver,
mockDropBox,