1/* 2 * Copyright (C) 2012 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 com.android.test.runner.listener; 18 19import com.android.test.runner.TestRequestBuilder; 20 21import org.junit.runner.Description; 22import org.junit.runner.notification.Failure; 23import org.junit.runner.notification.RunListener; 24 25import java.io.PrintStream; 26 27/** 28 * This class measures the elapsed run time of each test, and used it to report back to the user 29 * which suite ({@link SmallSuite}, {@link MediumSuite}, {@link LargeSuite}) the test should belong 30 * to. 31 */ 32public class SuiteAssignmentPrinter extends RunListener { 33 /** 34 * This constant defines the maximum allowed runtime (in ms) for a test included in the "small" 35 * suite. It is used to make an educated guess at what suite an unlabeled test belongs to. 36 */ 37 private static final float SMALL_SUITE_MAX_RUNTIME = 100; 38 39 /** 40 * This constant defines the maximum allowed runtime (in ms) for a test included in the "medium" 41 * suite. It is used to make an educated guess at what suite an unlabeled test belongs to. 42 */ 43 private static final float MEDIUM_SUITE_MAX_RUNTIME = 1000; 44 45 private final PrintStream mWriter; 46 47 public SuiteAssignmentPrinter(PrintStream writer) { 48 mWriter = writer; 49 } 50 51 private long mStartTime; 52 private boolean mTimingValid; 53 54 @Override 55 public void testStarted(Description description) throws Exception { 56 mTimingValid = true; 57 mStartTime = System.currentTimeMillis(); 58 } 59 60 @Override 61 public void testFinished(Description description) throws Exception { 62 long runTime; 63 String assignmentSuite; 64 long endTime = System.currentTimeMillis(); 65 66 if (!mTimingValid || mStartTime < 0) { 67 assignmentSuite = "NA"; 68 runTime = -1; 69 } else { 70 runTime = endTime - mStartTime; 71 if (runTime < SMALL_SUITE_MAX_RUNTIME) { 72 assignmentSuite = TestRequestBuilder.SMALL_SIZE; 73 } else if (runTime < MEDIUM_SUITE_MAX_RUNTIME) { 74 assignmentSuite = TestRequestBuilder.MEDIUM_SIZE; 75 } else { 76 assignmentSuite = TestRequestBuilder.LARGE_SIZE; 77 } 78 } 79 // Clear mStartTime so that we can verify that it gets set next time. 80 mStartTime = -1; 81 82 mWriter.printf("%s#%s\n" + "in %s suite\n" + "runTime: %d ms\n", 83 description.getClassName(), description.getMethodName(), assignmentSuite, 84 runTime); 85 } 86 87 @Override 88 public void testFailure(Failure failure) throws Exception { 89 mTimingValid = false; 90 } 91 92 @Override 93 public void testAssumptionFailure(Failure failure) { 94 mTimingValid = false; 95 } 96 97 @Override 98 public void testIgnored(Description description) throws Exception { 99 mTimingValid = false; 100 } 101} 102