Debian Codegraph @

testing/performance/hooks_recording.py

raw
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 json
5 import os
6 import platform
7 from pathlib import Path
8
9 from mozperftest.test.browsertime import add_option, add_options
10
11 # Uncomment the manual_login line if you need to do a manual login.
12 # The extra browsertime arguments get overwritten below so they
13 # need to be set here. The value is the time you need to do a login.
14 options = [
15 ("pageCompleteWaitTime", "10000"),
16 # ("browsertime.manual_login", 100000),
17 ]
18
19 next_site = None
20
21 RECORDING_LIST = Path(Path(__file__).parent, "pageload_sites.json")
22
23 # Uncomment this to record tp7 desktop sites in CI or locally.
24 # This is still a WIP (Bug 1831310) and meant to be used by the
25 # perftest team.
26 # RECORDING_LIST = Path(Path(__file__).parent, "tp7_desktop_sites.json")
27
28 SCM_1_LOGIN_SITES = ("facebook", "netflix")
29
30
31 def before_iterations(kw):
32 global next_site
33 print("Setting up next site to record.")
34
35 with RECORDING_LIST.open() as f:
36 site_list = json.load(f)
37 # currently can't record websites that require user interactions(Logins)
38 if kw.get("android"):
39 site_list = site_list["mobile"]
40 else:
41 site_list = site_list["desktop"]
42
43 def __should_record(test):
44 # If a test page selection was provided, only select those
45 # tests and exclude all the others
46 specified_tests = kw["proxy_perftest_page"]
47 if specified_tests is not None:
48 if test.get("name") == specified_tests:
49 if test.get("login"):
50 print(f"WARNING: You selected a login test: {test.get('name')}")
51 return True
52 else:
53 return False
54
55 # Only perform login recordings in automation or when
56 # RAPTOR_LOGINS is defined
57 record = False
58 if not test.get("login") or test.get("login-test"):
59 record = True
60 if not (
61 "MOZ_AUTOMATION" in os.environ or "RAPTOR_LOGINS" in os.environ
62 ) and test.get("login-test"):
63 record = False
64 print(
65 f"Skipping login test `{test.get('name')}` "
66 f"because login info cannot be obtained."
67 )
68
69 # When pushing to Try, only attempt login recording using the
70 # taskcluster secrets that are associated with SCM level 1 as defined
71 # in `SCM_LVL_1_SITES`.
72 if test.get("login"):
73 if "MOZ_AUTOMATION" in os.environ.keys():
74 if (
75 os.environ.get("MOZ_SCM_LEVEL") == 1
76 and test.get("name") not in SCM_1_LOGIN_SITES
77 ):
78 print(
79 f"Skipping login test `{test.get('name')}` "
80 f"Because SCM = `{os.environ.get('MOZ_SCM_LEVEL') }`"
81 f"and there is no secret available at this level"
82 )
83 return False
84 return True
85 elif "RAPTOR_LOGINS" in os.environ:
86 # Leave it to the user to have properly set up a local json file with
87 # the login websites of interest
88 return True
89
90 return record
91
92 sites = [test_site for test_site in site_list if __should_record(test_site)]
93
94 if not sites:
95 raise Exception("No tests were selected for recording!")
96
97 def next_site():
98 for site in sites:
99 yield site
100
101 next_site = next_site()
102
103 # Set the number of test-iterations to the number of builds
104 kw["test_iterations"] = len(sites)
105 return kw
106
107
108 def before_runs(env):
109 global next_site
110 print("Running before_runs")
111 add_options(env, options)
112
113 if next_site:
114 test_site = next(next_site)
115 print("Next site: %s" % test_site)
116
117 if env.get_arg("android"):
118 platform_name = "android"
119 app_name = env.get_arg("android-app-name").split(".")[-1]
120 else:
121 platform_name = platform.system().lower()
122 app_name = "firefox"
123
124 # bug 1883701 linux uses a different version for now
125 name = [
126 "mitm8" if platform_name == "linux" else "mitm11",
127 platform_name,
128 "gve" if app_name == "geckoview_example" else app_name,
129 test_site["name"],
130 ]
131
132 recording_file = "%s.zip" % "-".join(name)
133
134 env.set_arg("proxy-mode", "record")
135 env.set_arg(
136 "proxy-file",
137 recording_file,
138 )
139
140 add_options(env, options, overwrite=True)
141 add_option(env, "browsertime.url", test_site.get("test_url"))
142 add_option(env, "browsertime.screenshot", "true")
143 add_option(env, "browsertime.testName", test_site.get("name"))
144 add_option(env, "browsertime.testType", test_site.get("type", "pageload"))
145 add_option(
146 env, "browsertime.login", "true" if test_site.get("login") else "false"
147 )
148
149 prefs = test_site.get("preferences", {})
150 for pref, val in prefs.items():
151 add_option(env, "firefox.preference", f"{pref}:{val}")
152
153 # Add prefs that will attempt to remove cookie banners
154 add_option(
155 env, "firefox.preference", "cookiebanners.bannerClicking.enabled:true"
156 )
157 add_option(env, "firefox.preference", "cookiebanners.service.mode:2")
158
159 second_url = test_site.get("secondary_url", None)
160 if second_url:
161 add_option(env, "browsertime.secondary_url", second_url)
162
163 inject_deterministic = test_site.get("inject_deterministic", True)
164 env.set_arg("proxy-deterministic", inject_deterministic)
165
166 dismiss_cookie_prompt = test_site.get("dismiss_cookie_prompt", [])
167 if dismiss_cookie_prompt:
168 parsed_cmds = [
169 ":::".join([str(i) for i in item])
170 for item in dismiss_cookie_prompt
171 if item
172 ]
173 add_option(
174 env, "browsertime.dismiss_cookie_prompt", ";;;".join(parsed_cmds)
175 )
176
177 cmds = test_site.get("test_cmds", [])
178 if cmds:
179 parsed_cmds = [":::".join([str(i) for i in item]) for item in cmds if item]
180 add_option(env, "browsertime.commands", ";;;".join(parsed_cmds))
181
182 print("Recording %s to file: %s" % (test_site.get("test_url"), recording_file))