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/.
5 # Originally taken from /talos/mach_commands.py
7 # Integrates raptor mozharness with mach
16 from mach
.decorators
import Command
17 from mach
.util
import get_state_dir
18 from mozbuild
.base
import BinaryNotFoundException
, MozbuildObject
19 from mozbuild
.base
import MachCommandConditions
as Conditions
21 HERE
= os
.path
.dirname(os
.path
.realpath(__file__
))
23 ANDROID_BROWSERS
= ["geckoview", "refbrow", "fenix", "chrome-m", "cstm-car-m"]
26 class RaptorRunner(MozbuildObject
):
27 def run_test(self
, raptor_args
, kwargs
):
28 """Setup and run mozharness.
30 We want to do a few things before running Raptor:
33 2. Make the config for Raptor mozharness
36 # Validate that the user is using a supported python version before doing anything else
37 max_py_major
, max_py_minor
= 3, 11
38 sys_maj
, sys_min
= sys
.version_info
.major
, sys
.version_info
.minor
39 if sys_min
> max_py_minor
:
40 raise PythonVersionException(
42 f
"\tPlease downgrade your Python version as Raptor does not yet support Python "
43 f
"versions greater than {max_py_major}.{max_py_minor}."
44 f
"\n\tYou seem to currently be using Python {sys_maj}.{sys_min}."
45 f
"\n\tSee here for a possible solution in debugging your python environment: "
46 f
"https://firefox-source-docs.mozilla.org/testing/perfdocs/"
47 f
"debugging.html#debugging-local-python-environment"
50 self
.init_variables(raptor_args
, kwargs
)
54 return self
.run_mozharness()
56 def init_variables(self
, raptor_args
, kwargs
):
57 self
.raptor_args
= raptor_args
59 if kwargs
.get("host") == "HOST_IP":
60 kwargs
["host"] = os
.environ
["HOST_IP"]
61 self
.host
= kwargs
["host"]
62 self
.is_release_build
= kwargs
["is_release_build"]
63 self
.live_sites
= kwargs
["live_sites"]
64 self
.disable_perf_tuning
= kwargs
["disable_perf_tuning"]
65 self
.conditioned_profile
= kwargs
["conditioned_profile"]
66 self
.device_name
= kwargs
["device_name"]
67 self
.enable_marionette_trace
= kwargs
["enable_marionette_trace"]
68 self
.browsertime_visualmetrics
= kwargs
["browsertime_visualmetrics"]
69 self
.browsertime_node
= kwargs
["browsertime_node"]
70 self
.clean
= kwargs
["clean"]
71 self
.screenshot_on_failure
= kwargs
["screenshot_on_failure"]
73 if Conditions
.is_android(self
) or kwargs
["app"] in ANDROID_BROWSERS
:
74 self
.binary_path
= None
76 self
.binary_path
= kwargs
.get("binary") or self
.get_binary_path()
78 self
.python
= sys
.executable
80 self
.raptor_dir
= os
.path
.join(self
.topsrcdir
, "testing", "raptor")
81 self
.mozharness_dir
= os
.path
.join(self
.topsrcdir
, "testing", "mozharness")
82 self
.config_file_path
= os
.path
.join(
83 self
._topobjdir
, "testing", "raptor-in_tree_conf.json"
86 self
.virtualenv_script
= os
.path
.join(
87 self
.topsrcdir
, "third_party", "python", "virtualenv", "virtualenv.py"
89 self
.virtualenv_path
= os
.path
.join(self
._topobjdir
, "testing", "raptor-venv")
91 def make_config(self
):
95 "install-chromium-distribution",
100 "binary_path": self
.binary_path
,
101 "repo_path": self
.topsrcdir
,
102 "raptor_path": self
.raptor_dir
,
103 "obj_path": self
.topobjdir
,
104 "log_name": "raptor",
105 "virtualenv_path": self
.virtualenv_path
,
106 "pypi_url": "http://pypi.org/simple",
107 "base_work_dir": self
.mozharness_dir
,
109 "python": self
.python
,
110 "virtualenv": [self
.python
, self
.virtualenv_script
],
112 "title": socket
.gethostname(),
113 "default_actions": default_actions
,
114 "raptor_cmd_line_args": self
.raptor_args
,
116 "live_sites": self
.live_sites
,
117 "disable_perf_tuning": self
.disable_perf_tuning
,
118 "conditioned_profile": self
.conditioned_profile
,
119 "is_release_build": self
.is_release_build
,
120 "device_name": self
.device_name
,
121 "enable_marionette_trace": self
.enable_marionette_trace
,
122 "browsertime_visualmetrics": self
.browsertime_visualmetrics
,
123 "browsertime_node": self
.browsertime_node
,
124 "mozbuild_path": get_state_dir(),
126 "screenshot_on_failure": self
.screenshot_on_failure
,
129 sys
.path
.insert(0, os
.path
.join(self
.topsrcdir
, "tools", "browsertime"))
133 import mach_commands
as browsertime
135 # We don't set `browsertime_{chromedriver,geckodriver} -- those will be found by
136 # browsertime in its `node_modules` directory, which is appropriate for local builds.
137 # We don't set `browsertime_ffmpeg` yet: it will need to be on the path. There is code
138 # to configure the environment including the path in
139 # `tools/browsertime/mach_commands.py` but integrating it here will take more effort.
142 "browsertime_browsertimejs": browsertime
.browsertime_path(),
143 "browsertime_vismet_script": browsertime
.visualmetrics_path(),
147 def _get_browsertime_package():
158 return json
.load(package
)
160 def _get_browsertime_resolved():
168 ".package-lock.json",
171 return json
.load(package_lock
)["packages"][
172 "node_modules/browsertime"
174 except FileNotFoundError
:
175 # Older versions of node/npm add this metadata to package.json
176 return _get_browsertime_package()["_from"]
178 def _should_install():
179 # If ffmpeg doesn't exist in the .mozbuild directory,
180 # then we should install
181 btime_cache
= os
.path
.join(self
.config
["mozbuild_path"], "browsertime")
182 if not os
.path
.exists(btime_cache
) or not any(
183 ["ffmpeg" in cache_dir
for cache_dir
in os
.listdir(btime_cache
)]
187 # If browsertime doesn't exist, install it
188 if not os
.path
.exists(
189 self
.config
["browsertime_browsertimejs"]
190 ) or not os
.path
.exists(self
.config
["browsertime_vismet_script"]):
193 # Browsertime exists, check if it's outdated
195 os
.path
.join(self
.topsrcdir
, "tools", "browsertime", "package.json")
197 new_pkg
= json
.load(new
)
199 return not _get_browsertime_resolved().endswith(
200 new_pkg
["devDependencies"]["browsertime"]
203 def _get_browsertime_version():
204 # Returns the (version number, current commit) used
206 _get_browsertime_package()["version"],
207 _get_browsertime_resolved(),
210 # Check if browsertime scripts exist and try to install them if
212 if _should_install():
213 # TODO: Make this "integration" nicer in the near future
214 print("Missing browsertime files...attempting to install")
215 subprocess
.check_output(
217 os
.path
.join(self
.topsrcdir
, "mach"),
222 shell
="windows" in platform
.system().lower(),
224 if _should_install():
226 "Failed installation attempt. Cannot find browsertime scripts. "
227 "Run `./mach browsertime --setup --clobber` to set it up."
230 # Bug 1766112 - For the time being, we need to trigger a
231 # clean build to upgrade browsertime. This should be disabled
234 "Setting --clean to True to rebuild Python "
235 "environment for Browsertime upgrade..."
237 self
.config
["clean"] = True
239 print("Using browsertime version %s from %s" % _get_browsertime_version())
242 sys
.path
= sys
.path
[1:]
247 "initial_config_file": self
.config_file_path
,
250 def write_config(self
):
252 config_file
= open(self
.config_file_path
, "w")
253 config_file
.write(json
.dumps(self
.config
))
256 err_str
= "Error writing to Raptor Mozharness config file {0}:{1}"
257 print(err_str
.format(self
.config_file_path
, str(e
)))
260 def run_mozharness(self
):
261 sys
.path
.insert(0, self
.mozharness_dir
)
262 from mozharness
.mozilla
.testing
.raptor
import Raptor
265 config
=self
.args
["config"],
266 initial_config_file
=self
.args
["initial_config_file"],
268 return raptor_mh
.run()
271 def setup_node(command_context
):
272 """Fetch the latest node-18 binary and install it into the .mozbuild directory."""
275 from mozbuild
.artifact_commands
import artifact_toolchain
276 from mozbuild
.nodeutil
import find_node_executable
277 from packaging
.version
import Version
279 print("Setting up node for browsertime...")
280 state_dir
= get_state_dir()
281 cache_path
= os
.path
.join(state_dir
, "browsertime", "node-18")
283 def __check_for_node():
284 # Check standard locations first
285 node_exe
= find_node_executable(min_version
=Version("18.0.0"))
286 if node_exe
and (node_exe
[0] is not None):
288 if not os
.path
.exists(cache_path
):
291 # Check the browsertime-specific node location next
293 if platform
.system() == "Windows":
294 node_name
= "node.exe"
295 node_exe_path
= os
.path
.join(
302 node_exe_path
= os
.path
.join(
310 node_exe
= os
.path
.join(node_exe_path
, node_name
)
311 if not os
.path
.exists(node_exe
):
316 node_exe
= __check_for_node()
318 toolchain_job
= "{}-node-18"
319 plat
= platform
.system()
320 if plat
== "Windows":
321 toolchain_job
= toolchain_job
.format("win64")
322 elif plat
== "Darwin":
323 if platform
.processor() == "arm":
324 toolchain_job
= toolchain_job
.format("macosx64-aarch64")
326 toolchain_job
= toolchain_job
.format("macosx64")
328 toolchain_job
= toolchain_job
.format("linux64")
331 "Downloading Node v18 from Taskcluster toolchain {}...".format(
336 if not os
.path
.exists(cache_path
):
337 os
.makedirs(cache_path
, exist_ok
=True)
339 # Change directories to where node should be installed
340 # before installing. Otherwise, it gets installed in the
341 # top level of the repo (or the current working directory).
342 cur_dir
= os
.getcwd()
347 from_build
=[toolchain_job
],
350 cache_dir
=cache_path
,
354 node_exe
= __check_for_node()
356 raise Exception("Could not find Node v18 binary for Raptor-Browsertime")
358 print("Finished downloading Node v18 from Taskcluster")
360 print("Node v18+ found at: %s" % node_exe
)
365 sys
.path
.insert(0, HERE
) # allow to import the raptor package
366 from raptor
.cmdline
import create_parser
368 return create_parser(mach_interface
=True)
374 description
="Run Raptor performance tests.",
375 parser
=create_parser
,
377 def run_raptor(command_context
, **kwargs
):
378 build_obj
= command_context
380 # Setup node for browsertime
381 kwargs
["browsertime_node"] = setup_node(command_context
)
383 is_android
= Conditions
.is_android(build_obj
) or kwargs
["app"] in ANDROID_BROWSERS
386 from mozrunner
.devices
.android_device
import (
388 verify_android_device
,
392 InstallIntent
.NO
if kwargs
.pop("noinstall", False) else InstallIntent
.YES
396 kwargs
.get("log_mach_verbose")
397 or kwargs
.get("log_tbpl_level") == "debug"
398 or kwargs
.get("log_mach_level") == "debug"
399 or kwargs
.get("log_raw_level") == "debug"
402 if not verify_android_device(
405 app
=kwargs
["binary"],
408 ): # Equivalent to 'run_local' = True.
410 "****************************************************************************"
413 "Unable to verify device, please check your attached/connected android device"
416 "****************************************************************************"
419 # Disable fission until geckoview supports fission by default.
420 # Need fission on Android? Use '--setpref fission.autostart=true'
421 kwargs
["fission"] = False
423 # Remove mach global arguments from sys.argv to prevent them
424 # from being consumed by raptor. Treat any item in sys.argv
425 # occuring before "raptor" as a mach global argument.
431 if arg
.startswith("raptor"):
434 raptor
= command_context
._spawn
(RaptorRunner
)
437 return raptor
.run_test(argv
, kwargs
)
438 except BinaryNotFoundException
as e
:
440 logging
.ERROR
, "raptor", {"error": str(e
)}, "ERROR: {error}"
442 command_context
.log(logging
.INFO
, "raptor", {"help": e
.help()}, "{help}")
444 except Exception as e
:
452 description
="Run Raptor performance tests.",
453 parser
=create_parser
,
455 def run_raptor_test(command_context
, **kwargs
):
456 return run_raptor(command_context
, **kwargs
)
459 class PythonVersionException(Exception):