diff --git a/metrics/src/main/java/com/facebook/battery/metrics/appwakeup/AppWakeupMetricsCollector.java b/metrics/src/main/java/com/facebook/battery/metrics/appwakeup/AppWakeupMetricsCollector.java index 44f82a8..718908c 100644 --- a/metrics/src/main/java/com/facebook/battery/metrics/appwakeup/AppWakeupMetricsCollector.java +++ b/metrics/src/main/java/com/facebook/battery/metrics/appwakeup/AppWakeupMetricsCollector.java @@ -10,11 +10,14 @@ import static com.facebook.battery.metrics.appwakeup.AppWakeupMetrics.WakeupDetails; import static com.facebook.battery.metrics.core.Utilities.checkNotNull; +import android.content.Context; import android.os.SystemClock; import com.facebook.battery.metrics.core.SystemMetricsCollector; import com.facebook.battery.metrics.core.SystemMetricsLogger; import com.facebook.infer.annotation.ThreadSafe; +import javax.annotation.Nullable; + /** * This class is used to record and aggregate the wakeups in the app. It exposes methods to record * start {@link #recordWakeupStart(AppWakeupMetrics.WakeupReason, String)} and end {@link @@ -34,7 +37,7 @@ public AppWakeupMetricsCollector() { @Override @ThreadSafe(enableChecks = false) - public synchronized boolean getSnapshot(AppWakeupMetrics snapshot) { + public synchronized boolean getSnapshot(AppWakeupMetrics snapshot, @Nullable Context context) { checkNotNull(snapshot, "Null value passed to getSnapshot!"); // TODO: Optimize by taking intersection of the two lists snapshot.appWakeups.clear(); diff --git a/metrics/src/main/java/com/facebook/battery/metrics/bluetooth/BluetoothMetricsCollector.java b/metrics/src/main/java/com/facebook/battery/metrics/bluetooth/BluetoothMetricsCollector.java index e672298..42be2eb 100644 --- a/metrics/src/main/java/com/facebook/battery/metrics/bluetooth/BluetoothMetricsCollector.java +++ b/metrics/src/main/java/com/facebook/battery/metrics/bluetooth/BluetoothMetricsCollector.java @@ -12,6 +12,7 @@ import android.app.PendingIntent; import android.bluetooth.le.BluetoothLeScanner; import android.bluetooth.le.ScanCallback; +import android.content.Context; import android.os.SystemClock; import android.util.SparseArray; import androidx.annotation.GuardedBy; @@ -19,6 +20,8 @@ import com.facebook.infer.annotation.Nullsafe; import com.facebook.infer.annotation.ThreadSafe; +import javax.annotation.Nullable; + /** * Records information about Bluetooth LE scans, meant to be used with {@link BluetoothLeScanner}. * @@ -55,7 +58,7 @@ public long getTotalDuration() { private final ScanMetrics mOpportunisticScan = new ScanMetrics(); @Override - public synchronized boolean getSnapshot(BluetoothMetrics snapshot) { + public synchronized boolean getSnapshot(BluetoothMetrics snapshot, @Nullable Context context) { checkNotNull(snapshot, "Null value passed to getSnapshot!"); snapshot.bleScanCount = mNonOpportunisticScan.count; snapshot.bleOpportunisticScanCount = mOpportunisticScan.count; diff --git a/metrics/src/main/java/com/facebook/battery/metrics/camera/CameraMetricsCollector.java b/metrics/src/main/java/com/facebook/battery/metrics/camera/CameraMetricsCollector.java index 3bde5bf..21d11f8 100644 --- a/metrics/src/main/java/com/facebook/battery/metrics/camera/CameraMetricsCollector.java +++ b/metrics/src/main/java/com/facebook/battery/metrics/camera/CameraMetricsCollector.java @@ -9,6 +9,7 @@ import static com.facebook.battery.metrics.core.Utilities.checkNotNull; +import android.content.Context; import android.hardware.Camera; import android.hardware.camera2.CameraCaptureSession; import android.hardware.camera2.CameraDevice; @@ -22,6 +23,8 @@ import com.facebook.battery.metrics.core.SystemMetricsLogger; import com.facebook.infer.annotation.ThreadSafe; +import javax.annotation.Nullable; + /** * CameraMetricsCollector internally maintains how long the camera was open and previewed; this is * simply a helper class to maintain state and can't automatically instrument camera usage. @@ -64,7 +67,7 @@ public class CameraMetricsCollector extends SystemMetricsCollector, T extends SystemMetricsCollector> T getMe * collectors are expected to report any errors they might encounter. * * @param snapshot snapshot to reuse + * @param context * @return whether _any_ underlying snapshot succeeded */ @Override @ThreadSafe(enableChecks = false) - public boolean getSnapshot(CompositeMetrics snapshot) { + public boolean getSnapshot(CompositeMetrics snapshot, @Nullable Context context) { checkNotNull(snapshot, "Null value passed to getSnapshot!"); boolean result = false; SimpleArrayMap, SystemMetrics> snapshotMetrics = @@ -96,7 +101,7 @@ public boolean getSnapshot(CompositeMetrics snapshot) { boolean snapshotResult = false; if (collector != null) { SystemMetrics metric = snapshot.getMetric(metricsClass); - snapshotResult = collector.getSnapshot(metric); + snapshotResult = collector.getSnapshot(metric, null); } snapshot.setIsValid(metricsClass, snapshotResult); result |= snapshotResult; diff --git a/metrics/src/main/java/com/facebook/battery/metrics/core/StatefulSystemMetricsCollector.java b/metrics/src/main/java/com/facebook/battery/metrics/core/StatefulSystemMetricsCollector.java index ac3552e..7d1fd72 100644 --- a/metrics/src/main/java/com/facebook/battery/metrics/core/StatefulSystemMetricsCollector.java +++ b/metrics/src/main/java/com/facebook/battery/metrics/core/StatefulSystemMetricsCollector.java @@ -29,7 +29,7 @@ public class StatefulSystemMetricsCollector< R extends SystemMetrics, S extends SystemMetricsCollector> { - private final S mCollector; + private S mCollector; private final R mDiff; private R mCurr; @@ -44,7 +44,7 @@ public class StatefulSystemMetricsCollector< public StatefulSystemMetricsCollector(S collector) { this( collector, collector.createMetrics(), collector.createMetrics(), collector.createMetrics()); - mIsValid &= collector.getSnapshot(mPrev); + mIsValid &= collector.getSnapshot(mPrev, null); } /** @@ -82,7 +82,7 @@ public R getLatestDiffAndReset() { /** Get a diff form the previous baseline. */ @Nullable public R getLatestDiff() { - mIsValid &= mCollector.getSnapshot(this.mCurr); + mIsValid &= mCollector.getSnapshot(this.mCurr, null); if (!mIsValid) { return null; } @@ -90,4 +90,8 @@ public R getLatestDiff() { mCurr.diff(mPrev, mDiff); return mDiff; } + + public void clearCollectors(){ + mCollector = null; + } } diff --git a/metrics/src/main/java/com/facebook/battery/metrics/core/SystemMetricsCollector.java b/metrics/src/main/java/com/facebook/battery/metrics/core/SystemMetricsCollector.java index f5e7d7a..900c042 100644 --- a/metrics/src/main/java/com/facebook/battery/metrics/core/SystemMetricsCollector.java +++ b/metrics/src/main/java/com/facebook/battery/metrics/core/SystemMetricsCollector.java @@ -7,6 +7,10 @@ package com.facebook.battery.metrics.core; +import android.content.Context; + +import javax.annotation.Nullable; + /** * Takes snapshots of a given metric. There are generally two types of metrics collectors - - those * that depend on an underlying api, such as the {@link @@ -22,10 +26,11 @@ public abstract class SystemMetricsCollector { * by the caller requesting getSnapshot. * * @param snapshot snapshot on which the data will be written + * @param context * @return true if the snapshot has been updated with valid data. * @throws IllegalArgumentException if snapshot == null. */ - public abstract boolean getSnapshot(T snapshot); + public abstract boolean getSnapshot(T snapshot, @Nullable Context context); /** * Creates an empty instance of the corresponding system metrics. diff --git a/metrics/src/main/java/com/facebook/battery/metrics/cpu/CpuFrequencyMetricsCollector.java b/metrics/src/main/java/com/facebook/battery/metrics/cpu/CpuFrequencyMetricsCollector.java index b6570fe..5a9dd14 100644 --- a/metrics/src/main/java/com/facebook/battery/metrics/cpu/CpuFrequencyMetricsCollector.java +++ b/metrics/src/main/java/com/facebook/battery/metrics/cpu/CpuFrequencyMetricsCollector.java @@ -9,6 +9,7 @@ import static com.facebook.battery.metrics.core.Utilities.checkNotNull; +import android.content.Context; import android.util.SparseIntArray; import androidx.annotation.VisibleForTesting; import com.facebook.battery.metrics.core.ProcFileReader; @@ -43,7 +44,7 @@ public class CpuFrequencyMetricsCollector extends SystemMetricsCollector { @@ -37,7 +41,7 @@ public class DiskMetricsCollector extends SystemMetricsCollector { @Override @ThreadSafe(enableChecks = false) - public synchronized boolean getSnapshot(DiskMetrics snapshot) { + public synchronized boolean getSnapshot(DiskMetrics snapshot, @Nullable Context context) { checkNotNull(snapshot, "Null value passed to getSnapshot!"); if (!mIsEnabled) { return false; diff --git a/metrics/src/main/java/com/facebook/battery/metrics/healthstats/HealthStatsMetricsCollector.java b/metrics/src/main/java/com/facebook/battery/metrics/healthstats/HealthStatsMetricsCollector.java index fae2ba6..df02114 100644 --- a/metrics/src/main/java/com/facebook/battery/metrics/healthstats/HealthStatsMetricsCollector.java +++ b/metrics/src/main/java/com/facebook/battery/metrics/healthstats/HealthStatsMetricsCollector.java @@ -15,6 +15,8 @@ import com.facebook.battery.metrics.core.SystemMetricsLogger; import com.facebook.infer.annotation.ThreadSafe; +import javax.annotation.Nullable; + @RequiresApi(api = Build.VERSION_CODES.N) @ThreadSafe(enableChecks = false) public class HealthStatsMetricsCollector extends SystemMetricsCollector { @@ -30,7 +32,7 @@ public HealthStatsMetricsCollector(Context context) { @Override @SuppressWarnings("CatchGeneralException") // because takeMyUidSnapshot wraps RemoteException in a RuntimeException - public boolean getSnapshot(HealthStatsMetrics snapshot) { + public boolean getSnapshot(HealthStatsMetrics snapshot, @Nullable Context context) { try { snapshot.set(mSystemHealthManager.takeMyUidSnapshot()); return true; diff --git a/metrics/src/main/java/com/facebook/battery/metrics/memory/MemoryMetricsCollector.java b/metrics/src/main/java/com/facebook/battery/metrics/memory/MemoryMetricsCollector.java index 98ebfa1..2535b33 100644 --- a/metrics/src/main/java/com/facebook/battery/metrics/memory/MemoryMetricsCollector.java +++ b/metrics/src/main/java/com/facebook/battery/metrics/memory/MemoryMetricsCollector.java @@ -9,6 +9,7 @@ import static com.facebook.battery.metrics.core.Utilities.checkNotNull; +import android.content.Context; import android.os.Debug; import androidx.annotation.GuardedBy; import com.facebook.battery.metrics.core.ProcFileReader; @@ -18,6 +19,8 @@ import com.facebook.infer.annotation.ThreadSafe; import java.util.concurrent.atomic.AtomicLong; +import javax.annotation.Nullable; + @Nullsafe(Nullsafe.Mode.LOCAL) @ThreadSafe public class MemoryMetricsCollector extends SystemMetricsCollector { @@ -35,7 +38,7 @@ public class MemoryMetricsCollector extends SystemMetricsCollector { @Override @ThreadSafe(enableChecks = false) - public boolean getSnapshot(TimeMetrics snapshot) { + public boolean getSnapshot(TimeMetrics snapshot, @Nullable Context context) { checkNotNull(snapshot, "Null value passed to getSnapshot!"); snapshot.realtimeMs = SystemClock.elapsedRealtime(); snapshot.uptimeMs = SystemClock.uptimeMillis(); diff --git a/metrics/src/main/java/com/facebook/battery/metrics/wakelock/WakeLockMetricsCollector.java b/metrics/src/main/java/com/facebook/battery/metrics/wakelock/WakeLockMetricsCollector.java index 8a95fbd..a1f5242 100644 --- a/metrics/src/main/java/com/facebook/battery/metrics/wakelock/WakeLockMetricsCollector.java +++ b/metrics/src/main/java/com/facebook/battery/metrics/wakelock/WakeLockMetricsCollector.java @@ -9,6 +9,7 @@ import static com.facebook.battery.metrics.core.Utilities.checkNotNull; +import android.content.Context; import android.os.PowerManager; import android.os.SystemClock; import androidx.annotation.GuardedBy; @@ -18,6 +19,8 @@ import java.util.ArrayList; import java.util.Iterator; import java.util.WeakHashMap; + +import javax.annotation.Nullable; import javax.annotation.concurrent.ThreadSafe; /** @@ -180,7 +183,7 @@ private synchronized void updateWakeLockCounts() { } @Override - public synchronized boolean getSnapshot(WakeLockMetrics snapshot) { + public synchronized boolean getSnapshot(WakeLockMetrics snapshot, @Nullable Context context) { checkNotNull(snapshot, "Null value passed to getSnapshot!"); if (!mIsEnabled) { return false; diff --git a/metrics/src/test/java/com/facebook/battery/metrics/appwakeup/AppWakeupMetricsCollectorTest.java b/metrics/src/test/java/com/facebook/battery/metrics/appwakeup/AppWakeupMetricsCollectorTest.java index d6ea6ac..55ada78 100644 --- a/metrics/src/test/java/com/facebook/battery/metrics/appwakeup/AppWakeupMetricsCollectorTest.java +++ b/metrics/src/test/java/com/facebook/battery/metrics/appwakeup/AppWakeupMetricsCollectorTest.java @@ -47,7 +47,7 @@ public void testGetSnapshot() { AppWakeupMetrics.WakeupReason.JOB_SCHEDULER, "key3"); ShadowSystemClock.setElapsedRealtime(4); mAppWakeupMetricsCollector.recordWakeupStart(AppWakeupMetrics.WakeupReason.ALARM, "key4"); - mAppWakeupMetricsCollector.getSnapshot(mAppWakeupMetrics); + mAppWakeupMetricsCollector.getSnapshot(mAppWakeupMetrics, null); assertThat(mAppWakeupMetrics.appWakeups.size()).isEqualTo(0); // Test with 2 wakeups closed and 2 open @@ -56,7 +56,7 @@ public void testGetSnapshot() { ShadowSystemClock.setElapsedRealtime(30); mAppWakeupMetricsCollector.recordWakeupEnd("key3"); ShadowSystemClock.setElapsedRealtime(35); - mAppWakeupMetricsCollector.getSnapshot(mAppWakeupMetrics); + mAppWakeupMetricsCollector.getSnapshot(mAppWakeupMetrics, null); assertThat(mAppWakeupMetrics.appWakeups.size()).isEqualTo(2); assertThat(mAppWakeupMetrics.appWakeups.get("key1")) .isEqualTo(new WakeupDetails(WakeupReason.ALARM, 1, 19)); @@ -71,7 +71,7 @@ public void testGetSnapshot() { ShadowSystemClock.setElapsedRealtime(57); mAppWakeupMetricsCollector.recordWakeupEnd("key2"); - mAppWakeupMetricsCollector.getSnapshot(mAppWakeupMetrics); + mAppWakeupMetricsCollector.getSnapshot(mAppWakeupMetrics, null); assertThat(mAppWakeupMetrics.appWakeups.size()).isEqualTo(3); assertThat(mAppWakeupMetrics.appWakeups.get("key1")) .isEqualTo(new WakeupDetails(WakeupReason.ALARM, 2, 28)); @@ -85,7 +85,7 @@ public void testGetSnapshot() { mAppWakeupMetricsCollector.recordWakeupStart(AppWakeupMetrics.WakeupReason.ALARM, "key4"); ShadowSystemClock.setElapsedRealtime(65); mAppWakeupMetricsCollector.recordWakeupEnd("key1"); - mAppWakeupMetricsCollector.getSnapshot(mAppWakeupMetrics); + mAppWakeupMetricsCollector.getSnapshot(mAppWakeupMetrics, null); assertThat(mAppWakeupMetrics.appWakeups.size()).isEqualTo(3); assertThat(mAppWakeupMetrics.appWakeups.get("key1")) .isEqualTo(new WakeupDetails(WakeupReason.ALARM, 2, 28)); diff --git a/metrics/src/test/java/com/facebook/battery/metrics/bluetooth/BluetoothMetricsCollectorTest.java b/metrics/src/test/java/com/facebook/battery/metrics/bluetooth/BluetoothMetricsCollectorTest.java index 568186b..4a3cfd3 100644 --- a/metrics/src/test/java/com/facebook/battery/metrics/bluetooth/BluetoothMetricsCollectorTest.java +++ b/metrics/src/test/java/com/facebook/battery/metrics/bluetooth/BluetoothMetricsCollectorTest.java @@ -39,7 +39,7 @@ public void testNonOpportunisticScan() { ScanCallback callback = mock(ScanCallback.class); // Zero at beginning - boolean isSuccess = mBluetoothMetricsCollector.getSnapshot(bluetoothMetrics); + boolean isSuccess = mBluetoothMetricsCollector.getSnapshot(bluetoothMetrics, null); assertThat(isSuccess).isTrue(); assertThat(bluetoothMetrics.bleScanCount).isEqualTo(0); assertThat(bluetoothMetrics.bleScanDurationMs).isEqualTo(0); @@ -50,7 +50,7 @@ public void testNonOpportunisticScan() { // Intermediate snapshot ShadowSystemClock.setUptimeMillis(21); - isSuccess = mBluetoothMetricsCollector.getSnapshot(bluetoothMetrics); + isSuccess = mBluetoothMetricsCollector.getSnapshot(bluetoothMetrics, null); assertThat(isSuccess).isTrue(); assertThat(bluetoothMetrics.bleScanCount).isEqualTo(1); assertThat(bluetoothMetrics.bleScanDurationMs).isEqualTo(20); @@ -60,14 +60,14 @@ public void testNonOpportunisticScan() { mBluetoothMetricsCollector.stopScan(callback); // Snapshot at stop - isSuccess = mBluetoothMetricsCollector.getSnapshot(bluetoothMetrics); + isSuccess = mBluetoothMetricsCollector.getSnapshot(bluetoothMetrics, null); assertThat(isSuccess).isTrue(); assertThat(bluetoothMetrics.bleScanCount).isEqualTo(1); assertThat(bluetoothMetrics.bleScanDurationMs).isEqualTo(50); // Snapshot after stop ShadowSystemClock.setUptimeMillis(61); - isSuccess = mBluetoothMetricsCollector.getSnapshot(bluetoothMetrics); + isSuccess = mBluetoothMetricsCollector.getSnapshot(bluetoothMetrics, null); assertThat(isSuccess).isTrue(); assertThat(bluetoothMetrics.bleScanCount).isEqualTo(1); assertThat(bluetoothMetrics.bleScanDurationMs).isEqualTo(50); @@ -82,7 +82,7 @@ public void testOpportunisticScan() { ScanCallback callback = mock(ScanCallback.class); // Zero at beginning - boolean isSuccess = mBluetoothMetricsCollector.getSnapshot(bluetoothMetrics); + boolean isSuccess = mBluetoothMetricsCollector.getSnapshot(bluetoothMetrics, null); assertThat(isSuccess).isTrue(); assertThat(bluetoothMetrics.bleOpportunisticScanCount).isEqualTo(0); assertThat(bluetoothMetrics.bleOpportunisticScanDurationMs).isEqualTo(0); @@ -93,7 +93,7 @@ public void testOpportunisticScan() { // Intermediate snapshot ShadowSystemClock.setUptimeMillis(21); - isSuccess = mBluetoothMetricsCollector.getSnapshot(bluetoothMetrics); + isSuccess = mBluetoothMetricsCollector.getSnapshot(bluetoothMetrics, null); assertThat(isSuccess).isTrue(); assertThat(bluetoothMetrics.bleOpportunisticScanCount).isEqualTo(1); assertThat(bluetoothMetrics.bleOpportunisticScanDurationMs).isEqualTo(20); @@ -103,14 +103,14 @@ public void testOpportunisticScan() { mBluetoothMetricsCollector.stopScan(callback); // Snapshot at stop - isSuccess = mBluetoothMetricsCollector.getSnapshot(bluetoothMetrics); + isSuccess = mBluetoothMetricsCollector.getSnapshot(bluetoothMetrics, null); assertThat(isSuccess).isTrue(); assertThat(bluetoothMetrics.bleOpportunisticScanCount).isEqualTo(1); assertThat(bluetoothMetrics.bleOpportunisticScanDurationMs).isEqualTo(50); // Snapshot after stop ShadowSystemClock.setUptimeMillis(61); - isSuccess = mBluetoothMetricsCollector.getSnapshot(bluetoothMetrics); + isSuccess = mBluetoothMetricsCollector.getSnapshot(bluetoothMetrics, null); assertThat(isSuccess).isTrue(); assertThat(bluetoothMetrics.bleOpportunisticScanCount).isEqualTo(1); assertThat(bluetoothMetrics.bleOpportunisticScanDurationMs).isEqualTo(50); @@ -150,7 +150,7 @@ public void testOverlappingScans() { mBluetoothMetricsCollector.stopScan(callbackC); // Intermediate snapshot - mBluetoothMetricsCollector.getSnapshot(bluetoothMetrics); + mBluetoothMetricsCollector.getSnapshot(bluetoothMetrics, null); assertThat(bluetoothMetrics.bleScanCount).isEqualTo(2); assertThat(bluetoothMetrics.bleScanDurationMs).isEqualTo(8 - 2); assertThat(bluetoothMetrics.bleOpportunisticScanCount).isEqualTo(1); @@ -164,7 +164,7 @@ public void testOverlappingScans() { // Final snapshot ShadowSystemClock.setUptimeMillis(64); - mBluetoothMetricsCollector.getSnapshot(bluetoothMetrics); + mBluetoothMetricsCollector.getSnapshot(bluetoothMetrics, null); assertThat(bluetoothMetrics.bleScanCount).isEqualTo(2); assertThat(bluetoothMetrics.bleScanDurationMs).isEqualTo(16 - 2); assertThat(bluetoothMetrics.bleOpportunisticScanCount).isEqualTo(1); @@ -192,7 +192,7 @@ public void testDuplicateStarts() { ShadowSystemClock.setUptimeMillis(128); BluetoothMetrics bluetoothMetrics = new BluetoothMetrics(); - mBluetoothMetricsCollector.getSnapshot(bluetoothMetrics); + mBluetoothMetricsCollector.getSnapshot(bluetoothMetrics, null); assertThat(bluetoothMetrics.bleScanCount).isEqualTo(1); assertThat(bluetoothMetrics.bleScanDurationMs).isEqualTo(32 - 2); assertThat(bluetoothMetrics.bleOpportunisticScanCount).isEqualTo(0); diff --git a/metrics/src/test/java/com/facebook/battery/metrics/camera/CameraMetricsCollectorTest.java b/metrics/src/test/java/com/facebook/battery/metrics/camera/CameraMetricsCollectorTest.java index 7776a02..ed90700 100644 --- a/metrics/src/test/java/com/facebook/battery/metrics/camera/CameraMetricsCollectorTest.java +++ b/metrics/src/test/java/com/facebook/battery/metrics/camera/CameraMetricsCollectorTest.java @@ -37,10 +37,10 @@ public void testDisabling() { CameraMetricsCollector collector = new CameraMetricsCollector(); collector.recordCameraOpen(testCamera); - assertThat(collector.getSnapshot(metrics)).isTrue(); + assertThat(collector.getSnapshot(metrics, null)).isTrue(); collector.disable(); - assertThat(collector.getSnapshot(metrics)).isFalse(); + assertThat(collector.getSnapshot(metrics, null)).isFalse(); // Sanity check no exceptions after disabling collector.recordPreviewStop(testCamera); @@ -59,7 +59,7 @@ public void testSimpleOpenSnapshot() { collector.recordCameraClose(testCamera); CameraMetrics snapshot = new CameraMetrics(); - assertThat(collector.getSnapshot(snapshot)).isTrue(); + assertThat(collector.getSnapshot(snapshot, null)).isTrue(); assertThat(snapshot.cameraOpenTimeMs).isEqualTo(800); assertThat(snapshot.cameraPreviewTimeMs).isEqualTo(0); } @@ -75,7 +75,7 @@ public void testSimplePreviewSnapshot() { collector.recordPreviewStop(testCamera); CameraMetrics snapshot = new CameraMetrics(); - assertThat(collector.getSnapshot(snapshot)).isTrue(); + assertThat(collector.getSnapshot(snapshot, null)).isTrue(); assertThat(snapshot.cameraOpenTimeMs).isEqualTo(0); assertThat(snapshot.cameraPreviewTimeMs).isEqualTo(300); } @@ -89,7 +89,7 @@ public void testIncompleteOpenSnapshot() { ShadowSystemClock.setUptimeMillis(1000); CameraMetrics snapshot = new CameraMetrics(); - assertThat(collector.getSnapshot(snapshot)).isTrue(); + assertThat(collector.getSnapshot(snapshot, null)).isTrue(); assertThat(snapshot.cameraOpenTimeMs).isEqualTo(500); assertThat(snapshot.cameraPreviewTimeMs).isEqualTo(0); } @@ -103,7 +103,7 @@ public void testIncompletePreviewSnapshot() { ShadowSystemClock.setUptimeMillis(1000); CameraMetrics snapshot = new CameraMetrics(); - assertThat(collector.getSnapshot(snapshot)).isTrue(); + assertThat(collector.getSnapshot(snapshot, null)).isTrue(); assertThat(snapshot.cameraOpenTimeMs).isEqualTo(0); assertThat(snapshot.cameraPreviewTimeMs).isEqualTo(300); } @@ -128,7 +128,7 @@ public void testCameraOpenExceptionWithRelease() { collector.recordCameraError(testCamera); CameraMetrics snapshot = new CameraMetrics(); - assertThat(collector.getSnapshot(snapshot)).isTrue(); + assertThat(collector.getSnapshot(snapshot, null)).isTrue(); assertThat(snapshot.cameraOpenTimeMs).isEqualTo(800); assertThat(snapshot.cameraPreviewTimeMs).isEqualTo(0); } @@ -158,7 +158,7 @@ public void testCameraOpenExceptionWithoutRelease() { collector.recordPreviewStop(testCamera); CameraMetrics snapshot = new CameraMetrics(); - assertThat(collector.getSnapshot(snapshot)).isTrue(); + assertThat(collector.getSnapshot(snapshot, null)).isTrue(); assertThat(snapshot.cameraOpenTimeMs).isEqualTo(800); assertThat(snapshot.cameraPreviewTimeMs).isEqualTo(300); } @@ -184,7 +184,7 @@ public void wtf(String Tag, String message, @Nullable Throwable cause) { collector.recordCameraClose(testCamera); CameraMetrics snapshot = new CameraMetrics(); - assertThat(collector.getSnapshot(snapshot)).isTrue(); + assertThat(collector.getSnapshot(snapshot, null)).isTrue(); assertThat(snapshot.cameraOpenTimeMs).isEqualTo(0); assertThat(snapshot.cameraPreviewTimeMs).isEqualTo(0); } diff --git a/metrics/src/test/java/com/facebook/battery/metrics/composite/CompositeMetricsCollectorTest.java b/metrics/src/test/java/com/facebook/battery/metrics/composite/CompositeMetricsCollectorTest.java index 6aae5cf..5afb230 100644 --- a/metrics/src/test/java/com/facebook/battery/metrics/composite/CompositeMetricsCollectorTest.java +++ b/metrics/src/test/java/com/facebook/battery/metrics/composite/CompositeMetricsCollectorTest.java @@ -9,6 +9,8 @@ import static org.assertj.core.api.Java6Assertions.assertThat; +import android.content.Context; + import androidx.annotation.Nullable; import com.facebook.battery.metrics.core.SystemMetrics; import com.facebook.battery.metrics.core.SystemMetricsCollector; @@ -45,7 +47,7 @@ public void setUp() throws Exception { @Test public void testNullSnapshot() { mExpectedException.expect(IllegalArgumentException.class); - mCollector.getSnapshot(null); + mCollector.getSnapshot(null, null); } @Test @@ -55,7 +57,7 @@ public void allSnapshotsSucceed() throws Exception { mBCollector.succeeds = true; mBCollector.currentValue = 120; - assertThat(mCollector.getSnapshot(mMetrics)).isTrue(); + assertThat(mCollector.getSnapshot(mMetrics, null)).isTrue(); assertThat(mMetrics.getMetrics().size()).isEqualTo(2); assertThat(mMetrics.getMetric(A.class).value).isEqualTo(100); assertThat(mMetrics.isValid(A.class)).isEqualTo(true); @@ -69,7 +71,7 @@ public void partialFailures() throws Exception { mACollector.currentValue = 100; mBCollector.succeeds = false; - assertThat(mCollector.getSnapshot(mMetrics)).isTrue(); + assertThat(mCollector.getSnapshot(mMetrics, null)).isTrue(); assertThat(mMetrics.getMetrics().size()).isEqualTo(2); assertThat(mMetrics.getMetric(A.class).value).isEqualTo(100); assertThat(mMetrics.isValid(A.class)).isEqualTo(true); @@ -81,7 +83,7 @@ public void allSnapshotsFail() throws Exception { mACollector.succeeds = false; mBCollector.succeeds = false; - assertThat(mCollector.getSnapshot(mMetrics)).isFalse(); + assertThat(mCollector.getSnapshot(mMetrics, null)).isFalse(); assertThat(mMetrics.getMetrics().size()).isEqualTo(2); assertThat(mMetrics.isValid(A.class)).isEqualTo(false); assertThat(mMetrics.isValid(B.class)).isEqualTo(false); @@ -92,9 +94,9 @@ public void subsetSnapshots() throws Exception { mACollector.currentValue = 100; mACollector.succeeds = true; CompositeMetrics m = new CompositeMetrics().putMetric(A.class, new A()); - mCollector.getSnapshot(m); + mCollector.getSnapshot(m, null); - assertThat(mCollector.getSnapshot(m)).isTrue(); + assertThat(mCollector.getSnapshot(m, null)).isTrue(); assertThat(m.getMetrics().size()).isEqualTo(1); assertThat(m.getMetric(A.class).value).isEqualTo(100); assertThat(m.isValid(A.class)).isEqualTo(true); @@ -108,9 +110,9 @@ public void supersetSnapshots() throws Exception { mACollector.succeeds = true; CompositeMetrics m = new CompositeMetrics().putMetric(A.class, new A()).putMetric(C.class, new C()); - mCollector.getSnapshot(m); + mCollector.getSnapshot(m, null); - assertThat(mCollector.getSnapshot(m)).isTrue(); + assertThat(mCollector.getSnapshot(m, null)).isTrue(); assertThat(m.getMetrics().size()).isEqualTo(2); assertThat(m.getMetric(A.class).value).isEqualTo(100); assertThat(m.isValid(A.class)).isEqualTo(true); @@ -171,7 +173,7 @@ class ACollector extends SystemMetricsCollector { boolean succeeds = true; @Override - public boolean getSnapshot(A snapshot) { + public boolean getSnapshot(A snapshot, @javax.annotation.Nullable Context context) { snapshot.value = currentValue; return succeeds; } @@ -188,7 +190,7 @@ class BCollector extends SystemMetricsCollector { boolean succeeds = true; @Override - public boolean getSnapshot(B snapshot) { + public boolean getSnapshot(B snapshot, @javax.annotation.Nullable Context context) { snapshot.value = currentValue; return succeeds; } diff --git a/metrics/src/test/java/com/facebook/battery/metrics/core/StatefulSystemMetricsCollectorTest.java b/metrics/src/test/java/com/facebook/battery/metrics/core/StatefulSystemMetricsCollectorTest.java index 32a6262..1667f84 100644 --- a/metrics/src/test/java/com/facebook/battery/metrics/core/StatefulSystemMetricsCollectorTest.java +++ b/metrics/src/test/java/com/facebook/battery/metrics/core/StatefulSystemMetricsCollectorTest.java @@ -9,10 +9,14 @@ import static org.assertj.core.api.Java6Assertions.assertThat; +import android.content.Context; + import org.junit.Test; import org.junit.runner.RunWith; import org.robolectric.RobolectricTestRunner; +import javax.annotation.Nullable; + /** Sanity check swapping in {@link StatefulSystemMetricsCollector} */ @RunWith(RobolectricTestRunner.class) public class StatefulSystemMetricsCollectorTest { @@ -94,7 +98,7 @@ class DummyMetricCollector extends SystemMetricsCollector { int currentValue; @Override - public boolean getSnapshot(DummyMetric snapshot) { + public boolean getSnapshot(DummyMetric snapshot, @Nullable Context context) { snapshot.value = currentValue; return true; } diff --git a/metrics/src/test/java/com/facebook/battery/metrics/core/SystemMetricsCollectorTest.java b/metrics/src/test/java/com/facebook/battery/metrics/core/SystemMetricsCollectorTest.java index cd6d9b2..2027421 100644 --- a/metrics/src/test/java/com/facebook/battery/metrics/core/SystemMetricsCollectorTest.java +++ b/metrics/src/test/java/com/facebook/battery/metrics/core/SystemMetricsCollectorTest.java @@ -20,7 +20,7 @@ public abstract class SystemMetricsCollectorTest< public void testNullSnapshot() throws Exception { mExpectedException.expect(IllegalArgumentException.class); T instance = getClazz().newInstance(); - instance.getSnapshot(null); + instance.getSnapshot(null, null); } protected abstract Class getClazz(); diff --git a/metrics/src/test/java/com/facebook/battery/metrics/cpu/CpuFrequencyMetricsCollectorTest.java b/metrics/src/test/java/com/facebook/battery/metrics/cpu/CpuFrequencyMetricsCollectorTest.java index 52c7806..0b4ced9 100644 --- a/metrics/src/test/java/com/facebook/battery/metrics/cpu/CpuFrequencyMetricsCollectorTest.java +++ b/metrics/src/test/java/com/facebook/battery/metrics/cpu/CpuFrequencyMetricsCollectorTest.java @@ -48,7 +48,7 @@ public void testValidCores() throws Exception { }); CpuFrequencyMetrics metrics = collector.createMetrics(); - assertThat(collector.getSnapshot(metrics)).isTrue(); + assertThat(collector.getSnapshot(metrics, null)).isTrue(); assertThat(metrics.timeInStateS.length).isEqualTo(4); assertThat(metrics.timeInStateS[0].size()).isEqualTo(2); @@ -76,7 +76,7 @@ public void testPartialMissingCores() throws Exception { }); CpuFrequencyMetrics metrics = collector.createMetrics(); - assertThat(collector.getSnapshot(metrics)).isTrue(); + assertThat(collector.getSnapshot(metrics, null)).isTrue(); assertThat(metrics.timeInStateS.length).isEqualTo(4); assertThat(metrics.timeInStateS[0].size()).isEqualTo(1); @@ -97,7 +97,7 @@ public void testInvalidContents() throws Exception { }); CpuFrequencyMetrics metrics = collector.createMetrics(); - assertThat(collector.getSnapshot(metrics)).isTrue(); + assertThat(collector.getSnapshot(metrics, null)).isTrue(); assertThat(metrics.timeInStateS.length).isEqualTo(4); assertThat(metrics.timeInStateS[0].size()).isEqualTo(0); @@ -110,7 +110,7 @@ public void testAllMissingCores() throws Exception { new String[] {"everything", "is", "horribly", "broken"}); CpuFrequencyMetrics metrics = collector.createMetrics(); - assertThat(collector.getSnapshot(metrics)).isFalse(); + assertThat(collector.getSnapshot(metrics, null)).isFalse(); } private String createFile(String contents) throws IOException { diff --git a/metrics/src/test/java/com/facebook/battery/metrics/cpu/CpuMetricsCollectorTest.java b/metrics/src/test/java/com/facebook/battery/metrics/cpu/CpuMetricsCollectorTest.java index a42fe95..9b02559 100644 --- a/metrics/src/test/java/com/facebook/battery/metrics/cpu/CpuMetricsCollectorTest.java +++ b/metrics/src/test/java/com/facebook/battery/metrics/cpu/CpuMetricsCollectorTest.java @@ -43,7 +43,7 @@ public void testBrokenFile() throws Exception { new TestableCpuMetricsCollector().setPath(createFile("I am a weird android manufacturer")); CpuMetrics snapshot = new CpuMetrics(); - assertThat(collector.getSnapshot(snapshot)).isFalse(); + assertThat(collector.getSnapshot(snapshot, null)).isFalse(); } @Test @@ -56,7 +56,7 @@ public void testNegativeFields() throws Exception { new TestableCpuMetricsCollector().setPath(createFile(testStringBuilder.toString())); CpuMetrics snapshot = new CpuMetrics(); - assertThat(collector.getSnapshot(snapshot)).isFalse(); + assertThat(collector.getSnapshot(snapshot, null)).isFalse(); } @Test @@ -71,7 +71,7 @@ public void testErrorOnDecreasing() throws Exception { String path = createFile(initialEntry.toString()); TestableCpuMetricsCollector collector = new TestableCpuMetricsCollector().setPath(path); CpuMetrics snapshot = new CpuMetrics(); - assertThat(collector.getSnapshot(snapshot)).isTrue(); + assertThat(collector.getSnapshot(snapshot, null)).isTrue(); verify(logger, never()).wtf(anyString(), anyString(), (Throwable) any()); StringBuilder secondEntry = new StringBuilder(); @@ -79,7 +79,7 @@ public void testErrorOnDecreasing() throws Exception { secondEntry.append(i * 1000).append(' '); } overwriteFile(new File(path), secondEntry.toString()); - assertThat(collector.getSnapshot(snapshot)).isFalse(); + assertThat(collector.getSnapshot(snapshot, null)).isFalse(); verify(logger, times(1)).wtf(anyString(), anyString(), (Throwable) any()); } @@ -91,7 +91,7 @@ public void testRealProcfile() throws Exception { new TestableCpuMetricsCollector().setPath(createFile(stat)); CpuMetrics snapshot = new CpuMetrics(); - assertThat(collector.getSnapshot(snapshot)).isTrue(); + assertThat(collector.getSnapshot(snapshot, null)).isTrue(); assertThat(snapshot.userTimeS).isEqualTo(9852.0 / 100); assertThat(snapshot.systemTimeS).isEqualTo(889.0 / 100); @@ -109,7 +109,7 @@ public void testSaneProcFile() throws Exception { new TestableCpuMetricsCollector().setPath(createFile(testStringBuilder.toString())); CpuMetrics snapshot = new CpuMetrics(); - assertThat(collector.getSnapshot(snapshot)).isTrue(); + assertThat(collector.getSnapshot(snapshot, null)).isTrue(); assertThat(snapshot.userTimeS).isEqualTo(13); assertThat(snapshot.systemTimeS).isEqualTo(14); @@ -122,7 +122,7 @@ public void testUnreadableProcFile() throws Exception { TestableCpuMetricsCollector collector = new TestableCpuMetricsCollector().setPath(""); CpuMetrics snapshot = new CpuMetrics(); - assertThat(collector.getSnapshot(snapshot)).isFalse(); + assertThat(collector.getSnapshot(snapshot, null)).isFalse(); } private String createFile(String contents) throws IOException { diff --git a/metrics/src/test/java/com/facebook/battery/metrics/devicebattery/DeviceBatteryMetricsCollectorTest.java b/metrics/src/test/java/com/facebook/battery/metrics/devicebattery/DeviceBatteryMetricsCollectorTest.java index 1c4e68e..2f36c1b 100644 --- a/metrics/src/test/java/com/facebook/battery/metrics/devicebattery/DeviceBatteryMetricsCollectorTest.java +++ b/metrics/src/test/java/com/facebook/battery/metrics/devicebattery/DeviceBatteryMetricsCollectorTest.java @@ -19,7 +19,7 @@ import android.content.IntentFilter; import android.os.BatteryManager; import com.facebook.battery.metrics.core.ShadowSystemClock; -import java.util.List; + import org.junit.Before; import org.junit.Rule; import org.junit.Test; @@ -29,6 +29,8 @@ import org.mockito.Matchers; import org.robolectric.RobolectricTestRunner; +import java.util.List; + @RunWith(RobolectricTestRunner.class) @org.robolectric.annotation.Config(shadows = {ShadowSystemClock.class}) public class DeviceBatteryMetricsCollectorTest { @@ -51,7 +53,7 @@ public void testInitialSnapshot() { DeviceBatteryMetrics metrics = new DeviceBatteryMetrics(); DeviceBatteryMetricsCollector collector = new DeviceBatteryMetricsCollector(mContext); ShadowSystemClock.setElapsedRealtime(10000); - collector.getSnapshot(metrics); + collector.getSnapshot(metrics, null); verifySnapshot(metrics, 20, 0, 5000); } @@ -60,7 +62,7 @@ public void testInitialSnapshot() { public void testEmptyBatteryIntent() { DeviceBatteryMetrics metrics = new DeviceBatteryMetrics(); DeviceBatteryMetricsCollector collector = new DeviceBatteryMetricsCollector(mContext); - collector.getSnapshot(metrics); + collector.getSnapshot(metrics, null); } @Test @@ -70,7 +72,7 @@ public void testNullSnapshot() { .thenReturn(createBatteryIntent(BatteryManager.BATTERY_STATUS_CHARGING, 20, 100)); DeviceBatteryMetricsCollector collector = new DeviceBatteryMetricsCollector(mContext); mExpectedException.expect(IllegalArgumentException.class); - collector.getSnapshot(null); + collector.getSnapshot(null, null); } @Test @@ -80,13 +82,13 @@ public void testExceptionInRegisterReceiver() { .thenThrow(new SecurityException("Testing exception")); DeviceBatteryMetrics metrics = new DeviceBatteryMetrics(); DeviceBatteryMetricsCollector collector = new DeviceBatteryMetricsCollector(mContext); - collector.getSnapshot(metrics); + collector.getSnapshot(metrics, null); assertThat(metrics.batteryLevelPct).isEqualTo(DeviceBatteryMetricsCollector.UNKNOWN_LEVEL); } @Test public void testSnapshotAfterValidBroadcasts() { - // Set up the collector + //Set up the collector ShadowSystemClock.setElapsedRealtime(5000); when(mContext.registerReceiver( Matchers.isNull(BroadcastReceiver.class), Matchers.any(IntentFilter.class))) @@ -118,7 +120,7 @@ public void testSnapshotAfterValidBroadcasts() { when(mContext.registerReceiver( Matchers.isNull(BroadcastReceiver.class), Matchers.any(IntentFilter.class))) .thenReturn(createBatteryIntent(BatteryManager.BATTERY_STATUS_CHARGING, 20, 100)); - collector.getSnapshot(metrics); + collector.getSnapshot(metrics, null); verifySnapshot(metrics, 20, 12000, 11000); // Power Connected and test getSnapshot @@ -128,7 +130,7 @@ public void testSnapshotAfterValidBroadcasts() { when(mContext.registerReceiver( Matchers.isNull(BroadcastReceiver.class), Matchers.any(IntentFilter.class))) .thenReturn(createBatteryIntent(BatteryManager.BATTERY_STATUS_CHARGING, 30, 100)); - collector.getSnapshot(metrics); + collector.getSnapshot(metrics, null); verifySnapshot(metrics, 30, 14000, 23000); // PowerConnected and testGetSnapshot (Check for two consecutive CONNECTED intents) @@ -138,7 +140,7 @@ public void testSnapshotAfterValidBroadcasts() { when(mContext.registerReceiver( Matchers.isNull(BroadcastReceiver.class), Matchers.any(IntentFilter.class))) .thenReturn(createBatteryIntent(BatteryManager.BATTERY_STATUS_CHARGING, 40, 100)); - collector.getSnapshot(metrics); + collector.getSnapshot(metrics, null); verifySnapshot(metrics, 40, 14000, 32000); // Test for 2 consecutive powerdisconnected intents @@ -150,7 +152,7 @@ public void testSnapshotAfterValidBroadcasts() { when(mContext.registerReceiver( Matchers.isNull(BroadcastReceiver.class), Matchers.any(IntentFilter.class))) .thenReturn(createBatteryIntent(BatteryManager.BATTERY_STATUS_CHARGING, 60, 100)); - collector.getSnapshot(metrics); + collector.getSnapshot(metrics, null); verifySnapshot(metrics, 60, 24000, 36000); } diff --git a/metrics/src/test/java/com/facebook/battery/metrics/disk/DiskMetricsCollectorTest.java b/metrics/src/test/java/com/facebook/battery/metrics/disk/DiskMetricsCollectorTest.java index cefa316..adde096 100644 --- a/metrics/src/test/java/com/facebook/battery/metrics/disk/DiskMetricsCollectorTest.java +++ b/metrics/src/test/java/com/facebook/battery/metrics/disk/DiskMetricsCollectorTest.java @@ -37,14 +37,14 @@ public void testBrokenFile() throws Exception { .setPath(createFile("I am a weird android"), createFile("manufacturer")); DiskMetrics snapshot = new DiskMetrics(); - assertThat(collector.getSnapshot(snapshot)).isFalse(); + assertThat(collector.getSnapshot(snapshot, null)).isFalse(); } @Test public void testDefaultDisabled() throws Exception { DiskMetricsCollector collector = new DiskMetricsCollector(); DiskMetrics snapshot = new DiskMetrics(); - assertThat(collector.getSnapshot(snapshot)).isFalse(); + assertThat(collector.getSnapshot(snapshot, null)).isFalse(); assertThat(snapshot.rcharBytes).isEqualTo(0); assertThat(snapshot.wcharBytes).isEqualTo(0); } @@ -67,7 +67,7 @@ public void testRealProcfile() throws Exception { new DiskMetricsCollectorWithProcFile().setPath(createFile(io), createFile(stat)); DiskMetrics snapshot = new DiskMetrics(); - assertThat(collector.getSnapshot(snapshot)).isTrue(); + assertThat(collector.getSnapshot(snapshot, null)).isTrue(); assertThat(snapshot.rcharBytes).isEqualTo(100); assertThat(snapshot.wcharBytes).isEqualTo(101); @@ -98,9 +98,9 @@ public void testRealProcfileRepeat() throws Exception { new DiskMetricsCollectorWithProcFile().setPath(createFile(io), createFile(stat)); DiskMetrics snapshot = new DiskMetrics(); - assertThat(collector.getSnapshot(snapshot)).isTrue(); - assertThat(collector.getSnapshot(snapshot)).isTrue(); - assertThat(collector.getSnapshot(snapshot)).isTrue(); + assertThat(collector.getSnapshot(snapshot, null)).isTrue(); + assertThat(collector.getSnapshot(snapshot, null)).isTrue(); + assertThat(collector.getSnapshot(snapshot, null)).isTrue(); assertThat(snapshot.rcharBytes).isEqualTo(100); assertThat(snapshot.wcharBytes).isEqualTo(101); @@ -131,7 +131,7 @@ public void testRealProcfileWithNegative() throws Exception { new DiskMetricsCollectorWithProcFile().setPath(createFile(io), createFile(stat)); DiskMetrics snapshot = new DiskMetrics(); - assertThat(collector.getSnapshot(snapshot)).isTrue(); + assertThat(collector.getSnapshot(snapshot, null)).isTrue(); assertThat(snapshot.rcharBytes).isEqualTo(100); assertThat(snapshot.wcharBytes).isEqualTo(101); diff --git a/metrics/src/test/java/com/facebook/battery/metrics/memory/MemoryMetricsCollectorTest.java b/metrics/src/test/java/com/facebook/battery/metrics/memory/MemoryMetricsCollectorTest.java index e1c8f26..7dc4fc0 100644 --- a/metrics/src/test/java/com/facebook/battery/metrics/memory/MemoryMetricsCollectorTest.java +++ b/metrics/src/test/java/com/facebook/battery/metrics/memory/MemoryMetricsCollectorTest.java @@ -39,8 +39,8 @@ public void testBrokenFile() throws Exception { .setPath(createFile("I am a weird android manufacturer")); MemoryMetrics snapshot = new MemoryMetrics(); - collector.getSnapshot(snapshot); - assertThat(collector.getSnapshot(snapshot)).isTrue(); + collector.getSnapshot(snapshot, null); + assertThat(collector.getSnapshot(snapshot, null)).isTrue(); assertThat(snapshot.vmSizeKb).isEqualTo(-1); assertThat(snapshot.vmRssKb).isEqualTo(-1); @@ -53,7 +53,7 @@ public void testVirtualMemory() throws Exception { new MemoryMetricsCollectorWithProcFile().setPath(createFile(statm)); MemoryMetrics snapshot = new MemoryMetrics(); - assertThat(collector.getSnapshot(snapshot)).isTrue(); + assertThat(collector.getSnapshot(snapshot, null)).isTrue(); assertThat(snapshot.vmSizeKb).isEqualTo(16); assertThat(snapshot.vmRssKb).isEqualTo(8); @@ -66,9 +66,9 @@ public void testDefaultDisabled() { MemoryMetrics snapshot = new MemoryMetrics(); MemoryMetricsCollector collector = new MemoryMetricsCollector(); - collector.getSnapshot(snapshot); + collector.getSnapshot(snapshot, null); - assertThat(collector.getSnapshot(snapshot)).isFalse(); + assertThat(collector.getSnapshot(snapshot, null)).isFalse(); assertThat(snapshot.nativeHeapSizeKb).isEqualTo(0); assertThat(snapshot.nativeHeapAllocatedKb).isEqualTo(0); } @@ -81,7 +81,7 @@ public void testNativeHeap() { MemoryMetrics snapshot = new MemoryMetrics(); MemoryMetricsCollector collector = new MemoryMetricsCollector(); collector.enable(); - collector.getSnapshot(snapshot); + collector.getSnapshot(snapshot, null); assertThat(snapshot.nativeHeapSizeKb).isEqualTo(4); assertThat(snapshot.nativeHeapAllocatedKb).isEqualTo(3); @@ -95,8 +95,8 @@ public void testSnapshotSequenceNumber() { MemoryMetrics first = new MemoryMetrics(); MemoryMetrics second = new MemoryMetrics(); - collector.getSnapshot(first); - collector.getSnapshot(second); + collector.getSnapshot(first, null); + collector.getSnapshot(second, null); assertThat(first.sequenceNumber).isEqualTo(1); assertThat(second.sequenceNumber).isEqualTo(2); @@ -113,11 +113,11 @@ public void testNativeHeapSumFirstWithSecond() { ShadowDebug.setNativeHeapSize(4 * 1024); ShadowDebug.setNativeHeapAllocatedSize(3 * 1024); - collector.getSnapshot(first); + collector.getSnapshot(first, null); ShadowDebug.setNativeHeapSize(8 * 1024); ShadowDebug.setNativeHeapAllocatedSize(6 * 1024); - collector.getSnapshot(second); + collector.getSnapshot(second, null); first.sum(second, output); assertThat(output.nativeHeapSizeKb).isEqualTo(8); @@ -135,11 +135,11 @@ public void testNativeHeapSumSecondWithFirst() { ShadowDebug.setNativeHeapSize(4 * 1024); ShadowDebug.setNativeHeapAllocatedSize(3 * 1024); - collector.getSnapshot(first); + collector.getSnapshot(first, null); ShadowDebug.setNativeHeapSize(8 * 1024); ShadowDebug.setNativeHeapAllocatedSize(6 * 1024); - collector.getSnapshot(second); + collector.getSnapshot(second, null); second.sum(first, output); assertThat(output.nativeHeapSizeKb).isEqualTo(8); @@ -155,7 +155,7 @@ public void testJavaHeap() { collector.enable(); MemoryMetrics snapshot = new MemoryMetrics(); - collector.getSnapshot(snapshot); + collector.getSnapshot(snapshot, null); assertThat(snapshot.javaHeapMaxSizeKb).isEqualTo(8); assertThat(snapshot.nativeHeapAllocatedKb).isEqualTo(6); } diff --git a/metrics/src/test/java/com/facebook/battery/metrics/memory/MemoryMetricsTest.java b/metrics/src/test/java/com/facebook/battery/metrics/memory/MemoryMetricsTest.java index af1bd65..9603e6c 100644 --- a/metrics/src/test/java/com/facebook/battery/metrics/memory/MemoryMetricsTest.java +++ b/metrics/src/test/java/com/facebook/battery/metrics/memory/MemoryMetricsTest.java @@ -39,8 +39,8 @@ public void testSum() throws Exception { MemoryMetricsCollector collector = new MemoryMetricsCollector(); collector.enable(); - collector.getSnapshot(a); - collector.getSnapshot(b); + collector.getSnapshot(a, null); + collector.getSnapshot(b, null); sum = b.sum(a, sum); assertThat(sum.sequenceNumber).isEqualTo(b.sequenceNumber); @@ -71,8 +71,8 @@ public void testDiff() throws Exception { MemoryMetricsCollector collector = new MemoryMetricsCollector(); collector.enable(); - collector.getSnapshot(a); - collector.getSnapshot(b); + collector.getSnapshot(a, null); + collector.getSnapshot(b, null); sum = b.diff(a, sum); assertThat(sum.sequenceNumber).isEqualTo(b.sequenceNumber); diff --git a/metrics/src/test/java/com/facebook/battery/metrics/network/NetworkMetricsCollectorTest.java b/metrics/src/test/java/com/facebook/battery/metrics/network/NetworkMetricsCollectorTest.java index 5a522ff..cf64b72 100644 --- a/metrics/src/test/java/com/facebook/battery/metrics/network/NetworkMetricsCollectorTest.java +++ b/metrics/src/test/java/com/facebook/battery/metrics/network/NetworkMetricsCollectorTest.java @@ -53,7 +53,7 @@ public void testWithoutBgDetection() { mBytesCollector.yieldTotalBytes(bytes); NetworkMetrics metrics = mMetricsCollector.createMetrics(); - boolean hasMetrics = mMetricsCollector.getSnapshot(metrics); + boolean hasMetrics = mMetricsCollector.getSnapshot(metrics, null); assertThat(hasMetrics).isTrue(); NetworkMetrics expected = new NetworkMetrics(); @@ -72,7 +72,7 @@ public void testWithBgDetection() { mBytesCollector.yieldTotalBytes(bytes); NetworkMetrics metrics = mMetricsCollector.createMetrics(); - boolean hasMetrics = mMetricsCollector.getSnapshot(metrics); + boolean hasMetrics = mMetricsCollector.getSnapshot(metrics, null); assertThat(hasMetrics).isTrue(); NetworkMetrics expected = new NetworkMetrics(); @@ -91,19 +91,19 @@ public void testDecreasingBytes() throws Exception { mBytesCollector.yieldTotalBytes(bytes); NetworkMetrics metrics = mMetricsCollector.createMetrics(); - assertThat(mMetricsCollector.getSnapshot(metrics)).isTrue(); + assertThat(mMetricsCollector.getSnapshot(metrics, null)).isTrue(); bytes[MOBILE | TX | FG] = 90; mBytesCollector.yieldTotalBytes(bytes); - assertThat(mMetricsCollector.getSnapshot(metrics)).isFalse(); + assertThat(mMetricsCollector.getSnapshot(metrics, null)).isFalse(); verify(logger, times(1)).wtf(anyString(), anyString(), (Throwable) any()); - assertThat(mMetricsCollector.getSnapshot(metrics)).isFalse(); + assertThat(mMetricsCollector.getSnapshot(metrics, null)).isFalse(); // Validate that any further snapshots, even if increasing, are disabled bytes[MOBILE | TX | FG] = 1000; mBytesCollector.yieldTotalBytes(bytes); - assertThat(mMetricsCollector.getSnapshot(metrics)).isFalse(); + assertThat(mMetricsCollector.getSnapshot(metrics, null)).isFalse(); // No new error logged because we've given up on this user session verify(logger, times(1)).wtf(anyString(), anyString(), (Throwable) any()); diff --git a/metrics/src/test/java/com/facebook/battery/metrics/sensor/SensorMetricsCollectorTest.java b/metrics/src/test/java/com/facebook/battery/metrics/sensor/SensorMetricsCollectorTest.java index d5c987d..d407dc1 100644 --- a/metrics/src/test/java/com/facebook/battery/metrics/sensor/SensorMetricsCollectorTest.java +++ b/metrics/src/test/java/com/facebook/battery/metrics/sensor/SensorMetricsCollectorTest.java @@ -42,7 +42,7 @@ public void setUp() { @Test public void test_blank() { - collector.getSnapshot(metrics); + collector.getSnapshot(metrics, null); assertThat(metrics.total.activeTimeMs).isEqualTo(0); assertThat(metrics.total.powerMah).isEqualTo(0); assertThat(metrics.total.wakeUpTimeMs).isEqualTo(0); @@ -52,7 +52,7 @@ public void test_blank() { public void test_disabled() { collector.disable(); - assertThat(collector.getSnapshot(metrics)).isFalse(); + assertThat(collector.getSnapshot(metrics, null)).isFalse(); } /** @@ -69,14 +69,14 @@ public void test_single_sensor() { collector.register(listener, sensor); ShadowSystemClock.setElapsedRealtime(10); - assertThat(collector.getSnapshot(metrics)).isTrue(); + assertThat(collector.getSnapshot(metrics, null)).isTrue(); assertThat(metrics.total.powerMah).isEqualTo((((double) sensor.getPower()) * 9) / 3600 / 1000); assertThat(metrics.total.wakeUpTimeMs).isEqualTo(0); assertThat(metrics.total.activeTimeMs).isEqualTo(9); ShadowSystemClock.setElapsedRealtime(20); - assertThat(collector.getSnapshot(metrics)).isTrue(); + assertThat(collector.getSnapshot(metrics, null)).isTrue(); assertThat(metrics.total.powerMah).isEqualTo((((double) sensor.getPower()) * 19) / 3600 / 1000); assertThat(metrics.total.wakeUpTimeMs).isEqualTo(0); @@ -85,7 +85,7 @@ public void test_single_sensor() { collector.unregister(listener, null); ShadowSystemClock.setElapsedRealtime(50); - assertThat(collector.getSnapshot(metrics)).isTrue(); + assertThat(collector.getSnapshot(metrics, null)).isTrue(); assertThat(metrics.total.activeTimeMs).isEqualTo(19); } @@ -106,27 +106,27 @@ public void test_multiple_listeners_single_sensor() { collector.register(listener, sensor); ShadowSystemClock.setElapsedRealtime(5); - assertThat(collector.getSnapshot(metrics)).isTrue(); + assertThat(collector.getSnapshot(metrics, null)).isTrue(); assertThat(metrics.total.activeTimeMs).isEqualTo(4); ShadowSystemClock.setElapsedRealtime(10); collector.register(listenerB, sensor); ShadowSystemClock.setElapsedRealtime(11); - assertThat(collector.getSnapshot(metrics)).isTrue(); + assertThat(collector.getSnapshot(metrics, null)).isTrue(); assertThat(metrics.total.activeTimeMs).isEqualTo(10); ShadowSystemClock.setElapsedRealtime(15); collector.unregister(listenerB, null); ShadowSystemClock.setElapsedRealtime(20); - assertThat(collector.getSnapshot(metrics)).isTrue(); + assertThat(collector.getSnapshot(metrics, null)).isTrue(); assertThat(metrics.total.activeTimeMs).isEqualTo(19); collector.unregister(listener, null); ShadowSystemClock.setElapsedRealtime(25); - assertThat(collector.getSnapshot(metrics)).isTrue(); + assertThat(collector.getSnapshot(metrics, null)).isTrue(); assertThat(metrics.total.activeTimeMs).isEqualTo(19); } @@ -153,7 +153,7 @@ public void test_single_listener_multiple_sensors() { ShadowSystemClock.setElapsedRealtime(15); collector.unregister(listener, null); - assertThat(collector.getSnapshot(metrics)).isTrue(); + assertThat(collector.getSnapshot(metrics, null)).isTrue(); assertThat(metrics.total.activeTimeMs).isEqualTo(5 + 14); assertThat(metrics.total.powerMah) .isEqualTo((5 * 20.0) / 3600 / 1000 + (14 * 10.0) / 3600 / 1000); @@ -177,28 +177,28 @@ public void test_multiple_listeners_sensors() { collector.register(listener, sensor); ShadowSystemClock.setElapsedRealtime(5); - assertThat(collector.getSnapshot(metrics)).isTrue(); + assertThat(collector.getSnapshot(metrics, null)).isTrue(); assertThat(metrics.total.activeTimeMs).isEqualTo(4); ShadowSystemClock.setElapsedRealtime(10); collector.register(listenerB, sensorB); ShadowSystemClock.setElapsedRealtime(13); - assertThat(collector.getSnapshot(metrics)).isTrue(); + assertThat(collector.getSnapshot(metrics, null)).isTrue(); assertThat(metrics.total.activeTimeMs).isEqualTo(12 + 3); ShadowSystemClock.setElapsedRealtime(15); collector.unregister(listener, null); ShadowSystemClock.setElapsedRealtime(18); - assertThat(collector.getSnapshot(metrics)).isTrue(); + assertThat(collector.getSnapshot(metrics, null)).isTrue(); assertThat(metrics.total.activeTimeMs).isEqualTo(14 + 8); ShadowSystemClock.setElapsedRealtime(20); collector.unregister(listenerB, null); ShadowSystemClock.setElapsedRealtime(50); - assertThat(collector.getSnapshot(metrics)).isTrue(); + assertThat(collector.getSnapshot(metrics, null)).isTrue(); assertThat(metrics.total.activeTimeMs).isEqualTo(14 + 10); } @@ -234,7 +234,7 @@ public void test_attribution_snapshot() { ShadowSystemClock.setElapsedRealtime(50); metrics.isAttributionEnabled = true; - assertThat(collector.getSnapshot(metrics)).isTrue(); + assertThat(collector.getSnapshot(metrics, null)).isTrue(); assertThat(metrics.total.activeTimeMs).isEqualTo(14 + 10); assertThat(metrics.sensorConsumption.size()).isEqualTo(2); assertThat(metrics.sensorConsumption.get(1337).activeTimeMs).isEqualTo(14); diff --git a/metrics/src/test/java/com/facebook/battery/metrics/time/TimeMetricsCollectorTest.java b/metrics/src/test/java/com/facebook/battery/metrics/time/TimeMetricsCollectorTest.java index 7065cc7..bf8fc03 100644 --- a/metrics/src/test/java/com/facebook/battery/metrics/time/TimeMetricsCollectorTest.java +++ b/metrics/src/test/java/com/facebook/battery/metrics/time/TimeMetricsCollectorTest.java @@ -26,7 +26,7 @@ public void testTimes() { ShadowSystemClock.setElapsedRealtime(9876); TimeMetrics snapshot = new TimeMetrics(); TimeMetricsCollector collector = new TimeMetricsCollector(); - collector.getSnapshot(snapshot); + collector.getSnapshot(snapshot, null); assertThat(snapshot.uptimeMs).isEqualTo(1234); assertThat(snapshot.realtimeMs).isEqualTo(9876); diff --git a/metrics/src/test/java/com/facebook/battery/metrics/wakelock/WakeLockMetricsCollectorTest.java b/metrics/src/test/java/com/facebook/battery/metrics/wakelock/WakeLockMetricsCollectorTest.java index fceecb7..406190f 100644 --- a/metrics/src/test/java/com/facebook/battery/metrics/wakelock/WakeLockMetricsCollectorTest.java +++ b/metrics/src/test/java/com/facebook/battery/metrics/wakelock/WakeLockMetricsCollectorTest.java @@ -58,24 +58,24 @@ public void testUnattributedSnapshot() { PowerManager.WakeLock wakelockA = mPowerManager.newWakeLock(0, "testA"); mCollector.newWakeLock(wakelockA, 0, "testA"); - mCollector.getSnapshot(metrics); + mCollector.getSnapshot(metrics, null); assertThat(metrics.heldTimeMs).as("Held time at beginning").isEqualTo(0); ShadowSystemClock.setUptimeMillis(1); mCollector.acquire(wakelockA, -1); ShadowSystemClock.setUptimeMillis(61); - mCollector.getSnapshot(metrics); + mCollector.getSnapshot(metrics, null); assertThat(metrics.heldTimeMs).as("Intermediate held time").isEqualTo(60); ShadowSystemClock.setUptimeMillis(91); mCollector.release(wakelockA, 0); - mCollector.getSnapshot(metrics); + mCollector.getSnapshot(metrics, null); assertThat(metrics.heldTimeMs).as("Held time at release").isEqualTo(90); ShadowSystemClock.setUptimeMillis(121); - mCollector.getSnapshot(metrics); + mCollector.getSnapshot(metrics, null); assertThat(metrics.heldTimeMs).as("Final held time").isEqualTo(90); } @@ -98,7 +98,7 @@ public void testAttributedSnapshot() { mCollector.newWakeLock(wakeLockB, 0, "testB"); ShadowSystemClock.setUptimeMillis(1); - mCollector.getSnapshot(metrics); + mCollector.getSnapshot(metrics, null); assertThat(metrics.heldTimeMs).as("Initialization").isEqualTo(0); assertThat(metrics.tagTimeMs.isEmpty()).isFalse(); assertThat(metrics.tagTimeMs.get("testA")).isEqualTo(0); @@ -106,32 +106,32 @@ public void testAttributedSnapshot() { ShadowSystemClock.setUptimeMillis(1); mCollector.acquire(wakeLockA, -1); - mCollector.getSnapshot(metrics); + mCollector.getSnapshot(metrics, null); assertThat(metrics.heldTimeMs).as("Acquired A").isEqualTo(0); ShadowSystemClock.setUptimeMillis(31); mCollector.acquire(wakeLockB, -1); - mCollector.getSnapshot(metrics); + mCollector.getSnapshot(metrics, null); assertThat(metrics.heldTimeMs).as("Acquired B").isEqualTo(30); assertThat(metrics.tagTimeMs.get("testA")).isEqualTo(30); assertThat(metrics.tagTimeMs.get("testB")).isEqualTo(0); ShadowSystemClock.setUptimeMillis(61); mCollector.release(wakeLockB, 0); - mCollector.getSnapshot(metrics); + mCollector.getSnapshot(metrics, null); assertThat(metrics.heldTimeMs).as("Released B").isEqualTo(60); assertThat(metrics.tagTimeMs.get("testA")).isEqualTo(60); assertThat(metrics.tagTimeMs.get("testB")).isEqualTo(30); ShadowSystemClock.setUptimeMillis(91); mCollector.release(wakeLockA, 0); - mCollector.getSnapshot(metrics); + mCollector.getSnapshot(metrics, null); assertThat(metrics.heldTimeMs).as("Released A").isEqualTo(90); assertThat(metrics.tagTimeMs.get("testA")).isEqualTo(90); assertThat(metrics.tagTimeMs.get("testB")).isEqualTo(30); ShadowSystemClock.setUptimeMillis(121); - mCollector.getSnapshot(metrics); + mCollector.getSnapshot(metrics, null); assertThat(metrics.heldTimeMs).as("Final").isEqualTo(90); assertThat(metrics.tagTimeMs.get("testA")).isEqualTo(90); assertThat(metrics.tagTimeMs.get("testB")).isEqualTo(30); @@ -143,10 +143,10 @@ public void testDisabling() { PowerManager.WakeLock wakeLockA = mPowerManager.newWakeLock(0, "testA"); mCollector.newWakeLock(wakeLockA, 0, "testA"); - assertThat(mCollector.getSnapshot(metrics)).isTrue(); + assertThat(mCollector.getSnapshot(metrics, null)).isTrue(); mCollector.disable(); - assertThat(mCollector.getSnapshot(metrics)).isFalse(); + assertThat(mCollector.getSnapshot(metrics, null)).isFalse(); // Sanity check that nothing throws an exception or logs after disabling mCollector.release(wakeLockA, 0); @@ -167,7 +167,7 @@ public void testWakelockTimeouts() { WakeLockMetrics metricsA = new WakeLockMetrics(true); - assertThat(mCollector.getSnapshot(metricsA)).isTrue(); + assertThat(mCollector.getSnapshot(metricsA, null)).isTrue(); assertThat(metricsA.acquiredCount).isEqualTo(0); assertThat(metricsA.heldTimeMs).isEqualTo(0); assertThat(metricsA.tagTimeMs.size()).isEqualTo(0); @@ -179,14 +179,14 @@ public void testWakelockTimeouts() { mCollector.acquire(wakelock, 100); ShadowSystemClock.setUptimeMillis(51); - assertThat(mCollector.getSnapshot(metricsA)).isTrue(); + assertThat(mCollector.getSnapshot(metricsA, null)).isTrue(); assertThat(metricsA.acquiredCount).isEqualTo(1); assertThat(metricsA.heldTimeMs).isEqualTo(50); assertThat(metricsA.tagTimeMs.size()).isEqualTo(1); assertThat(metricsA.tagTimeMs.get("tag")).isEqualTo(50); ShadowSystemClock.setUptimeMillis(151); - assertThat(mCollector.getSnapshot(metricsA)).isTrue(); + assertThat(mCollector.getSnapshot(metricsA, null)).isTrue(); assertThat(metricsA.acquiredCount).isEqualTo(1); assertThat(metricsA.heldTimeMs).isEqualTo(100); assertThat(metricsA.tagTimeMs.size()).isEqualTo(1); @@ -217,14 +217,14 @@ public void testWakelockNonReferenceCountedTimeoutAndReleases() throws Exception mCollector.acquire(wakelock, 100); ShadowSystemClock.setUptimeMillis(51); - assertThat(mCollector.getSnapshot(metricsA)).isTrue(); + assertThat(mCollector.getSnapshot(metricsA, null)).isTrue(); assertThat(metricsA.acquiredCount).isEqualTo(1); assertThat(metricsA.heldTimeMs).isEqualTo(50); assertThat(metricsA.tagTimeMs.size()).isEqualTo(1); assertThat(metricsA.tagTimeMs.get("tag")).isEqualTo(50); ShadowSystemClock.setUptimeMillis(151); - assertThat(mCollector.getSnapshot(metricsA)).isTrue(); + assertThat(mCollector.getSnapshot(metricsA, null)).isTrue(); assertThat(metricsA.acquiredCount).isEqualTo(1); assertThat(metricsA.heldTimeMs).isEqualTo(100); assertThat(metricsA.tagTimeMs.size()).isEqualTo(1); @@ -234,7 +234,7 @@ public void testWakelockNonReferenceCountedTimeoutAndReleases() throws Exception wakelock.release(); mCollector.release(wakelock, -1); WakeLockMetrics metricsB = new WakeLockMetrics(true); - assertThat(mCollector.getSnapshot(metricsB)).isTrue(); + assertThat(mCollector.getSnapshot(metricsB, null)).isTrue(); assertThat(metricsB).isEqualTo(metricsA); } @@ -259,7 +259,7 @@ public void testNotReferenceCountedWakeLock() { mCollector.acquire(wakelock, -1); ShadowSystemClock.setUptimeMillis(151); - assertThat(mCollector.getSnapshot(snapshot)).isTrue(); + assertThat(mCollector.getSnapshot(snapshot, null)).isTrue(); assertThat(snapshot.acquiredCount).isEqualTo(1); assertThat(snapshot.heldTimeMs).isEqualTo(100); @@ -267,7 +267,7 @@ public void testNotReferenceCountedWakeLock() { mCollector.release(wakelock, -1); ShadowSystemClock.setUptimeMillis(201); - assertThat(mCollector.getSnapshot(snapshot)).isTrue(); + assertThat(mCollector.getSnapshot(snapshot, null)).isTrue(); assertThat(snapshot.acquiredCount).isEqualTo(1); assertThat(snapshot.heldTimeMs).isEqualTo(100); @@ -275,7 +275,7 @@ public void testNotReferenceCountedWakeLock() { mCollector.release(wakelock, -1); ShadowSystemClock.setUptimeMillis(251); - assertThat(mCollector.getSnapshot(snapshot)).isTrue(); + assertThat(mCollector.getSnapshot(snapshot, null)).isTrue(); assertThat(snapshot.acquiredCount).isEqualTo(1); assertThat(snapshot.heldTimeMs).isEqualTo(100); } diff --git a/sample/src/main/java/com/facebook/battery/sample/MainActivity.java b/sample/src/main/java/com/facebook/battery/sample/MainActivity.java index 7d6ca99..3f01bc8 100644 --- a/sample/src/main/java/com/facebook/battery/sample/MainActivity.java +++ b/sample/src/main/java/com/facebook/battery/sample/MainActivity.java @@ -97,7 +97,7 @@ public void run() { } private void updateContent() { - mCollector.getSnapshot(mMetrics); + mCollector.getSnapshot(mMetrics, this); String text = "Snapshot at " + SystemClock.elapsedRealtime() + ":\n\n" + mMetrics.toString(); mContent.setText(text); Log.d("BatteryMetrics", text);