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
17package android.filterfw.core;
18
19import android.os.SystemClock;
20import android.util.Log;
21import java.util.HashMap;
22
23/**
24 * @hide
25 */
26class StopWatch {
27
28    private int STOP_WATCH_LOGGING_PERIOD = 200;
29    private String TAG = "MFF";
30
31    private String mName;
32    private long mStartTime;
33    private long mTotalTime;
34    private int mNumCalls;
35
36    public StopWatch(String name) {
37        mName = name;
38        mStartTime = -1;
39        mTotalTime = 0;
40        mNumCalls = 0;
41    }
42
43    public void start() {
44        if (mStartTime != -1) {
45             throw new RuntimeException(
46                 "Calling start with StopWatch already running");
47        }
48        mStartTime = SystemClock.elapsedRealtime();
49    }
50
51    public void stop() {
52        if (mStartTime == -1) {
53             throw new RuntimeException(
54                 "Calling stop with StopWatch already stopped");
55        }
56        long stopTime = SystemClock.elapsedRealtime();
57        mTotalTime += stopTime - mStartTime;
58        ++mNumCalls;
59        mStartTime = -1;
60        if (mNumCalls % STOP_WATCH_LOGGING_PERIOD == 0) {
61            Log.i(TAG, "AVG ms/call " + mName + ": " +
62                  String.format("%.1f", mTotalTime * 1.0f / mNumCalls));
63            mTotalTime = 0;
64            mNumCalls = 0;
65        }
66    }
67
68}
69
70public class StopWatchMap {
71
72    public boolean LOG_MFF_RUNNING_TIMES = false;
73
74    private HashMap<String, StopWatch> mStopWatches = null;
75
76    public StopWatchMap() {
77        mStopWatches = new HashMap<String, StopWatch>();
78    }
79
80    public void start(String stopWatchName) {
81        if (!LOG_MFF_RUNNING_TIMES) {
82            return;
83        }
84        if (!mStopWatches.containsKey(stopWatchName)) {
85            mStopWatches.put(stopWatchName, new StopWatch(stopWatchName));
86        }
87        mStopWatches.get(stopWatchName).start();
88    }
89
90    public void stop(String stopWatchName) {
91        if (!LOG_MFF_RUNNING_TIMES) {
92            return;
93        }
94        if (!mStopWatches.containsKey(stopWatchName)) {
95            throw new RuntimeException(
96                "Calling stop with unknown stopWatchName: " + stopWatchName);
97        }
98        mStopWatches.get(stopWatchName).stop();
99    }
100
101}
102