blob: 572a9b8137a969a2489adf1fece664b18dc3288e [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.test;
18
Jack Wangff1df692009-08-26 17:19:13 -070019import android.os.Bundle;
20import android.os.PerformanceCollector;
21import android.os.PerformanceCollector.PerformanceResultsWriter;
22
23import java.lang.reflect.Method;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080024
25/**
Jack Wangff1df692009-08-26 17:19:13 -070026 * Provides hooks and wrappers to automatically and manually collect and report
27 * performance data in tests.
28 *
29 * {@hide} Pending approval for public API.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080030 */
Jack Wangff1df692009-08-26 17:19:13 -070031public class PerformanceTestBase extends InstrumentationTestCase implements PerformanceTestCase {
32
33 private static PerformanceCollector sPerfCollector = new PerformanceCollector();
34 private static int sNumTestMethods = 0;
35 private static int sNumTestMethodsLeft = 0;
36
37 // Count number of tests, used to emulate beforeClass and afterClass from JUnit4
38 public PerformanceTestBase() {
39 if (sNumTestMethods == 0) {
40 Method methods[] = getClass().getMethods();
41 for (Method m : methods) {
42 if (m.getName().startsWith("test")) {
43 sNumTestMethods ++;
44 sNumTestMethodsLeft ++;
45 }
46 }
47 }
48 }
49
50 @Override
51 protected void setUp() throws Exception {
52 super.setUp();
53 // @beforeClass
54 // Will skew timing measured by TestRunner, but not by PerformanceCollector
55 if (sNumTestMethodsLeft == sNumTestMethods) {
56 sPerfCollector.beginSnapshot(this.getClass().getName());
57 }
58 }
59
60 @Override
61 protected void tearDown() throws Exception {
62 // @afterClass
63 // Will skew timing measured by TestRunner, but not by PerformanceCollector
64 if (--sNumTestMethodsLeft == 0) {
65 sPerfCollector.endSnapshot();
66 }
67 super.tearDown();
68 }
69
70 public void setPerformanceResultsWriter(PerformanceResultsWriter writer) {
71 sPerfCollector.setPerformanceResultsWriter(writer);
72 }
73
74 /**
75 * @see PerformanceCollector#beginSnapshot(String)
76 */
77 protected void beginSnapshot(String label) {
78 sPerfCollector.beginSnapshot(label);
79 }
80
81 /**
82 * @see PerformanceCollector#endSnapshot()
83 */
84 protected Bundle endSnapshot() {
85 return sPerfCollector.endSnapshot();
86 }
87
88 /**
89 * @see PerformanceCollector#startTiming(String)
90 */
91 protected void startTiming(String label) {
92 sPerfCollector.startTiming(label);
93 }
94
95 /**
96 * @see PerformanceCollector#addIteration(String)
97 */
98 protected Bundle addIteration(String label) {
99 return sPerfCollector.addIteration(label);
100 }
101
102 /**
103 * @see PerformanceCollector#stopTiming(String)
104 */
105 protected Bundle stopTiming(String label) {
106 return sPerfCollector.stopTiming(label);
107 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800108
109 public int startPerformance(PerformanceTestCase.Intermediates intermediates) {
110 return 0;
111 }
112
113 public boolean isPerformanceOnly() {
114 return true;
115 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800116}