diff options
author | Teng-Hui Zhu <ztenghui@google.com> | 2016-06-24 14:23:19 -0700 |
---|---|---|
committer | Teng-Hui Zhu <ztenghui@google.com> | 2016-06-27 14:07:00 -0700 |
commit | 119265d05e16c11d84bc51194dd0af6234a7ca4e (patch) | |
tree | 5e385ed1b2161664d0230c5247a1800cb632eda6 /apct-tests/perftests/utils | |
parent | 57fc7599e89ff37e2a92d315baa6576b9dc80dde (diff) |
Use new Rule to report status when success
b/28980976
Change-Id: Ib5e718ca8bfa2753eedcc40d41843f202c8c23cb
Diffstat (limited to 'apct-tests/perftests/utils')
-rw-r--r-- | apct-tests/perftests/utils/Android.mk | 19 | ||||
-rw-r--r-- | apct-tests/perftests/utils/src/android/perftests/utils/PerfStatusReporter.java | 87 |
2 files changed, 96 insertions, 10 deletions
diff --git a/apct-tests/perftests/utils/Android.mk b/apct-tests/perftests/utils/Android.mk index 2dc7d4c7915b..55c13b087626 100644 --- a/apct-tests/perftests/utils/Android.mk +++ b/apct-tests/perftests/utils/Android.mk @@ -1,14 +1,13 @@ - LOCAL_PATH := $(call my-dir) - include $(CLEAR_VARS) +LOCAL_PATH := $(call my-dir) +include $(CLEAR_VARS) - # Build all java files in the java subdirectory - LOCAL_SRC_FILES := $(call all-subdir-java-files) +LOCAL_STATIC_JAVA_LIBRARIES := android-support-test - # Any libraries that this library depends on - LOCAL_JAVA_LIBRARIES := android.test.runner +# Build all java files in the java subdirectory +LOCAL_SRC_FILES := $(call all-subdir-java-files) - # The name of the jar file to create - LOCAL_MODULE := apct-perftests-utils +# The name of the jar file to create +LOCAL_MODULE := apct-perftests-utils - # Build a static jar file. - include $(BUILD_STATIC_JAVA_LIBRARY)
\ No newline at end of file +# Build a static jar file. +include $(BUILD_STATIC_JAVA_LIBRARY)
\ No newline at end of file diff --git a/apct-tests/perftests/utils/src/android/perftests/utils/PerfStatusReporter.java b/apct-tests/perftests/utils/src/android/perftests/utils/PerfStatusReporter.java new file mode 100644 index 000000000000..3933b57753dc --- /dev/null +++ b/apct-tests/perftests/utils/src/android/perftests/utils/PerfStatusReporter.java @@ -0,0 +1,87 @@ +/* + * 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); + } + +} |