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