Added a new feature to the test app, which captures all the device info and default...
[zxing.git] / androidtest / src / com / google / zxing / client / androidtest / BenchmarkItem.java
1 /*
2  * Copyright (C) 2008 Google Inc.
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
17 package com.google.zxing.client.androidtest;
18
19 import com.google.zxing.BarcodeFormat;
20
21 public final class BenchmarkItem {
22
23   private final String mPath;
24   private final int[] mTimes;
25   private int mPosition;
26   private boolean mDecoded;
27   private BarcodeFormat mFormat;
28
29   public BenchmarkItem(String path, int runs) {
30     if (runs <= 0) {
31       throw new IllegalArgumentException();
32     }
33     mPath = path;
34     mTimes = new int[runs];
35     mPosition = 0;
36     mDecoded = false;
37     mFormat = null;
38   }
39
40   public void addResult(int microseconds) {
41     mTimes[mPosition] = microseconds;
42     mPosition++;
43   }
44
45   public void setDecoded(boolean decoded) {
46     mDecoded = decoded;
47   }
48
49   public void setFormat(BarcodeFormat format) {
50     mFormat = format;
51   }
52
53   @Override
54   public String toString() {
55     StringBuilder result = new StringBuilder();
56     result.append(mDecoded ? ("DECODED " + mFormat.toString() + ": ") : "FAILED: ");
57     result.append(mPath);
58     result.append(" (");
59     result.append(getAverageTime());
60     result.append(" us average)");
61 //    int size = mTimes.length;
62 //    for (int x = 0; x < size; x++) {
63 //      result.append(mTimes[x]);
64 //      result.append(" ");
65 //    }
66     return result.toString();
67   }
68
69   /**
70    * Calculates the average time but throws out the maximum as an outlier first.
71    *
72    * @return The average decoding time in microseconds.
73    */
74   public int getAverageTime() {
75     int size = mTimes.length;
76     int total = 0;
77     int max = mTimes[0];
78     for (int x = 0; x < size; x++) {
79       int time = mTimes[x];
80       total += time;
81       if (time > max) {
82         max = time;
83       }
84     }
85     total -= max;
86     size--;
87     if (size > 0) {
88       return total / size;
89     } else {
90       return 0;
91     }
92   }
93
94 }