summaryrefslogtreecommitdiffstats
path: root/testing/raptor/test/test_cpu.py
blob: c60d5c6577ec357e550873de62ae92e84565344b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
import os
import sys
from unittest import mock

import mozunit

# need this so the raptor unit tests can find output & filter classes
here = os.path.abspath(os.path.dirname(__file__))
raptor_dir = os.path.join(os.path.dirname(here), "raptor")
sys.path.insert(0, raptor_dir)

import cpu
from webextension import WebExtensionAndroid


def test_no_device():
    raptor = WebExtensionAndroid(
        "geckoview",
        "org.mozilla.org.mozilla.geckoview_example",
        cpu_test=True,
    )
    raptor.device = None
    resp = cpu.start_android_cpu_profiler(raptor)
    assert resp is None


def test_usage_with_invalid_data_returns_zero():
    with mock.patch("mozdevice.adb.ADBDevice") as device:
        with mock.patch("control_server.RaptorControlServer") as control_server:
            # Create a device that returns invalid data
            device.shell_output.side_effect = ["8.0.0", "geckoview"]
            device._verbose = True

            # Create a control server
            control_server.cpu_test = True
            control_server.device = device
            raptor = WebExtensionAndroid("geckoview", "org.mozilla.geckoview_example")
            raptor.config["cpu_test"] = True
            raptor.control_server = control_server
            raptor.device = device

            cpu_profiler = cpu.AndroidCPUProfiler(raptor)
            cpu_profiler.get_app_cpu_usage()

            # Verify the call to submit data was made
            avg_cpuinfo_data = {
                "type": "cpu",
                "test": "usage_with_invalid_data_returns_zero-avg",
                "unit": "%",
                "values": {"avg": 0},
            }
            min_cpuinfo_data = {
                "type": "cpu",
                "test": "usage_with_invalid_data_returns_zero-min",
                "unit": "%",
                "values": {"min": 0},
            }
            max_cpuinfo_data = {
                "type": "cpu",
                "test": "usage_with_invalid_data_returns_zero-max",
                "unit": "%",
                "values": {"max": 0},
            }

            cpu_profiler.generate_android_cpu_profile(
                "usage_with_invalid_data_returns_zero"
            )
            control_server.submit_supporting_data.assert_has_calls(
                [
                    mock.call(avg_cpuinfo_data),
                    mock.call(min_cpuinfo_data),
                    mock.call(max_cpuinfo_data),
                ]
            )


def test_usage_with_output():
    with mock.patch("mozdevice.adb.ADBDevice") as device:
        with mock.patch("control_server.RaptorControlServer") as control_server:
            # Override the shell output with sample CPU usage details
            filepath = os.path.abspath(os.path.dirname(__file__)) + "/files/"
            with open(filepath + "top-info.txt", "r") as f:
                test_data = f.read()
            device.shell_output.side_effect = ["8.0.0", test_data]
            device._verbose = True

            # Create a control server
            control_server.cpu_test = True
            control_server.test_name = "cpuunittest"
            control_server.device = device
            control_server.app_name = "org.mozilla.geckoview_example"
            raptor = WebExtensionAndroid("geckoview", "org.mozilla.geckoview_example")
            raptor.device = device
            raptor.config["cpu_test"] = True
            raptor.control_server = control_server

            cpu_profiler = cpu.AndroidCPUProfiler(raptor)
            cpu_profiler.polls.append(cpu_profiler.get_app_cpu_usage())
            cpu_profiler.polls.append(0)

            # Verify the response contains our expected CPU % of 93.7
            avg_cpuinfo_data = {
                "type": "cpu",
                "test": "usage_with_integer_cpu_info_output-avg",
                "unit": "%",
                "values": {"avg": 93.7 / 2},
            }
            min_cpuinfo_data = {
                "type": "cpu",
                "test": "usage_with_integer_cpu_info_output-min",
                "unit": "%",
                "values": {"min": 0},
            }
            max_cpuinfo_data = {
                "type": "cpu",
                "test": "usage_with_integer_cpu_info_output-max",
                "unit": "%",
                "values": {"max": 93.7},
            }

            cpu_profiler.generate_android_cpu_profile(
                "usage_with_integer_cpu_info_output"
            )
            control_server.submit_supporting_data.assert_has_calls(
                [
                    mock.call(avg_cpuinfo_data),
                    mock.call(min_cpuinfo_data),
                    mock.call(max_cpuinfo_data),
                ]
            )


def test_usage_with_fallback():
    with mock.patch("mozdevice.adb.ADBDevice") as device:
        with mock.patch("control_server.RaptorControlServer") as control_server:
            device._verbose = True

            # Return what our shell call to dumpsys would give us
            shell_output = (
                " 31093 u0_a196  10 -10   8% S    "
                + "66 1392100K 137012K  fg org.mozilla.geckoview_example"
            )

            # We set the version to be less than Android 8
            device.shell_output.side_effect = ["7.0.0", shell_output]

            # Create a control server
            control_server.cpu_test = True
            control_server.test_name = "cpuunittest"
            control_server.device = device
            control_server.app_name = "org.mozilla.geckoview_example"
            raptor = WebExtensionAndroid("geckoview", "org.mozilla.geckoview_example")
            raptor.device = device
            raptor.config["cpu_test"] = True
            raptor.control_server = control_server

            cpu_profiler = cpu.AndroidCPUProfiler(raptor)
            cpu_profiler.polls.append(cpu_profiler.get_app_cpu_usage())
            cpu_profiler.polls.append(0)

            # Verify the response contains our expected CPU % of 8
            # pylint --py3k W1619
            avg_cpuinfo_data = {
                "type": "cpu",
                "test": "usage_with_fallback-avg",
                "unit": "%",
                "values": {"avg": 8 / 2},
            }
            min_cpuinfo_data = {
                "type": "cpu",
                "test": "usage_with_fallback-min",
                "unit": "%",
                "values": {"min": 0},
            }
            max_cpuinfo_data = {
                "type": "cpu",
                "test": "usage_with_fallback-max",
                "unit": "%",
                "values": {"max": 8},
            }

            cpu_profiler.generate_android_cpu_profile("usage_with_fallback")
            control_server.submit_supporting_data.assert_has_calls(
                [
                    mock.call(avg_cpuinfo_data),
                    mock.call(min_cpuinfo_data),
                    mock.call(max_cpuinfo_data),
                ]
            )


if __name__ == "__main__":
    mozunit.main()