Merge "Update ImageStats test to use JSON file instead of txt file." am: 7ed0d63df8
Original change: https://android-review.googlesource.com/c/platform/tools/tradefederation/contrib/+/1915699
Change-Id: I2f09092fd8efb4eafb23f04644a41bbc680a0c35
diff --git a/src/com/android/build/tests/ImageStats.java b/src/com/android/build/tests/ImageStats.java
index 6f59f5c..61f45d5 100644
--- a/src/com/android/build/tests/ImageStats.java
+++ b/src/com/android/build/tests/ImageStats.java
@@ -30,12 +30,12 @@
import com.android.tradefed.util.FileUtil;
import com.android.tradefed.util.proto.TfMetricProtoUtil;
-import java.io.BufferedReader;
+import org.json.JSONArray;
+import org.json.JSONException;
+import org.json.JSONObject;
+
import java.io.File;
-import java.io.FileInputStream;
import java.io.IOException;
-import java.io.InputStream;
-import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
@@ -130,8 +130,8 @@
TestDescription td = new TestDescription(FileUtil.getBaseName(statsFileName),
FILE_SIZES);
listener.testStarted(td);
- try (InputStream in = new FileInputStream(statsFile)) {
- parseFinalMetrics(in, finalFileSizeMetrics);
+ try {
+ parseFinalMetrics(statsFile, finalFileSizeMetrics);
} catch (IOException ioe) {
String message = String.format(
"Failed to parse image stats file: %s",
@@ -161,15 +161,15 @@
/**
* Parse the aggregated metrics that matches the patterns and all individual file size metrics.
*
- * @param in an unread {@link InputStream} for the content of the file sizes; the stream will be
- * fully read after executing the method
+ * @param statsFile {@link File} which contains the file name and the corresponding size
+ * details.
* @param finalFileSizeMetrics final map that will have all the metrics of aggregated and
* individual file names and their corresponding values.
* @throws IOException
*/
- protected void parseFinalMetrics(InputStream in,
+ protected void parseFinalMetrics(File statsFile,
Map<String, String> finalFileSizeMetrics) throws IOException {
- Map<String, Long> individualFileSizes = parseFileSizes(in);
+ Map<String, Long> individualFileSizes = parseFileSizes(statsFile);
// Add aggregated metrics.
finalFileSizeMetrics.putAll(performAggregation(individualFileSizes,
processAggregationPatterns(mAggregationPattern)));
@@ -178,35 +178,25 @@
}
/**
- * Processes text files like 'installed-files.txt' (as built by standard Android build rules for
- * device targets) into a map of file path to file sizes
+ * Processes json files like 'installed-files.json' (as built by standard Android build rules
+ * for device targets) into a map of file path to file sizes
*
- * @param in an unread {@link InputStream} for the content of the file sizes; the stream will be
- * fully read after executing the method
+ * @param statsFile {@link File} which contains the file name and the corresponding size
+ * details.
* @return
*/
- protected Map<String, Long> parseFileSizes(InputStream in) throws IOException {
+ protected Map<String, Long> parseFileSizes(File statsFile) throws IOException {
Map<String, Long> ret = new HashMap<>();
- try (BufferedReader br = new BufferedReader(new InputStreamReader(in))) {
- String line;
- while ((line = br.readLine()) != null) {
- // trim both ends of the raw line and make a split by whitespaces
- // e.g. trying to match a line like this:
- // 96992106 /system/app/Chrome/Chrome.apk
- String[] fields = line.trim().split("\\s+");
- if (fields.length != 2) {
- CLog.w("Unable to split line to file size and name: %s", line);
- continue;
- }
- long size = 0;
- try {
- size = Long.parseLong(fields[0]);
- } catch (NumberFormatException nfe) {
- CLog.w("Failed to parse file size from field '%s', ignored", fields[0]);
- continue;
- }
- ret.put(fields[1], size);
+ try {
+ JSONArray jsonArray = new JSONArray(FileUtil.readStringFromFile(statsFile));
+ for (int i = 0; i < jsonArray.length(); i++) {
+ JSONObject jsonObject = jsonArray.getJSONObject(i);
+ String fileName = jsonObject.getString("Name");
+ Long fileSize = Long.parseLong(jsonObject.getString("Size"));
+ ret.put(fileName, fileSize);
}
+ } catch (JSONException e) {
+ CLog.w("JSONException: %s", e.getMessage());
}
return ret;
}
diff --git a/tests/src/com/android/build/tests/ImageStatsTest.java b/tests/src/com/android/build/tests/ImageStatsTest.java
index aca5642..998650b 100644
--- a/tests/src/com/android/build/tests/ImageStatsTest.java
+++ b/tests/src/com/android/build/tests/ImageStatsTest.java
@@ -16,6 +16,9 @@
package com.android.build.tests;
+import com.android.tradefed.util.FileUtil;
+
+import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
@@ -23,6 +26,7 @@
import org.junit.runners.JUnit4;
import java.io.ByteArrayInputStream;
+import java.io.File;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
@@ -32,21 +36,72 @@
@RunWith(JUnit4.class)
public class ImageStatsTest {
- // data obtained from build 4597696, taimen-userdebug_fastbuild_linux
- private static final String TEST_DATA =
- " 164424453 /system/app/WallpapersBReel2017/WallpapersBReel2017.apk\n"
- + " 124082279 /system/app/Chrome/Chrome.apk\n"
- + " 92966112 /system/priv-app/Velvet/Velvet.apk\n"
- + " 1790897 /system/framework/ext.jar\n"
- + " 505436 /system/fonts/NotoSansEgyptianHieroglyphs-Regular.ttf\n"
- + " 500448 /system/bin/ip6tables\n"
- + " 500393 /system/usr/share/zoneinfo/tzdata\n"
- + " 500380 /system/fonts/NotoSansCuneiform-Regular.ttf\n"
- + " 126391 /system/framework/core-oj.jar\n"
- + " 122641 /system/framework/com.quicinc.cne.jar\n";
- private static final String TEST_DATA_SMALL =
- " 164424453 /system/app/WallpapersBReel2017/WallpapersBReel2017.apk\n"
- + " 124082279 /system/app/Chrome/Chrome.apk\n";
+ private static final String TEST_DATA = " ["
+ + "{\n" +
+ " \"SHA256\": \"94557affe476aaf1bb77bc2114375162c2a7a066d3d4cfde9a545d7b1bcf\",\n" +
+ " \"Name\": \"/system/app/WallpapersBReel2017/WallpapersBReel2017.apk\",\n" +
+ " \"Size\": 164424453\n" +
+ " },\n" +
+ " {\n" +
+ " \"SHA256\": \"c15be36f620a78b98c79cf236175b33a09df8f9946f5814ba27ab2b746476\",\n" +
+ " \"Name\": \"/system/app/Chrome/Chrome.apk\",\n" +
+ " \"Size\": 124082279\n" +
+ " },\n" +
+ " {\n" +
+ " \"SHA256\": \"bff67553c1850054b00324bf24c7e2c57e2d5ca67c475919fde77a661e909\",\n" +
+ " \"Name\": \"/system/priv-app/Velvet/Velvet.apk\",\n" +
+ " \"Size\": 92966112\n" +
+ " },\n" +
+ " {\n" +
+ " \"SHA256\": \"c15be36f620a78b98c79cf236175b33a09df8f9946f5814ba27ab2b746476\",\n" +
+ " \"Name\": \"/system/framework/ext.jar\",\n" +
+ " \"Size\": 1790897\n" +
+ " },\n" +
+ " {\n" +
+ " \"SHA256\": \"c15be36f620a78b98c79cf236175b33a09df8f9946f5814ba27ab2b746476\",\n" +
+ " \"Name\": \"/system/fonts/NotoSansEgyptianHieroglyphs-Regular.ttf\",\n" +
+ " \"Size\": 505436\n" +
+ " },\n" +
+ " {\n" +
+ " \"SHA256\": \"c15be36f620a78b98c79cf236175b33a09df8f9946f5814ba27ab2b746476\",\n" +
+ " \"Name\": \"/system/bin/ip6tables\",\n" +
+ " \"Size\": 500448\n" +
+ " },\n" +
+ " {\n" +
+ " \"SHA256\": \"c15be36f620a78b98c79cf236175b33a09df8f9946f5814ba27ab2b746476\",\n" +
+ " \"Name\": \"/system/usr/share/zoneinfo/tzdata\",\n" +
+ " \"Size\": 500393\n" +
+ " },\n" +
+ " {\n" +
+ " \"SHA256\": \"c15be36f620a78b98c79cf236175b33a09df8f9946f5814ba27ab2b746476\",\n" +
+ " \"Name\": \"/system/fonts/NotoSansCuneiform-Regular.ttf\",\n" +
+ " \"Size\": 500380\n" +
+ " },\n" +
+ " {\n" +
+ " \"SHA256\": \"c15be36f620a78b98c79cf236175b33a09df8f9946f5814ba27ab2b746476\",\n" +
+ " \"Name\": \"/system/framework/core-oj.jar\",\n" +
+ " \"Size\": 126391\n" +
+ " },\n" +
+ " {\n" +
+ " \"SHA256\": \"c15be36f620a78b98c79cf236175b33a09df8f9946f5814ba27ab2b746476\",\n" +
+ " \"Name\": \"/system/framework/com.quicinc.cne.jar\",\n" +
+ " \"Size\": 122641\n" +
+ " }"
+ + "]";
+
+ private static final String TEST_DATA_SMALL = " ["
+ + "{\n" +
+ " \"SHA256\": \"94557affe476aaf1bb77bc2114375162c2a7a066d3d4cfde9a545d7b1bcfa\",\n" +
+ " \"Name\": \"/system/app/WallpapersBReel2017/WallpapersBReel2017.apk\",\n" +
+ " \"Size\": 164424453\n" +
+ " },\n" +
+ " {\n" +
+ " \"SHA256\": \"c15be36f620a78b98c79cf236175b33a09df8f9946f5814ba27ab2b746476\",\n" +
+ " \"Name\": \"/system/app/Chrome/Chrome.apk\",\n" +
+ " \"Size\": 124082279\n" +
+ " }"
+ + "]";
+
private static final Map<String, Long> PARSED_TEST_DATA = new HashMap<>();
static {
@@ -64,15 +119,23 @@
private ImageStats mImageStats = null;
+ private File mTestStatsFile;
+
@Before
public void setup() throws Exception {
mImageStats = new ImageStats();
}
+ @After
+ public void tearDown() throws Exception {
+ FileUtil.deleteFile(mTestStatsFile);
+ }
+
@Test
public void testParseFileSizes() throws Exception {
- Map<String, Long> ret =
- mImageStats.parseFileSizes(new ByteArrayInputStream(TEST_DATA.getBytes()));
+ mTestStatsFile = FileUtil.createTempFile("stats_temp", ".json");
+ FileUtil.writeToFile(TEST_DATA, mTestStatsFile);
+ Map<String, Long> ret = mImageStats.parseFileSizes(mTestStatsFile);
Assert.assertEquals(
"parsed test file sizes mismatches expectations", PARSED_TEST_DATA, ret);
}
@@ -155,8 +218,9 @@
@Test
public void testParseFinalMetrics() throws Exception {
Map<String, String> finalMetrics = new HashMap<>();
- mImageStats.parseFinalMetrics(new ByteArrayInputStream(TEST_DATA_SMALL.getBytes()),
- finalMetrics);
+ mTestStatsFile = FileUtil.createTempFile("stats_temp", ".json");
+ FileUtil.writeToFile(TEST_DATA_SMALL, mTestStatsFile);
+ mImageStats.parseFinalMetrics(mTestStatsFile, finalMetrics);
Assert.assertEquals("Total number of metrics is not as expected", 5, finalMetrics.size());
Assert.assertEquals(
"Failed to get WallpapersBReel2017.apk file metris.",