OSDN Git Service

Introduce Typeface.createXX performance tests.
authorSeigo Nonaka <nona@google.com>
Mon, 3 Oct 2016 10:51:24 +0000 (19:51 +0900)
committerSeigo Nonaka <nona@google.com>
Mon, 3 Oct 2016 10:58:02 +0000 (19:58 +0900)
BUG:29142734
Test: Performed following commands
mmma -j1024 ./frameworks/base/apct-tests/perftests/core/
adb install -r $OUT/data/app/CorePerfTests/CorePerfTests.apk
adb shell am instrument -w \
com.android.perftests.core/android.support.test.runner.AndroidJUnitRunner

Change-Id: I8e5ef3bf729f981d20dc88736d8a1a3d6b972c55

apct-tests/perftests/core/Android.mk
apct-tests/perftests/core/src/android/graphics/perftests/TypefaceCreatePerfTest.java [new file with mode: 0644]

index eb07c05..5fe2f02 100644 (file)
@@ -12,5 +12,8 @@ LOCAL_STATIC_JAVA_LIBRARIES := \
 
 LOCAL_PACKAGE_NAME := CorePerfTests
 
+# Use google-fonts/dancing-script for the performance metrics
+LOCAL_ASSET_DIR := $(TOP)/external/google-fonts/dancing-script
+
 include $(BUILD_PACKAGE)
 
diff --git a/apct-tests/perftests/core/src/android/graphics/perftests/TypefaceCreatePerfTest.java b/apct-tests/perftests/core/src/android/graphics/perftests/TypefaceCreatePerfTest.java
new file mode 100644 (file)
index 0000000..11ee599
--- /dev/null
@@ -0,0 +1,106 @@
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package android.graphics.perftests;
+
+import android.content.Context;
+import android.content.res.AssetManager;
+import android.graphics.Typeface;
+import android.perftests.utils.BenchmarkState;
+import android.perftests.utils.PerfStatusReporter;
+import android.support.test.InstrumentationRegistry;
+import android.support.test.filters.LargeTest;
+import android.support.test.runner.AndroidJUnit4;
+
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+@LargeTest
+@RunWith(AndroidJUnit4.class)
+public class TypefaceCreatePerfTest {
+    // A font file name in asset directory.
+    private static final String TEST_FONT_NAME = "DancingScript-Regular.ttf";
+
+    @Rule
+    public PerfStatusReporter mPerfStatusReporter = new PerfStatusReporter();
+
+    @Test
+    public void testCreate_fromFamily() {
+        BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
+
+        while (state.keepRunning()) {
+            Typeface face = Typeface.create(Typeface.SANS_SERIF, Typeface.NORMAL);
+        }
+    }
+
+    @Test
+    public void testCreate_fromFamilyName() {
+        BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
+
+        while (state.keepRunning()) {
+            Typeface face = Typeface.create("monospace", Typeface.NORMAL);
+        }
+    }
+
+    @Test
+    public void testCreate_fromAsset() {
+        BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
+        final Context context = InstrumentationRegistry.getContext();
+        final AssetManager am = context.getAssets();
+
+        while (state.keepRunning()) {
+            Typeface face = Typeface.createFromAsset(am, TEST_FONT_NAME);
+        }
+    }
+
+    @Test
+    public void testCreate_fromFile() {
+        BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
+        final Context context = InstrumentationRegistry.getContext();
+        final AssetManager am = context.getAssets();
+
+        File outFile = null;
+        try {
+            outFile = File.createTempFile("example", "ttf", context.getCacheDir());
+        } catch (IOException e) {
+            throw new RuntimeException(e);
+        }
+
+        try (InputStream in = am.open(TEST_FONT_NAME);
+                OutputStream out = new FileOutputStream(outFile)) {
+            byte[] buf = new byte[1024];
+            int n = 0;
+            while ((n = in.read(buf)) != -1) {
+                out.write(buf, 0, n);
+            }
+        } catch (IOException e) {
+            throw new RuntimeException(e);
+        }
+
+        while (state.keepRunning()) {
+            Typeface face = Typeface.createFromFile(outFile);
+        }
+
+        outFile.delete();
+    }
+}