making stuff final, weakening types, etc. per IntelliJ analysis
[zxing.git] / androidtest / src / com / google / zxing / client / androidtest / BenchmarkItem.java
index 3bd68e1..bd23cce 100644 (file)
@@ -18,9 +18,9 @@ package com.google.zxing.client.androidtest;
 
 import com.google.zxing.BarcodeFormat;
 
-public class BenchmarkItem {
+public final class BenchmarkItem {
 
-  private String mPath;
+  private final String mPath;
   private int[] mTimes;
   private int mPosition;
   private boolean mDecoded;
@@ -28,37 +28,60 @@ public class BenchmarkItem {
 
   public BenchmarkItem(String path, int runs) {
     mPath = path;
+    assert(runs > 0);
     mTimes = new int[runs];
     mPosition = 0;
     mDecoded = false;
     mFormat = null;
   }
 
-  // I'm storing these separately instead of as a running total so I can add features like
-  // calculating the min and max later, or ignoring outliers.
-  public void addResult(int milliseconds) {
-    mTimes[mPosition] = milliseconds;
+  public final void addResult(int microseconds) {
+    mTimes[mPosition] = microseconds;
     mPosition++;
   }
 
-  public void setDecoded(boolean decoded) {
+  public final void setDecoded(boolean decoded) {
     mDecoded = decoded;
   }
 
-  public void setFormat(BarcodeFormat format) {
+  public final void setFormat(BarcodeFormat format) {
     mFormat = format;
   }
 
-  public String getPath() {
-    return mPath;
+  @Override
+  public final String toString() {
+    StringBuffer result = new StringBuffer();
+    result.append(mDecoded ? ("DECODED " + mFormat.toString() + ": ") : "FAILED: ");
+    result.append(mPath);
+    result.append(" (");
+    result.append(getAverageTime());
+    result.append(" us average)");
+//    int size = mTimes.length;
+//    for (int x = 0; x < size; x++) {
+//      result.append(mTimes[x]);
+//      result.append(" ");
+//    }
+    return result.toString();
   }
 
-  public int getAverageMilliseconds() {
+  /**
+   * Calculates the average time but throws out the maximum as an outlier first.
+   *
+   * @return The average decoding time in microseconds.
+   */
+  private int getAverageTime() {
     int size = mTimes.length;
     int total = 0;
+    int max = mTimes[0];
     for (int x = 0; x < size; x++) {
-      total += mTimes[x];
+      int time = mTimes[x];
+      total += time;
+      if (time > max) {
+        max = time;
+      }
     }
+    total -= max;
+    size--;
     if (size > 0) {
       return total / size;
     } else {
@@ -66,16 +89,4 @@ public class BenchmarkItem {
     }
   }
 
-  public int getCount() {
-    return mTimes.length;
-  }
-
-  public boolean getDecoded() {
-    return mDecoded;
-  }
-
-  public BarcodeFormat getFormat() {
-    return mFormat;
-  }
-
 }