| 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 os |
| 5 |
import pathlib |
| 6 |
import re |
| 7 |
import subprocess |
| 8 |
import sys |
| 9 |
import time |
| 10 |
|
| 11 |
from mozperftest.utils import ON_TRY |
| 12 |
|
| 13 |
# Add the python packages installed by mozperftest |
| 14 |
sys.path.insert(0, os.environ["PYTHON_PACKAGES"]) |
| 15 |
|
| 16 |
import cv2 |
| 17 |
import numpy as np |
| 18 |
from mozdevice import ADBDevice |
| 19 |
from mozperftest.profiler import ProfilingMediator |
| 20 |
|
| 21 |
PROD_FENIX = "fenix" |
| 22 |
PROD_CHRM = "chrome-m" |
| 23 |
BACKGROUND_TABS = [ |
| 24 |
"https://www.google.com/search?q=toronto+weather", |
| 25 |
"https://en.m.wikipedia.org/wiki/Anemone_hepatica", |
| 26 |
"https://www.amazon.ca/gp/aw/gb?ref_=navm_cs_gb&discounts-widget", |
| 27 |
"https://www.espn.com/nfl/game/_/gameId/401671793/chiefs-falcons", |
| 28 |
] |
| 29 |
ITERATIONS = 5 |
| 30 |
|
| 31 |
|
| 32 |
class ImageAnalzer: |
| 33 |
def __init__(self, browser, test, test_url): |
| 34 |
self.video = None |
| 35 |
self.browser = browser |
| 36 |
self.test = test |
| 37 |
self.test_url = test_url |
| 38 |
self.width = 0 |
| 39 |
self.height = 0 |
| 40 |
self.video_name = "" |
| 41 |
self.package_name = os.environ["BROWSER_BINARY"] |
| 42 |
self.device = ADBDevice() |
| 43 |
self.profiler = ProfilingMediator() |
| 44 |
self.cpu_data = {"total": {"time": []}} |
| 45 |
if self.browser == PROD_FENIX: |
| 46 |
self.intent = "org.mozilla.fenix/org.mozilla.fenix.IntentReceiverActivity" |
| 47 |
elif self.browser == PROD_CHRM: |
| 48 |
self.intent = ( |
| 49 |
"com.android.chrome/com.google.android.apps.chrome.IntentDispatcher" |
| 50 |
) |
| 51 |
else: |
| 52 |
raise Exception("Bad browser name") |
| 53 |
self.nav_start_command = ( |
| 54 |
f"am start-activity -W -n {self.intent} -a " |
| 55 |
f"android.intent.action.VIEW -d " |
| 56 |
) |
| 57 |
self.view_intent_command = ( |
| 58 |
f"am start-activity -W -n {self.intent} -a " f"android.intent.action.VIEW" |
| 59 |
) |
| 60 |
|
| 61 |
self.device.shell("mkdir -p /sdcard/Download") |
| 62 |
self.device.shell("settings put global window_animation_scale 1") |
| 63 |
self.device.shell("settings put global transition_animation_scale 1") |
| 64 |
self.device.shell("settings put global animator_duration_scale 1") |
| 65 |
|
| 66 |
def app_setup(self): |
| 67 |
if ON_TRY: |
| 68 |
self.device.shell(f"pm clear {self.package_name}") |
| 69 |
time.sleep(3) |
| 70 |
self.skip_onboarding() |
| 71 |
self.device.shell( |
| 72 |
f"pm grant {self.package_name} android.permission.POST_NOTIFICATIONS" |
| 73 |
) # enabling notifications |
| 74 |
if self.test != "homeview_startup": |
| 75 |
self.create_background_tabs() |
| 76 |
self.device.shell(f"am force-stop {self.package_name}") |
| 77 |
|
| 78 |
def skip_onboarding(self): |
| 79 |
# Skip onboarding for chrome and fenix |
| 80 |
if self.browser == PROD_CHRM: |
| 81 |
self.device.shell_output( |
| 82 |
'echo "chrome --no-default-browser-check --no-first-run ' |
| 83 |
'--disable-fre" > /data/local/tmp/chrome-command-line ' |
| 84 |
) |
| 85 |
self.device.shell("am set-debug-app --persistent com.android.chrome") |
| 86 |
elif self.browser == PROD_FENIX: |
| 87 |
self.device.shell( |
| 88 |
"am start-activity -W -a android.intent.action.MAIN --ez " |
| 89 |
"performancetest true -n org.mozilla.fenix/org.mozilla.fenix.App" |
| 90 |
) |
| 91 |
|
| 92 |
def create_background_tabs(self): |
| 93 |
# Add background tabs that allow us to see the impact of having background tabs open |
| 94 |
# when we do the cold applink startup test. This makes the test workload more realistic |
| 95 |
# and will also help catch regressions that affect per-open-tab startup work. |
| 96 |
for website in BACKGROUND_TABS: |
| 97 |
self.device.shell(self.nav_start_command + website) |
| 98 |
time.sleep(3) |
| 99 |
if self.test == "mobile_restore": |
| 100 |
self.load_page_to_test_startup() |
| 101 |
|
| 102 |
def get_video(self, run): |
| 103 |
self.video_name = f"vid{run}_{self.browser}.mp4" |
| 104 |
video_location = f"/sdcard/Download/{self.video_name}" |
| 105 |
|
| 106 |
# Bug 1927548 - Recording command doesn't use mozdevice shell because the mozdevice shell |
| 107 |
# outputs an adbprocess obj whose adbprocess.proc.kill() does not work when called |
| 108 |
recording = subprocess.Popen( |
| 109 |
[ |
| 110 |
"adb", |
| 111 |
"shell", |
| 112 |
"screenrecord", |
| 113 |
"--bugreport", |
| 114 |
video_location, |
| 115 |
] |
| 116 |
) |
| 117 |
|
| 118 |
# Start Profilers if enabled. |
| 119 |
self.profiler.start() |
| 120 |
|
| 121 |
if self.test == "cold_view_nav_end": |
| 122 |
self.load_page_to_test_startup() |
| 123 |
elif self.test in ["mobile_restore", "homeview_startup"]: |
| 124 |
self.open_browser_with_view_intent() |
| 125 |
|
| 126 |
# Stop Profilers if enabled. |
| 127 |
self.profiler.stop(os.environ["TESTING_DIR"], run) |
| 128 |
|
| 129 |
self.process_cpu_info(run) |
| 130 |
recording.kill() |
| 131 |
time.sleep(5) |
| 132 |
self.device.command_output( |
| 133 |
["pull", "-a", video_location, os.environ["TESTING_DIR"]] |
| 134 |
) |
| 135 |
|
| 136 |
time.sleep(4) |
| 137 |
video_location = pathlib.Path(os.environ["TESTING_DIR"], self.video_name) |
| 138 |
|
| 139 |
self.video = cv2.VideoCapture(video_location) |
| 140 |
self.width = self.video.get(cv2.CAP_PROP_FRAME_WIDTH) |
| 141 |
self.height = self.video.get(cv2.CAP_PROP_FRAME_HEIGHT) |
| 142 |
self.device.shell(f"am force-stop {self.package_name}") |
| 143 |
|
| 144 |
def get_image(self, frame_position): |
| 145 |
self.video.set(cv2.CAP_PROP_POS_FRAMES, frame_position) |
| 146 |
ret, frame = self.video.read() |
| 147 |
if not ret: |
| 148 |
raise Exception("Frame not read") |
| 149 |
# We crop out the top 100 pixels in each image as when we have --bug-report in the |
| 150 |
# screen-recording command it displays a timestamp which interferes with the image comparisons |
| 151 |
return frame[100 : int(self.height), 0 : int(self.width)] |
| 152 |
|
| 153 |
def error(self, img1, img2): |
| 154 |
h = img1.shape[0] |
| 155 |
w = img1.shape[1] |
| 156 |
diff = cv2.subtract(img1, img2) |
| 157 |
err = np.sum(diff**2) |
| 158 |
mse = err / (float(h * w)) |
| 159 |
return mse |
| 160 |
|
| 161 |
def get_page_loaded_time(self): |
| 162 |
""" |
| 163 |
Returns the index of the frame where the main image on the shopify demo page is displayed |
| 164 |
for the first time. |
| 165 |
Specifically, we find the index of the first frame whose image is within an error of 20 |
| 166 |
compared to the final frame, via binary search. The binary search assumes that the error |
| 167 |
compared to the final frame decreases monotonically in the captured frames. |
| 168 |
""" |
| 169 |
final_frame_index = self.video.get(cv2.CAP_PROP_FRAME_COUNT) - 1 |
| 170 |
final_frame = self.get_image(final_frame_index) |
| 171 |
|
| 172 |
lo = 0 |
| 173 |
hi = final_frame_index |
| 174 |
|
| 175 |
while lo < hi: |
| 176 |
mid = (lo + hi) // 2 |
| 177 |
diff = self.error(self.get_image(mid), final_frame) |
| 178 |
if diff <= 20: |
| 179 |
hi = mid |
| 180 |
else: |
| 181 |
lo = mid + 1 |
| 182 |
return lo |
| 183 |
|
| 184 |
def get_time_from_frame_num(self, frame_num): |
| 185 |
self.video.set(cv2.CAP_PROP_POS_FRAMES, frame_num) |
| 186 |
self.video.read() |
| 187 |
return self.video.get(cv2.CAP_PROP_POS_MSEC) |
| 188 |
|
| 189 |
def load_page_to_test_startup(self): |
| 190 |
# Navigate to the page we want to use for testing startup |
| 191 |
self.device.shell(self.nav_start_command + self.test_url) |
| 192 |
time.sleep(5) |
| 193 |
|
| 194 |
def open_browser_with_view_intent(self): |
| 195 |
self.device.shell(self.view_intent_command) |
| 196 |
time.sleep(5) |
| 197 |
|
| 198 |
def process_cpu_info(self, run): |
| 199 |
cpu_info = self.device.shell_output( |
| 200 |
f"ps -A -o name=,cpu=,time+=,%cpu= | grep {self.package_name}" |
| 201 |
).split("\n") |
| 202 |
total_time_seconds = tab_processes_time = 0 |
| 203 |
for process in cpu_info: |
| 204 |
process_name = re.search(r"([\w\d_.:]+)\s", process).group(1) |
| 205 |
time = re.search(r"\s(\d+):(\d+).(\d+)\s", process) |
| 206 |
time_seconds = ( |
| 207 |
10 * int(time.group(3)) |
| 208 |
+ 1000 * int(time.group(2)) |
| 209 |
+ 60 * 1000 * int(time.group(1)) |
| 210 |
) |
| 211 |
total_time_seconds += time_seconds |
| 212 |
if "org.mozilla.fenix:tab" in process_name: |
| 213 |
process_name = "org.mozilla.fenix:tab" |
| 214 |
|
| 215 |
if process_name not in self.cpu_data.keys(): |
| 216 |
self.cpu_data[process_name] = {} |
| 217 |
self.cpu_data[process_name]["time"] = [] |
| 218 |
|
| 219 |
if "org.mozilla.fenix:tab" == process_name: |
| 220 |
tab_processes_time += time_seconds |
| 221 |
continue |
| 222 |
self.cpu_data[process_name]["time"] += [time_seconds] |
| 223 |
|
| 224 |
if tab_processes_time: |
| 225 |
self.cpu_data["org.mozilla.fenix:tab"]["time"] += [tab_processes_time] |
| 226 |
self.cpu_data["total"]["time"] += [total_time_seconds] |
| 227 |
|
| 228 |
def perfmetrics_cpu_data_ingesting(self): |
| 229 |
for process in self.cpu_data.keys(): |
| 230 |
print( |
| 231 |
'perfMetrics: {"values": ' |
| 232 |
+ str(self.cpu_data[process]["time"]) |
| 233 |
+ ', "name": "' |
| 234 |
+ process |
| 235 |
+ '-cpu-time", "shouldAlert": true }' |
| 236 |
) |
| 237 |
|
| 238 |
|
| 239 |
if __name__ == "__main__": |
| 240 |
if len(sys.argv) != 4: |
| 241 |
raise Exception("Didn't pass the args properly :(") |
| 242 |
start_video_timestamp = [] |
| 243 |
browser = sys.argv[1] |
| 244 |
test = sys.argv[2] |
| 245 |
test_url = sys.argv[3] |
| 246 |
|
| 247 |
perfherder_names = { |
| 248 |
"cold_view_nav_end": "applink_startup", |
| 249 |
"mobile_restore": "tab_restore", |
| 250 |
"homeview_startup": "homeview_startup", |
| 251 |
} |
| 252 |
|
| 253 |
ImageObject = ImageAnalzer(browser, test, test_url) |
| 254 |
for iteration in range(ITERATIONS): |
| 255 |
ImageObject.app_setup() |
| 256 |
ImageObject.get_video(iteration) |
| 257 |
nav_done_frame = ImageObject.get_page_loaded_time() |
| 258 |
start_video_timestamp += [ImageObject.get_time_from_frame_num(nav_done_frame)] |
| 259 |
print( |
| 260 |
'perfMetrics: {"values": ' |
| 261 |
+ str(start_video_timestamp) |
| 262 |
+ ', "name": "' |
| 263 |
+ perfherder_names[test] |
| 264 |
+ '", "shouldAlert": true}' |
| 265 |
) |
| 266 |
ImageObject.perfmetrics_cpu_data_ingesting() |