Preload font before TypefaceSerializationPerfTest.

This CL disables lazy font loading by preloading font in setUp(),
in order to stabilize testSerializeFontMap() results.

In testSerializeFontMap(), the first serializeFontMap() takes 20x time
due to lazy font loading (first call 40ms / avg 2ms on oriole).
This makes test results unstable by reducing the number of iterations.

ManualBenchmarkState calculates the number of iterations
('mMaxIterations') as follows:
    mMaxIterations = (int) (mTargetTestDurationNs / (warmupDuration / iterations));
where 'iterations' is the number of iterations done in warmup stage.
If the first iteration in warmup stage takes unusually long,
'iterations' becomes small and mMaxIterations will be smaller as well.

Bug: 239758440
Test: atest CorePerfTests:android.graphics.perftests.TypefaceSerializationPerfTest#testSerializeFontMap
Change-Id: I3181962a1807cb7b4ecbc8e26fe629b9f530e1c1
diff --git a/apct-tests/perftests/core/src/android/graphics/perftests/TypefaceSerializationPerfTest.java b/apct-tests/perftests/core/src/android/graphics/perftests/TypefaceSerializationPerfTest.java
index 1e2650d..3a23b54 100644
--- a/apct-tests/perftests/core/src/android/graphics/perftests/TypefaceSerializationPerfTest.java
+++ b/apct-tests/perftests/core/src/android/graphics/perftests/TypefaceSerializationPerfTest.java
@@ -28,6 +28,7 @@
 import androidx.test.filters.LargeTest;
 import androidx.test.runner.AndroidJUnit4;
 
+import org.junit.Before;
 import org.junit.Rule;
 import org.junit.Test;
 import org.junit.runner.RunWith;
@@ -50,6 +51,14 @@
     @Rule
     public PerfManualStatusReporter mPerfManualStatusReporter = new PerfManualStatusReporter();
 
+    @Before
+    public void setUp() {
+        // Parse and load the preinstalled fonts in the test process so that:
+        // (1) Updated fonts do not affect test results.
+        // (2) Lazy-loading of fonts does not affect test results (esp. testSerializeFontMap).
+        Typeface.loadPreinstalledSystemFontMap();
+    }
+
     @ManualBenchmarkState.ManualBenchmarkTest(
             warmupDurationNs = WARMUP_DURATION_NS,
             targetTestDurationNs = TARGET_TEST_DURATION_NS)
@@ -61,8 +70,12 @@
         long elapsedTime = 0;
         while (state.keepRunning(elapsedTime)) {
             long startTime = System.nanoTime();
-            Typeface.serializeFontMap(systemFontMap);
+            SharedMemory sharedMemory = Typeface.serializeFontMap(systemFontMap);
             elapsedTime = System.nanoTime() - startTime;
+            sharedMemory.close();
+            android.util.Log.i(TAG,
+                    "testSerializeFontMap isWarmingUp=" + state.isWarmingUp()
+                            + " elapsedTime=" + elapsedTime);
         }
     }