1 /*
2  * Copyright (C) 2020 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.internal.os;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 
21 import android.content.Context;
22 import android.os.BatteryStats;
23 import android.os.Bundle;
24 import android.os.UserHandle;
25 import android.perftests.utils.BenchmarkState;
26 import android.perftests.utils.PerfStatusReporter;
27 
28 import androidx.test.InstrumentationRegistry;
29 import androidx.test.filters.LargeTest;
30 import androidx.test.runner.AndroidJUnit4;
31 
32 import org.junit.Rule;
33 import org.junit.Test;
34 import org.junit.runner.RunWith;
35 
36 import java.util.List;
37 
38 @RunWith(AndroidJUnit4.class)
39 @LargeTest
40 public class BatteryStatsHelperPerfTest {
41 
42     @Rule
43     public final PerfStatusReporter mPerfStatusReporter = new PerfStatusReporter();
44 
45     /**
46      * Measures the performance of {@link BatteryStatsHelper#getStats()}, which triggers
47      * a battery stats sync on every iteration.
48      */
49     @Test
testGetStats_forceUpdate()50     public void testGetStats_forceUpdate() {
51         final Context context = InstrumentationRegistry.getContext();
52         final BatteryStatsHelper statsHelper = new BatteryStatsHelper(context,
53                 true /* collectBatteryBroadcast */);
54         statsHelper.create((Bundle) null);
55         statsHelper.refreshStats(BatteryStats.STATS_SINCE_CHARGED, UserHandle.myUserId());
56 
57         final BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
58         while (state.keepRunning()) {
59             state.pauseTiming();
60             statsHelper.clearStats();
61             state.resumeTiming();
62 
63             statsHelper.getStats();
64 
65             assertThat(statsHelper.getUsageList()).isNotEmpty();
66         }
67     }
68 
69     /**
70      * Measures performance of the {@link BatteryStatsHelper#getStats(boolean)}, which does
71      * not trigger a sync and just returns current values.
72      */
73     @Test
testGetStats_cached()74     public void testGetStats_cached() {
75         final Context context = InstrumentationRegistry.getContext();
76         final BatteryStatsHelper statsHelper = new BatteryStatsHelper(context,
77                 true /* collectBatteryBroadcast */);
78         statsHelper.create((Bundle) null);
79         statsHelper.refreshStats(BatteryStats.STATS_SINCE_CHARGED, UserHandle.myUserId());
80 
81         final BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
82         while (state.keepRunning()) {
83             state.pauseTiming();
84             statsHelper.clearStats();
85             state.resumeTiming();
86 
87             statsHelper.getStats(false /* forceUpdate */);
88 
89             assertThat(statsHelper.getUsageList()).isNotEmpty();
90         }
91     }
92 
93     @Test
testPowerCalculation()94     public void testPowerCalculation() {
95         final Context context = InstrumentationRegistry.getContext();
96         final BatteryStatsHelper statsHelper = new BatteryStatsHelper(context,
97                 true /* collectBatteryBroadcast */);
98         statsHelper.create((Bundle) null);
99         statsHelper.getStats();
100 
101         final BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
102         while (state.keepRunning()) {
103             // This will use the cached BatteryStatsObject
104             statsHelper.refreshStats(BatteryStats.STATS_SINCE_CHARGED, UserHandle.myUserId());
105 
106             assertThat(statsHelper.getUsageList()).isNotEmpty();
107         }
108     }
109 
110     @Test
testEndToEnd()111     public void testEndToEnd() {
112         final Context context = InstrumentationRegistry.getContext();
113         final BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
114         while (state.keepRunning()) {
115             final BatteryStatsHelper statsHelper = new BatteryStatsHelper(context,
116                     true /* collectBatteryBroadcast */);
117             statsHelper.create((Bundle) null);
118             statsHelper.clearStats();
119             statsHelper.refreshStats(BatteryStats.STATS_SINCE_CHARGED, UserHandle.myUserId());
120 
121             state.pauseTiming();
122 
123             List<BatterySipper> usageList = statsHelper.getUsageList();
124             double power = 0;
125             for (int i = 0; i < usageList.size(); i++) {
126                 BatterySipper sipper = usageList.get(i);
127                 power += sipper.sumPower();
128             }
129 
130             assertThat(power).isGreaterThan(0.0);
131 
132             state.resumeTiming();
133         }
134     }
135 }
136