• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2011 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.text.DecimalFormat;
23 import java.text.ParseException;
24 import java.util.regex.Matcher;
25 import java.util.regex.Pattern;
26 
27 /**
28 * A {@link IShellOutputReceiver} that parses the benchmark test data output, collecting metrics on
29 * average time per operation.
30 * <p/>
31 * Looks for the following output
32 * <p/>
33 * <code>
34 * Time per iteration min: X avg: Y max: Z
35 * </code>
36 */
37 public class NativeBenchmarkTestParser extends MultiLineReceiver {
38 
39     private final static String LOG_TAG = "NativeBenchmarkTestParser";
40 
41     // just parse any string
42     private final static String FLOAT_STRING = "\\s*(.*)\\s*";
43     private final static String COMPLETE_STRING = String.format(
44             "Time per iteration min:%savg:%smax:%s", FLOAT_STRING, FLOAT_STRING, FLOAT_STRING);
45     private final static Pattern COMPLETE_PATTERN = Pattern.compile(COMPLETE_STRING);
46 
47     private final String mTestRunName;
48     private boolean mIsCanceled = false;
49     private double mMinOpTime = 0;
50     private double mAvgOpTime = 0;
51     private double mMaxOpTime = 0;
52 
53     /**
54      * Creates a {@link NativeBenchmarkTestParser}.
55      *
56      * @param runName the run name. Used for logging purposes.
57      */
NativeBenchmarkTestParser(String runName)58     public NativeBenchmarkTestParser(String runName) {
59         mTestRunName = runName;
60     }
61 
62     /**
63      * {@inheritDoc}
64      */
65     @Override
processNewLines(String[] lines)66     public void processNewLines(String[] lines) {
67         for (String line : lines) {
68             parseLine(line);
69         }
70     }
71 
parseLine(String line)72     private void parseLine(String line) {
73         Log.d(LOG_TAG, line);
74         Matcher matcher = COMPLETE_PATTERN.matcher(line);
75         if (matcher.find()) {
76             Log.i(LOG_TAG, String.format("Found result for benchmark %s: %s", getRunName(), line));
77             mMinOpTime = parseDoubleValue(line, matcher.group(1));
78             mAvgOpTime = parseDoubleValue(line, matcher.group(2));
79             mMaxOpTime = parseDoubleValue(line, matcher.group(3));
80         }
81     }
82 
parseDoubleValue(String line, String valueString)83     private double parseDoubleValue(String line, String valueString) {
84         try {
85             return Double.parseDouble(valueString);
86         } catch (NumberFormatException e) {
87             // fall through
88         }
89         Log.w(LOG_TAG, String.format("Value was not a double (%s), trying for scientfic",
90                 line));
91         DecimalFormat format = new DecimalFormat("0.0E0");
92         try {
93             Number num = format.parse(valueString);
94             return num.doubleValue();
95         } catch (ParseException e) {
96             Log.e(LOG_TAG, String.format("Could not parse double value in (%s)",
97                     line));
98         }
99         return 0;
100     }
101 
102     /**
103      * {@inheritDoc}
104      */
105     @Override
isCancelled()106     public boolean isCancelled() {
107         return mIsCanceled;
108     }
109 
110     /**
111      * @return The name of the Test Run.
112      */
getRunName()113     public String getRunName() {
114         return mTestRunName;
115     }
116 
117     /**
118      * @return the average operation time
119      */
getAvgOperationTime()120     public double getAvgOperationTime() {
121         return mAvgOpTime;
122     }
123 
124     /**
125      * @return the minimum operation time
126      */
getMinOperationTime()127     public double getMinOperationTime() {
128         return mMinOpTime;
129     }
130 
131     /**
132      * @return the maximum operation time
133      */
getMaxOperationTime()134     public double getMaxOperationTime() {
135         return mMaxOpTime;
136     }
137 }
138