Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 119265d0 authored by Teng-Hui Zhu's avatar Teng-Hui Zhu
Browse files

Use new Rule to report status when success

b/28980976

Change-Id: Ib5e718ca8bfa2753eedcc40d41843f202c8c23cb
parent 57fc7599
Loading
Loading
Loading
Loading
+8 −7
Original line number Original line Diff line number Diff line
@@ -18,6 +18,7 @@ package android.widget;


import android.app.Activity;
import android.app.Activity;
import android.os.Bundle;
import android.os.Bundle;
import android.perftests.utils.PerfStatusReporter;
import android.util.Log;
import android.util.Log;


import android.perftests.utils.BenchmarkState;
import android.perftests.utils.BenchmarkState;
@@ -40,12 +41,11 @@ import org.junit.runner.RunWith;
@LargeTest
@LargeTest
@RunWith(Parameterized.class)
@RunWith(Parameterized.class)
public class TextViewSetTextLocalePerfTest {
public class TextViewSetTextLocalePerfTest {

    @Parameters(name = "{0}")
    @Parameters
    public static Collection locales() {
    public static Collection locales() {
        return Arrays.asList(new Object[][] {
        return Arrays.asList(new Object[][] {
            { "TextView_setTextLocale_SameLocale", "en-US", "en-US" },
            { "SameLocale", "en-US", "en-US" },
            { "TextView_setTextLocale_DifferentLocale", "en-US", "ja-JP"}
            { "DifferentLocale", "en-US", "ja-JP"}
        });
        });
    }
    }


@@ -63,17 +63,18 @@ public class TextViewSetTextLocalePerfTest {
    @Rule
    @Rule
    public ActivityTestRule<StubActivity> mActivityRule = new ActivityTestRule(StubActivity.class);
    public ActivityTestRule<StubActivity> mActivityRule = new ActivityTestRule(StubActivity.class);


    @Rule
    public PerfStatusReporter mPerfStatusReporter = new PerfStatusReporter();

    @Test
    @Test
    public void testSetTextLocale() {
    public void testSetTextLocale() {
        TextView textView = new TextView(mActivityRule.getActivity());
        TextView textView = new TextView(mActivityRule.getActivity());


        BenchmarkState state = new BenchmarkState();
        BenchmarkState state = mPerfStatusReporter.getBenchmarkState();


        while (state.keepRunning()) {
        while (state.keepRunning()) {
            textView.setTextLocale(mFirstLocale);
            textView.setTextLocale(mFirstLocale);
            textView.setTextLocale(mSecondLocale);
            textView.setTextLocale(mSecondLocale);
        }
        }

        state.sendFullStatusReport(InstrumentationRegistry.getInstrumentation(), mMetricKey);
    }
    }
}
}
+4 −6
Original line number Original line Diff line number Diff line
@@ -23,6 +23,7 @@ import android.graphics.Color;
import android.graphics.drawable.VectorDrawable;
import android.graphics.drawable.VectorDrawable;
import android.perftests.utils.BenchmarkState;
import android.perftests.utils.BenchmarkState;
import android.perftests.utils.BitmapUtils;
import android.perftests.utils.BitmapUtils;
import android.perftests.utils.PerfStatusReporter;
import android.perftests.utils.StubActivity;
import android.perftests.utils.StubActivity;
import android.support.test.InstrumentationRegistry;
import android.support.test.InstrumentationRegistry;
import android.support.test.rule.ActivityTestRule;
import android.support.test.rule.ActivityTestRule;
@@ -48,12 +49,12 @@ public class VectorDrawablePerfTest {
    private int[] mTestWidths = {1024, 512};
    private int[] mTestWidths = {1024, 512};
    private int[] mTestHeights = {512, 1024};
    private int[] mTestHeights = {512, 1024};


    private String KEY_VECTORDRAWABLE_DRAW_TIME = "VectorDrawable_Draw_Time_NanoSec";

    @Rule
    @Rule
    public ActivityTestRule<StubActivity> mActivityRule =
    public ActivityTestRule<StubActivity> mActivityRule =
            new ActivityTestRule(StubActivity.class);
            new ActivityTestRule(StubActivity.class);


    @Rule
    public PerfStatusReporter mPerfStatusReporter = new PerfStatusReporter();


    @Test
    @Test
    public void testBitmapDrawPerf() {
    public void testBitmapDrawPerf() {
@@ -66,7 +67,7 @@ public class VectorDrawablePerfTest {
        Bitmap bmp = Bitmap.createBitmap(w, h, conf);
        Bitmap bmp = Bitmap.createBitmap(w, h, conf);
        Canvas canvas = new Canvas(bmp);
        Canvas canvas = new Canvas(bmp);


        BenchmarkState state = new BenchmarkState();
        BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
        int i = 0;
        int i = 0;
        while (state.keepRunning()) {
        while (state.keepRunning()) {
            // Use different width / height each to force the vectorDrawable abandon the cache.
            // Use different width / height each to force the vectorDrawable abandon the cache.
@@ -86,8 +87,5 @@ public class VectorDrawablePerfTest {
        if (DUMP_BITMAP) {
        if (DUMP_BITMAP) {
            BitmapUtils.saveBitmapIntoPNG(activity, bmp, resId);
            BitmapUtils.saveBitmapIntoPNG(activity, bmp, resId);
        }
        }

        state.sendFullStatusReport(InstrumentationRegistry.getInstrumentation(),
                KEY_VECTORDRAWABLE_DRAW_TIME);
    }
    }
}
}
+9 −10
Original line number Original line Diff line number Diff line
LOCAL_PATH := $(call my-dir)
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
include $(CLEAR_VARS)


LOCAL_STATIC_JAVA_LIBRARIES := android-support-test

# Build all java files in the java subdirectory
# Build all java files in the java subdirectory
LOCAL_SRC_FILES := $(call all-subdir-java-files)
LOCAL_SRC_FILES := $(call all-subdir-java-files)


 # Any libraries that this library depends on
 LOCAL_JAVA_LIBRARIES := android.test.runner

# The name of the jar file to create
# The name of the jar file to create
LOCAL_MODULE := apct-perftests-utils
LOCAL_MODULE := apct-perftests-utils


+87 −0
Original line number Original line Diff line number Diff line
/*
 * 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.perftests.utils;

import android.support.test.InstrumentationRegistry;

import org.junit.rules.TestWatcher;
import org.junit.runner.Description;

import static junit.framework.Assert.assertFalse;
import static junit.framework.Assert.assertTrue;

/**
 * Use this rule to make sure we report the status after the test success.
 *
 * <code>
 *
 * @Rule public PerfStatusReporter mPerfStatusReporter = new PerfStatusReporter();
 * @Test public void functionName() {
 *     ...
 *     BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
 *     while (state.keepRunning()) {
 *         // DO YOUR TEST HERE!
 *     }
 *     ...
 * }
 * </code>
 *
 * When test succeeded, the status report will use the key as
 * "functionName[optional subTestName]_*"
 *
 * Notice that optional subTestName can't be just numbers, that means each sub test needs to have a
 * name when using parameterization.
 */

public class PerfStatusReporter extends TestWatcher {
    private final BenchmarkState mState = new BenchmarkState();

    public BenchmarkState getBenchmarkState() {
        return mState;
    }

    @Override
    protected void succeeded(Description description) {
        String invokeMethodName = description.getMethodName();
        // validate and simplify the function name.
        // First, remove the "test" prefix which normally comes from CTS test.
        // Then make sure the [subTestName] is valid, not just numbers like [0].
        if (invokeMethodName.startsWith("test")) {
            assertTrue("The test name " + invokeMethodName + " is too short",
                    invokeMethodName.length() > 5);
            invokeMethodName = invokeMethodName.substring(4, 5).toLowerCase()
                    + invokeMethodName.substring(5);
        }

        int index = invokeMethodName.lastIndexOf('[');
        if (index > 0) {
            boolean allDigits = true;
            for (int i = index + 1; i < invokeMethodName.length() - 1; i++) {
                if (!Character.isDigit(invokeMethodName.charAt(i))) {
                    allDigits = false;
                    break;
                }
            }
            assertFalse("The name in [] can't contain only digits for " + invokeMethodName,
                    allDigits);
        }

        mState.sendFullStatusReport(InstrumentationRegistry.getInstrumentation(),
                invokeMethodName);
    }

}