1 /* 2 * Copyright (C) 2018 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 17 package com.android.dialer.metrics; 18 19 import android.os.SystemClock; 20 import com.android.dialer.common.Assert; 21 import com.android.dialer.common.LogUtil; 22 import java.util.concurrent.ConcurrentHashMap; 23 import java.util.concurrent.ConcurrentMap; 24 import javax.annotation.concurrent.ThreadSafe; 25 import javax.inject.Inject; 26 import javax.inject.Singleton; 27 28 /** Stub {@link Metrics} which simply logs debug messages to logcat. */ 29 @ThreadSafe 30 @Singleton 31 public final class StubMetrics implements Metrics { 32 33 private final ConcurrentMap<String, StubTimerEvent> namedEvents = new ConcurrentHashMap<>(); 34 private final ConcurrentMap<Integer, StubTimerEvent> unnamedEvents = new ConcurrentHashMap<>(); 35 36 @Inject StubMetrics()37 StubMetrics() {} 38 39 @Override startTimer(String timerEventName)40 public void startTimer(String timerEventName) { 41 namedEvents.put(timerEventName, new StubTimerEvent()); 42 } 43 44 @Override startUnnamedTimer()45 public Integer startUnnamedTimer() { 46 StubTimerEvent stubTimerEvent = new StubTimerEvent(); 47 int id = stubTimerEvent.hashCode(); 48 LogUtil.d("StubMetrics.startUnnamedTimer", "started timer for id: %d", id); 49 unnamedEvents.put(id, stubTimerEvent); 50 return id; 51 } 52 53 @Override stopTimer(String timerEventName)54 public void stopTimer(String timerEventName) { 55 StubTimerEvent stubTimerEvent = namedEvents.remove(timerEventName); 56 if (stubTimerEvent == null) { 57 return; 58 } 59 60 LogUtil.d( 61 "StubMetrics.stopTimer", 62 "%s took %dms", 63 timerEventName, 64 SystemClock.elapsedRealtime() - stubTimerEvent.startTime); 65 } 66 67 @Override stopUnnamedTimer(int timerId, String timerEventName)68 public void stopUnnamedTimer(int timerId, String timerEventName) { 69 long startTime = 70 Assert.isNotNull( 71 unnamedEvents.remove(timerId), 72 "no timer found for id: %d (%s)", 73 timerId, 74 timerEventName) 75 .startTime; 76 77 LogUtil.d( 78 "StubMetrics.stopUnnamedTimer", 79 "%s took %dms", 80 timerEventName, 81 SystemClock.elapsedRealtime() - startTime); 82 } 83 84 @Override startJankRecorder(String eventName)85 public void startJankRecorder(String eventName) { 86 LogUtil.d("StubMetrics.startJankRecorder", "started jank recorder for %s", eventName); 87 } 88 89 @Override stopJankRecorder(String eventName)90 public void stopJankRecorder(String eventName) { 91 LogUtil.d("StubMetrics.startJankRecorder", "stopped jank recorder for %s", eventName); 92 } 93 94 @Override recordMemory(String memoryEventName)95 public void recordMemory(String memoryEventName) { 96 LogUtil.d("StubMetrics.startJankRecorder", "recorded memory for %s", memoryEventName); 97 } 98 99 @Override recordBattery(String batteryEventName)100 public void recordBattery(String batteryEventName) { 101 LogUtil.d("StubMetrics.recordBattery", "recorded battery for %s", batteryEventName); 102 } 103 104 private static class StubTimerEvent { 105 final long startTime; 106 StubTimerEvent()107 StubTimerEvent() { 108 this.startTime = SystemClock.elapsedRealtime(); 109 } 110 } 111 } 112