Add more unit tests for client.result, and more small code tweaks.
[zxing.git] / androidtest / src / com / google / zxing / client / androidtest / BenchmarkThread.java
1 /*
2  * Copyright (C) 2008 ZXing authors
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 android.os.Debug;
20 import android.os.Message;
21 import android.util.Log;
22 import com.google.zxing.MultiFormatReader;
23 import com.google.zxing.ReaderException;
24 import com.google.zxing.Result;
25
26 import java.io.File;
27 import java.io.FileNotFoundException;
28 import java.util.Arrays;
29 import java.util.ArrayList;
30 import java.util.List;
31
32 final class BenchmarkThread extends Thread {
33
34   private static final String TAG = "BenchmarkThread";
35   private static final int RUNS = 10;
36
37   private final BenchmarkActivity mActivity;
38   private final String mPath;
39   private MultiFormatReader mMultiFormatReader;
40
41   BenchmarkThread(BenchmarkActivity activity, String path) {
42     mActivity = activity;
43     mPath = path;
44   }
45
46   @Override
47   public void run() {
48     mMultiFormatReader = new MultiFormatReader();
49     mMultiFormatReader.setHints(null);
50     // Try to get in a known state before starting the benchmark
51     System.gc();
52
53     List<BenchmarkItem> items = new ArrayList<BenchmarkItem>();
54     walkTree(mPath, items);
55     Message message = Message.obtain(mActivity.mHandler, R.id.benchmark_done);
56     message.obj = items;
57     message.sendToTarget();
58   }
59
60   // Recurse to allow subdirectories
61   private void walkTree(String path, List<BenchmarkItem> items) {
62     File file = new File(path);
63     if (file.isDirectory()) {
64       String[] files = file.list();
65       Arrays.sort(files);
66       for (int x = 0; x < files.length; x++) {
67         walkTree(file.getAbsolutePath() + '/' + files[x], items);
68       }
69     } else {
70       BenchmarkItem item = decode(path);
71       if (item != null) {
72         items.add(item);
73       }
74     }
75   }
76
77   private BenchmarkItem decode(String path) {
78     RGBMonochromeBitmapSource source;
79     try {
80       source = new RGBMonochromeBitmapSource(path);
81     } catch (FileNotFoundException e) {
82       Log.e(TAG, e.toString());
83       return null;
84     }
85
86     BenchmarkItem item = new BenchmarkItem(path, RUNS);
87     for (int x = 0; x < RUNS; x++) {
88       boolean success;
89       Result result = null;
90       // Using this call instead of getting the time should eliminate a lot of variability due to
91       // scheduling and what else is happening in the system.
92       long now = Debug.threadCpuTimeNanos();
93       try {
94         result = mMultiFormatReader.decodeWithState(source);
95         success = true;
96       } catch (ReaderException e) {
97         success = false;
98       }
99       now = Debug.threadCpuTimeNanos() - now;
100       if (x == 0) {
101         item.setDecoded(success);
102         item.setFormat(result != null ? result.getBarcodeFormat() : null);
103       }
104       item.addResult((int) (now / 1000));
105     }
106     return item;
107   }
108
109 }