| 1 |
# This Source Code Form is subject to the terms of the Mozilla Public |
| 2 |
# License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 |
# file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 4 |
import re |
| 5 |
import statistics |
| 6 |
import sys |
| 7 |
import time |
| 8 |
from datetime import datetime |
| 9 |
|
| 10 |
import mozdevice |
| 11 |
|
| 12 |
ITERATIONS = 50 |
| 13 |
DATETIME_FORMAT = "%Y.%m.%d" |
| 14 |
PAGE_START_MOZ = re.compile("GeckoSession: handleMessage GeckoView:PageStart uri=") |
| 15 |
|
| 16 |
PROD_FENIX = "fenix" |
| 17 |
PROD_FOCUS = "focus" |
| 18 |
PROD_GVEX = "geckoview" |
| 19 |
PROD_CHRM = "chrome-m" |
| 20 |
MOZILLA_PRODUCTS = [PROD_FENIX, PROD_FOCUS, PROD_GVEX] |
| 21 |
|
| 22 |
MEASUREMENT_DATA = ["mean", "median", "standard_deviation"] |
| 23 |
OLD_VERSION_FOCUS_PAGE_START_LINE_COUNT = 3 |
| 24 |
NEW_VERSION_FOCUS_PAGE_START_LINE_COUNT = 2 |
| 25 |
STDOUT_LINE_COUNT = 2 |
| 26 |
|
| 27 |
TEST_COLD_MAIN_FF = "cold_main_first_frame" |
| 28 |
TEST_COLD_MAIN_RESTORE = "cold_main_session_restore" |
| 29 |
TEST_COLD_VIEW_FF = "cold_view_first_frame" |
| 30 |
TEST_COLD_VIEW_NAV_START = "cold_view_nav_start" |
| 31 |
TEST_URI = "https://example.com" |
| 32 |
|
| 33 |
PROD_TO_CHANNEL_TO_PKGID = { |
| 34 |
PROD_FENIX: { |
| 35 |
"nightly": "org.mozilla.fenix", |
| 36 |
"beta": "org.mozilla.firefox.beta", |
| 37 |
"release": "org.mozilla.firefox", |
| 38 |
"debug": "org.mozilla.fenix.debug", |
| 39 |
}, |
| 40 |
PROD_FOCUS: { |
| 41 |
"nightly": "org.mozilla.focus.nightly", |
| 42 |
"beta": "org.mozilla.focus.beta", # only present since post-fenix update. |
| 43 |
"release": "org.mozilla.focus.nightly", |
| 44 |
"debug": "org.mozilla.focus.debug", |
| 45 |
}, |
| 46 |
PROD_GVEX: { |
| 47 |
"nightly": "org.mozilla.geckoview_example", |
| 48 |
"release": "org.mozilla.geckoview_example", |
| 49 |
}, |
| 50 |
PROD_CHRM: { |
| 51 |
"nightly": "com.android.chrome", |
| 52 |
"release": "com.android.chrome", |
| 53 |
}, |
| 54 |
} |
| 55 |
TEST_LIST = [ |
| 56 |
"cold_main_first_frame", |
| 57 |
"cold_view_nav_start", |
| 58 |
"cold_view_first_frame", |
| 59 |
"cold_main_session_restore", |
| 60 |
] |
| 61 |
# "cold_view_first_frame", "cold_main_session_restore" are 2 disabled tests(broken) |
| 62 |
|
| 63 |
|
| 64 |
class AndroidStartUpUnknownTestError(Exception): |
| 65 |
""" |
| 66 |
Test name provided is not one avaiable to test, this is either because |
| 67 |
the test is currently not being tested or a typo in the spelling |
| 68 |
""" |
| 69 |
|
| 70 |
pass |
| 71 |
|
| 72 |
|
| 73 |
class AndroidStartUpMatchingError(Exception): |
| 74 |
""" |
| 75 |
We expected a certain number of matches but did not get them |
| 76 |
""" |
| 77 |
|
| 78 |
pass |
| 79 |
|
| 80 |
|
| 81 |
class Startup_test: |
| 82 |
def __init__(self, browser, startup_test): |
| 83 |
self.test_name = startup_test |
| 84 |
self.product = browser |
| 85 |
|
| 86 |
def run(self): |
| 87 |
self.device = mozdevice.ADBDevice(use_root=False) |
| 88 |
self.release_channel = "nightly" |
| 89 |
self.architecture = "arm64-v8a" |
| 90 |
self.startup_cache = True |
| 91 |
self.package_id = PROD_TO_CHANNEL_TO_PKGID[self.product][self.release_channel] |
| 92 |
self.proc_start = re.compile( |
| 93 |
rf"ActivityManager: Start proc \d+:{self.package_id}/" |
| 94 |
) |
| 95 |
self.key_name = f"{self.product}_nightly_{self.architecture}.apk" |
| 96 |
results = self.run_tests() |
| 97 |
|
| 98 |
# Cleanup |
| 99 |
if self.product in MOZILLA_PRODUCTS: |
| 100 |
self.device.uninstall_app(self.package_id) |
| 101 |
|
| 102 |
return results |
| 103 |
|
| 104 |
def should_alert(self, key_name): |
| 105 |
if MEASUREMENT_DATA[2] in key_name: |
| 106 |
return False |
| 107 |
return True |
| 108 |
|
| 109 |
def run_tests(self): |
| 110 |
measurements = {} |
| 111 |
# Iterate through the tests in the test list |
| 112 |
print(f"Running {self.test_name} on {self.package_id}...") |
| 113 |
time.sleep(self.get_warmup_delay_seconds()) |
| 114 |
self.skip_onboarding(self.test_name) |
| 115 |
test_measurements = [] |
| 116 |
|
| 117 |
for i in range(ITERATIONS): |
| 118 |
start_cmd_args = self.get_start_cmd(self.test_name) |
| 119 |
print(start_cmd_args) |
| 120 |
self.device.stop_application(self.package_id) |
| 121 |
time.sleep(1) |
| 122 |
print(f"iteration {i + 1}") |
| 123 |
self.device.shell("logcat -c") |
| 124 |
process = self.device.shell_output(start_cmd_args).splitlines() |
| 125 |
test_measurements.append(self.get_measurement(self.test_name, process)) |
| 126 |
|
| 127 |
self.device.stop_application(self.package_id) |
| 128 |
print(f"{self.test_name}: {str(test_measurements)}") |
| 129 |
# Bug 1934023 - create way to pass median and still have replicates available |
| 130 |
measurements[f"{self.test_name}.{MEASUREMENT_DATA[0]}"] = [ |
| 131 |
statistics.mean(test_measurements) |
| 132 |
] |
| 133 |
print(f"Mean: {statistics.mean(test_measurements)}") |
| 134 |
measurements[f"{self.test_name}.{MEASUREMENT_DATA[1]}"] = [ |
| 135 |
statistics.median(test_measurements) |
| 136 |
] |
| 137 |
print(f"Median: {statistics.median(test_measurements)}") |
| 138 |
if ITERATIONS > 1: |
| 139 |
measurements[f"{self.test_name}.{MEASUREMENT_DATA[2]}"] = [ |
| 140 |
statistics.stdev(test_measurements) |
| 141 |
] |
| 142 |
print(f"Standard Deviation: {statistics.stdev(test_measurements)}") |
| 143 |
|
| 144 |
return measurements |
| 145 |
|
| 146 |
def get_measurement(self, test_name, stdout): |
| 147 |
if test_name in [TEST_COLD_MAIN_FF, TEST_COLD_VIEW_FF]: |
| 148 |
return self.get_measurement_from_am_start_log(stdout) |
| 149 |
elif ( |
| 150 |
test_name in [TEST_COLD_VIEW_NAV_START, TEST_COLD_MAIN_RESTORE] |
| 151 |
and self.product in MOZILLA_PRODUCTS |
| 152 |
): |
| 153 |
# We must sleep until the Navigation::Start event occurs. If we don't |
| 154 |
# the script will fail. This can take up to 14s on the G5 |
| 155 |
time.sleep(17) |
| 156 |
proc = self.device.shell_output("logcat -d") |
| 157 |
return self.get_measurement_from_nav_start_logcat(proc) |
| 158 |
else: |
| 159 |
raise AndroidStartUpUnknownTestError( |
| 160 |
"invalid test settings selected, please double check that " |
| 161 |
"the test name is valid and that the test is supported for " |
| 162 |
"the browser you are testing" |
| 163 |
) |
| 164 |
|
| 165 |
def get_measurement_from_am_start_log(self, stdout): |
| 166 |
total_time_prefix = "TotalTime: " |
| 167 |
matching_lines = [line for line in stdout if line.startswith(total_time_prefix)] |
| 168 |
if len(matching_lines) != 1: |
| 169 |
raise AndroidStartUpMatchingError( |
| 170 |
f"Each run should only have 1 {total_time_prefix}." |
| 171 |
f"However, this run unexpectedly had {matching_lines} matching lines" |
| 172 |
) |
| 173 |
duration = int(matching_lines[0][len(total_time_prefix) :]) |
| 174 |
return duration |
| 175 |
|
| 176 |
def get_measurement_from_nav_start_logcat(self, process_output): |
| 177 |
def __line_to_datetime(line): |
| 178 |
date_str = " ".join(line.split(" ")[:2]) # e.g. "05-18 14:32:47.366" |
| 179 |
# strptime needs microseconds. logcat outputs millis so we append zeroes |
| 180 |
date_str_with_micros = date_str + "000" |
| 181 |
return datetime.strptime(date_str_with_micros, "%m-%d %H:%M:%S.%f") |
| 182 |
|
| 183 |
def __get_proc_start_datetime(): |
| 184 |
# This regex may not work on older versions of Android: we don't care |
| 185 |
# yet because supporting older versions isn't in our requirements. |
| 186 |
proc_start_lines = [line for line in lines if self.proc_start.search(line)] |
| 187 |
if len(proc_start_lines) != 1: |
| 188 |
raise AndroidStartUpMatchingError( |
| 189 |
f"Expected to match 1 process start string but matched {len(proc_start_lines)}" |
| 190 |
) |
| 191 |
return __line_to_datetime(proc_start_lines[0]) |
| 192 |
|
| 193 |
def __get_page_start_datetime(): |
| 194 |
page_start_lines = [line for line in lines if PAGE_START_MOZ.search(line)] |
| 195 |
page_start_line_count = len(page_start_lines) |
| 196 |
page_start_assert_msg = "found len=" + str(page_start_line_count) |
| 197 |
|
| 198 |
# In focus versions <= v8.8.2, it logs 3 PageStart lines and these include actual uris. |
| 199 |
# We need to handle our assertion differently due to the different line count. In focus |
| 200 |
# versions >= v8.8.3, this measurement is broken because the logcat were removed. |
| 201 |
is_old_version_of_focus = ( |
| 202 |
"about:blank" in page_start_lines[0] and self.product == PROD_FOCUS |
| 203 |
) |
| 204 |
if is_old_version_of_focus: |
| 205 |
assert ( |
| 206 |
page_start_line_count |
| 207 |
== OLD_VERSION_FOCUS_PAGE_START_LINE_COUNT # should be 3 |
| 208 |
), page_start_assert_msg # Lines: about:blank, target URL, target URL. |
| 209 |
else: |
| 210 |
assert ( |
| 211 |
page_start_line_count |
| 212 |
== NEW_VERSION_FOCUS_PAGE_START_LINE_COUNT # Should be 2 |
| 213 |
), page_start_assert_msg # Lines: about:blank, target URL. |
| 214 |
return __line_to_datetime( |
| 215 |
page_start_lines[1] |
| 216 |
) # 2nd PageStart is for target URL. |
| 217 |
|
| 218 |
lines = process_output.split("\n") |
| 219 |
elapsed_seconds = ( |
| 220 |
__get_page_start_datetime() - __get_proc_start_datetime() |
| 221 |
).total_seconds() |
| 222 |
elapsed_millis = round(elapsed_seconds * 1000) |
| 223 |
return elapsed_millis |
| 224 |
|
| 225 |
def get_warmup_delay_seconds(self): |
| 226 |
""" |
| 227 |
We've been told the start up cache is populated ~60s after first start up. As such, |
| 228 |
we should measure start up with the start up cache populated. If the |
| 229 |
args say we shouldn't wait, we only wait a short duration ~= visual completeness. |
| 230 |
""" |
| 231 |
return 60 if self.startup_cache else 5 |
| 232 |
|
| 233 |
def get_start_cmd(self, test_name): |
| 234 |
intent_action_prefix = "android.intent.action.{}" |
| 235 |
if test_name in [TEST_COLD_MAIN_FF, TEST_COLD_MAIN_RESTORE]: |
| 236 |
intent = ( |
| 237 |
f"-a {intent_action_prefix.format('MAIN')} " |
| 238 |
f"-c android.intent.category.LAUNCHER" |
| 239 |
) |
| 240 |
elif test_name in [TEST_COLD_VIEW_FF, TEST_COLD_VIEW_NAV_START]: |
| 241 |
intent = f"-a {intent_action_prefix.format('VIEW')} -d {TEST_URI}" |
| 242 |
else: |
| 243 |
raise AndroidStartUpUnknownTestError( |
| 244 |
"Unknown test provided please double check the test name and spelling" |
| 245 |
) |
| 246 |
|
| 247 |
# You can't launch an app without an pkg_id/activity pair |
| 248 |
component_name = self.get_component_name_for_intent(intent) |
| 249 |
cmd = f"am start-activity -W -n {component_name} {intent} " |
| 250 |
|
| 251 |
# If focus skip onboarding: it is not stateful so must be sent for every cold start intent |
| 252 |
if self.product == PROD_FOCUS: |
| 253 |
cmd += "--ez performancetest true" |
| 254 |
|
| 255 |
return cmd |
| 256 |
|
| 257 |
def get_component_name_for_intent(self, intent): |
| 258 |
resolve_component_args = ( |
| 259 |
f"cmd package resolve-activity --brief {intent} {self.package_id}" |
| 260 |
) |
| 261 |
result_output = self.device.shell_output(resolve_component_args) |
| 262 |
stdout = result_output.splitlines() |
| 263 |
if len(stdout) != STDOUT_LINE_COUNT: # Should be 2 |
| 264 |
if "No activity found" in stdout: |
| 265 |
raise Exception("Please verify your apk is installed") |
| 266 |
raise AndroidStartUpMatchingError(f"expected 2 lines. Got: {stdout}") |
| 267 |
return stdout[1] |
| 268 |
|
| 269 |
def skip_onboarding(self, test_name): |
| 270 |
self.device.enable_notifications(self.package_id) |
| 271 |
if self.product in MOZILLA_PRODUCTS: |
| 272 |
self.skip_app_onboarding() |
| 273 |
|
| 274 |
if self.product == PROD_FOCUS or test_name not in { |
| 275 |
TEST_COLD_MAIN_FF, |
| 276 |
TEST_COLD_MAIN_RESTORE, |
| 277 |
}: |
| 278 |
return |
| 279 |
|
| 280 |
def skip_app_onboarding(self): |
| 281 |
""" |
| 282 |
We skip onboarding for focus in measure_start_up.py because it's stateful |
| 283 |
and needs to be called for every cold start intent. |
| 284 |
Onboarding only visibly gets in the way of our MAIN test results. |
| 285 |
""" |
| 286 |
# This sets mutable state we only need to pass this flag once, before we start the test |
| 287 |
self.device.shell( |
| 288 |
f"am start-activity -W -a android.intent.action.MAIN --ez " |
| 289 |
f"performancetest true -n {self.package_id}/org.mozilla.fenix.App" |
| 290 |
) |
| 291 |
time.sleep(4) # ensure skip onboarding call has time to propagate. |
| 292 |
|
| 293 |
|
| 294 |
if __name__ == "__main__": |
| 295 |
if len(sys.argv) < 2: |
| 296 |
raise Exception("Didn't pass the arg properly :(") |
| 297 |
print(len(sys.argv)) |
| 298 |
browser = sys.argv[1] |
| 299 |
test = sys.argv[2] |
| 300 |
start_video_timestamp = [] |
| 301 |
|
| 302 |
Startup = Startup_test(browser, test) |
| 303 |
startup_data = Startup.run() |
| 304 |
for measurement in MEASUREMENT_DATA: |
| 305 |
print( |
| 306 |
'perfMetrics: {"values": ', |
| 307 |
startup_data[f"{test}.{measurement}"], |
| 308 |
', "name": "' + f"{test}.{measurement}" + '", "shouldAlert": true', |
| 309 |
"}", |
| 310 |
) |