Home | History | Annotate | Download | only in testtype
      1 /*
      2  * Copyright (C) 2010 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 package com.android.tradefed.testtype;
     17 
     18 import com.android.ddmlib.IShellOutputReceiver;
     19 import com.android.ddmlib.Log;
     20 import com.android.ddmlib.MultiLineReceiver;
     21 
     22 import java.util.regex.Matcher;
     23 import java.util.regex.Pattern;
     24 
     25 /**
     26 * A {@link IShellOutputReceiver} that parses the stress test data output, collecting metrics on
     27 * number of iterations complete and average time per iteration.
     28 * <p/>
     29 * Looks for the following output
     30 * <p/>
     31 * <code>
     32 * pass 0
     33 * ...
     34 * ==== pass X
     35 * Successfully completed X passes
     36 * </code>
     37 * <br/>
     38 * where 'X' refers to the iteration number
     39 */
     40 public class NativeStressTestParser extends MultiLineReceiver {
     41 
     42     private final static String LOG_TAG = "NativeStressTestParser";
     43 
     44     private final static Pattern ITERATION_COMPLETE_PATTERN = Pattern.compile(
     45             "^====\\s*Completed\\s*pass:\\s*(\\d+)");
     46 
     47     private final String mTestRunName;
     48     private boolean mIsCanceled = false;
     49     private int mTotalIterations = 0;
     50 
     51     /**
     52      * Creates a {@link NativeStressTestParser}.
     53      *
     54      * @param runName the run name. Used for logging purposes.
     55      */
     56     public NativeStressTestParser(String runName) {
     57         mTestRunName = runName;
     58     }
     59 
     60     /**
     61      * {@inheritDoc}
     62      */
     63     @Override
     64     public void processNewLines(String[] lines) {
     65         for (String line : lines) {
     66             parseLine(line);
     67         }
     68     }
     69 
     70     private void parseLine(String line) {
     71         Matcher matcher = ITERATION_COMPLETE_PATTERN.matcher(line);
     72         if (matcher.find()) {
     73             parseIterationValue(line, matcher.group(1));
     74         }
     75     }
     76 
     77     private void parseIterationValue(String line, String iterationString) {
     78         try {
     79             int currentIteration = Integer.parseInt(iterationString);
     80             Log.i(LOG_TAG, String.format("%s: pass %d", mTestRunName, currentIteration));
     81             mTotalIterations++;
     82         } catch (NumberFormatException e) {
     83             // this should never happen, since regular expression matches on digits
     84             Log.e(LOG_TAG, String.format("Unexpected iteration content %s", line));
     85         }
     86     }
     87 
     88     /**
     89      * {@inheritDoc}
     90      */
     91     @Override
     92     public boolean isCancelled() {
     93         return mIsCanceled;
     94     }
     95 
     96     /**
     97      * @return the name of the test run.
     98      */
     99     public String getRunName() {
    100         return mTestRunName;
    101     }
    102 
    103     /**
    104      * @return the total number of iterations completed across one or more runs
    105      */
    106     public int getIterationsCompleted() {
    107         return mTotalIterations;
    108     }
    109 }
    110