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