SimpleTimer.java revision 243c23618b066bcdcd0ab9e36d8c01f50db2cbd0
1243c23618b066bcdcd0ab9e36d8c01f50db2cbd0Andy Huang/* 2243c23618b066bcdcd0ab9e36d8c01f50db2cbd0Andy Huang * Copyright (C) 2013 The Android Open Source Project 3243c23618b066bcdcd0ab9e36d8c01f50db2cbd0Andy Huang * 4243c23618b066bcdcd0ab9e36d8c01f50db2cbd0Andy Huang * Licensed under the Apache License, Version 2.0 (the "License"); 5243c23618b066bcdcd0ab9e36d8c01f50db2cbd0Andy Huang * you may not use this file except in compliance with the License. 6243c23618b066bcdcd0ab9e36d8c01f50db2cbd0Andy Huang * You may obtain a copy of the License at 7243c23618b066bcdcd0ab9e36d8c01f50db2cbd0Andy Huang * 8243c23618b066bcdcd0ab9e36d8c01f50db2cbd0Andy Huang * http://www.apache.org/licenses/LICENSE-2.0 9243c23618b066bcdcd0ab9e36d8c01f50db2cbd0Andy Huang * 10243c23618b066bcdcd0ab9e36d8c01f50db2cbd0Andy Huang * Unless required by applicable law or agreed to in writing, software 11243c23618b066bcdcd0ab9e36d8c01f50db2cbd0Andy Huang * distributed under the License is distributed on an "AS IS" BASIS, 12243c23618b066bcdcd0ab9e36d8c01f50db2cbd0Andy Huang * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13243c23618b066bcdcd0ab9e36d8c01f50db2cbd0Andy Huang * See the License for the specific language governing permissions and 14243c23618b066bcdcd0ab9e36d8c01f50db2cbd0Andy Huang * limitations under the License. 15243c23618b066bcdcd0ab9e36d8c01f50db2cbd0Andy Huang */ 1630e2c24b056542f3b1b438aeb798305d1226d0c8Andy Huangpackage com.android.mail.perf; 176f92de64bbdf1ea6c4cd9774fc96921a10c266d7Mindy Pereira 186f92de64bbdf1ea6c4cd9774fc96921a10c266d7Mindy Pereiraimport android.os.SystemClock; 19243c23618b066bcdcd0ab9e36d8c01f50db2cbd0Andy Huangimport android.text.TextUtils; 206f92de64bbdf1ea6c4cd9774fc96921a10c266d7Mindy Pereira 2130e2c24b056542f3b1b438aeb798305d1226d0c8Andy Huangimport com.android.mail.utils.LogUtils; 226f92de64bbdf1ea6c4cd9774fc96921a10c266d7Mindy Pereira 236f92de64bbdf1ea6c4cd9774fc96921a10c266d7Mindy Pereira/** 24243c23618b066bcdcd0ab9e36d8c01f50db2cbd0Andy Huang * A simple perf timer class that supports lap-time-style measurements. Once a 25243c23618b066bcdcd0ab9e36d8c01f50db2cbd0Andy Huang * timer is started, any number of laps can be marked, but they are all relative 26243c23618b066bcdcd0ab9e36d8c01f50db2cbd0Andy Huang * to the original start time. 276f92de64bbdf1ea6c4cd9774fc96921a10c266d7Mindy Pereira */ 286f92de64bbdf1ea6c4cd9774fc96921a10c266d7Mindy Pereirapublic class SimpleTimer { 296f92de64bbdf1ea6c4cd9774fc96921a10c266d7Mindy Pereira 30243c23618b066bcdcd0ab9e36d8c01f50db2cbd0Andy Huang private static final String DEFAULT_LOG_TAG = "SimpleTimer"; 31243c23618b066bcdcd0ab9e36d8c01f50db2cbd0Andy Huang 32243c23618b066bcdcd0ab9e36d8c01f50db2cbd0Andy Huang private static final boolean ENABLE_SIMPLE_TIMER = false; 336f92de64bbdf1ea6c4cd9774fc96921a10c266d7Mindy Pereira 346f92de64bbdf1ea6c4cd9774fc96921a10c266d7Mindy Pereira private final boolean mEnabled; 356f92de64bbdf1ea6c4cd9774fc96921a10c266d7Mindy Pereira private long mStartTime; 36243c23618b066bcdcd0ab9e36d8c01f50db2cbd0Andy Huang private long mLastMarkTime; 376f92de64bbdf1ea6c4cd9774fc96921a10c266d7Mindy Pereira private String mSessionName; 386f92de64bbdf1ea6c4cd9774fc96921a10c266d7Mindy Pereira 396f92de64bbdf1ea6c4cd9774fc96921a10c266d7Mindy Pereira public SimpleTimer() { 406f92de64bbdf1ea6c4cd9774fc96921a10c266d7Mindy Pereira this(false); 416f92de64bbdf1ea6c4cd9774fc96921a10c266d7Mindy Pereira } 426f92de64bbdf1ea6c4cd9774fc96921a10c266d7Mindy Pereira 436f92de64bbdf1ea6c4cd9774fc96921a10c266d7Mindy Pereira public SimpleTimer(boolean enabled) { 446f92de64bbdf1ea6c4cd9774fc96921a10c266d7Mindy Pereira mEnabled = enabled; 456f92de64bbdf1ea6c4cd9774fc96921a10c266d7Mindy Pereira } 466f92de64bbdf1ea6c4cd9774fc96921a10c266d7Mindy Pereira 47243c23618b066bcdcd0ab9e36d8c01f50db2cbd0Andy Huang public final boolean isEnabled() { 48243c23618b066bcdcd0ab9e36d8c01f50db2cbd0Andy Huang return ENABLE_SIMPLE_TIMER && LogUtils.isLoggable(getTag(), LogUtils.DEBUG) 496f92de64bbdf1ea6c4cd9774fc96921a10c266d7Mindy Pereira && mEnabled; 506f92de64bbdf1ea6c4cd9774fc96921a10c266d7Mindy Pereira } 516f92de64bbdf1ea6c4cd9774fc96921a10c266d7Mindy Pereira 52243c23618b066bcdcd0ab9e36d8c01f50db2cbd0Andy Huang public SimpleTimer withSessionName(String sessionName) { 53243c23618b066bcdcd0ab9e36d8c01f50db2cbd0Andy Huang mSessionName = sessionName; 54243c23618b066bcdcd0ab9e36d8c01f50db2cbd0Andy Huang return this; 556f92de64bbdf1ea6c4cd9774fc96921a10c266d7Mindy Pereira } 566f92de64bbdf1ea6c4cd9774fc96921a10c266d7Mindy Pereira 57243c23618b066bcdcd0ab9e36d8c01f50db2cbd0Andy Huang public void start() { 58243c23618b066bcdcd0ab9e36d8c01f50db2cbd0Andy Huang mStartTime = mLastMarkTime = SystemClock.uptimeMillis(); 59243c23618b066bcdcd0ab9e36d8c01f50db2cbd0Andy Huang LogUtils.d(getTag(), "timer START"); 606f92de64bbdf1ea6c4cd9774fc96921a10c266d7Mindy Pereira } 616f92de64bbdf1ea6c4cd9774fc96921a10c266d7Mindy Pereira 626f92de64bbdf1ea6c4cd9774fc96921a10c266d7Mindy Pereira public void mark(String msg) { 636f92de64bbdf1ea6c4cd9774fc96921a10c266d7Mindy Pereira if (isEnabled()) { 64243c23618b066bcdcd0ab9e36d8c01f50db2cbd0Andy Huang long now = SystemClock.uptimeMillis(); 65243c23618b066bcdcd0ab9e36d8c01f50db2cbd0Andy Huang LogUtils.d(getTag(), "[%s] %sms elapsed (%sms since last mark)", msg, now - mStartTime, 66243c23618b066bcdcd0ab9e36d8c01f50db2cbd0Andy Huang now - mLastMarkTime); 67243c23618b066bcdcd0ab9e36d8c01f50db2cbd0Andy Huang mLastMarkTime = now; 686f92de64bbdf1ea6c4cd9774fc96921a10c266d7Mindy Pereira } 696f92de64bbdf1ea6c4cd9774fc96921a10c266d7Mindy Pereira } 706f92de64bbdf1ea6c4cd9774fc96921a10c266d7Mindy Pereira 71243c23618b066bcdcd0ab9e36d8c01f50db2cbd0Andy Huang private String getTag() { 72243c23618b066bcdcd0ab9e36d8c01f50db2cbd0Andy Huang return TextUtils.isEmpty(mSessionName) ? DEFAULT_LOG_TAG : mSessionName; 73243c23618b066bcdcd0ab9e36d8c01f50db2cbd0Andy Huang } 74243c23618b066bcdcd0ab9e36d8c01f50db2cbd0Andy Huang 756f92de64bbdf1ea6c4cd9774fc96921a10c266d7Mindy Pereira} 76