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"]
72 if Conditions
.is_android(self
) or kwargs
["app"] in ANDROID_BROWSERS
:
73 self
.binary_path
= None
75 self
.binary_path
= kwargs
.get("binary") or self
.get_binary_path()
77 self
.python
= sys
.executable
79 self
.raptor_dir
= os
.path
.join(self
.topsrcdir
, "testing", "raptor")
80 self
.mozharness_dir
= os
.path
.join(self
.topsrcdir
, "testing", "mozharness")
81 self
.config_file_path
= os
.path
.join(
82 self
._topobjdir
, "testing", "raptor-in_tree_conf.json"
85 self
.virtualenv_script
= os
.path
.join(
86 self
.topsrcdir
, "third_party", "python", "virtualenv", "virtualenv.py"
88 self
.virtualenv_path
= os
.path
.join(self
._topobjdir
, "testing", "raptor-venv")
90 def make_config(self
):
94 "install-chromium-distribution",
99 "binary_path": self
.binary_path
,
100 "repo_path": self
.topsrcdir
,
101 "raptor_path": self
.raptor_dir
,
102 "obj_path": self
.topobjdir
,
103 "log_name": "raptor",
104 "virtualenv_path": self
.virtualenv_path
,
105 "pypi_url": "http://pypi.org/simple",
106 "base_work_dir": self
.mozharness_dir
,
108 "python": self
.python
,
109 "virtualenv": [self
.python
, self
.virtualenv_script
],
111 "title": socket
.gethostname(),
112 "default_actions": default_actions
,
113 "raptor_cmd_line_args": self
.raptor_args
,
115 "live_sites": self
.live_sites
,
116 "disable_perf_tuning": self
.disable_perf_tuning
,
117 "conditioned_profile": self
.conditioned_profile
,
118 "is_release_build": self
.is_release_build
,
119 "device_name": self
.device_name
,
120 "enable_marionette_trace": self
.enable_marionette_trace
,
121 "browsertime_visualmetrics": self
.browsertime_visualmetrics
,
122 "browsertime_node": self
.browsertime_node
,
123 "mozbuild_path": get_state_dir(),
127 sys
.path
.insert(0, os
.path
.join(self
.topsrcdir
, "tools", "browsertime"))
131 import mach_commands
as browsertime
133 # We don't set `browsertime_{chromedriver,geckodriver} -- those will be found by
134 # browsertime in its `node_modules` directory, which is appropriate for local builds.
135 # We don't set `browsertime_ffmpeg` yet: it will need to be on the path. There is code
136 # to configure the environment including the path in
137 # `tools/browsertime/mach_commands.py` but integrating it here will take more effort.
140 "browsertime_browsertimejs": browsertime
.browsertime_path(),
141 "browsertime_vismet_script": browsertime
.visualmetrics_path(),
145 def _get_browsertime_package():
156 return json
.load(package
)
158 def _get_browsertime_resolved():
166 ".package-lock.json",
169 return json
.load(package_lock
)["packages"][
170 "node_modules/browsertime"
172 except FileNotFoundError
:
173 # Older versions of node/npm add this metadata to package.json
174 return _get_browsertime_package()["_from"]
176 def _should_install():
177 # If ffmpeg doesn't exist in the .mozbuild directory,
178 # then we should install
179 btime_cache
= os
.path
.join(self
.config
["mozbuild_path"], "browsertime")
180 if not os
.path
.exists(btime_cache
) or not any(
181 ["ffmpeg" in cache_dir
for cache_dir
in os
.listdir(btime_cache
)]
185 # If browsertime doesn't exist, install it
186 if not os
.path
.exists(
187 self
.config
["browsertime_browsertimejs"]
188 ) or not os
.path
.exists(self
.config
["browsertime_vismet_script"]):
191 # Browsertime exists, check if it's outdated
193 os
.path
.join(self
.topsrcdir
, "tools", "browsertime", "package.json")
195 new_pkg
= json
.load(new
)
197 return not _get_browsertime_resolved().endswith(
198 new_pkg
["devDependencies"]["browsertime"]
201 def _get_browsertime_version():
202 # Returns the (version number, current commit) used
204 _get_browsertime_package()["version"],
205 _get_browsertime_resolved(),
208 # Check if browsertime scripts exist and try to install them if
210 if _should_install():
211 # TODO: Make this "integration" nicer in the near future
212 print("Missing browsertime files...attempting to install")
213 subprocess
.check_output(
215 os
.path
.join(self
.topsrcdir
, "mach"),
220 shell
="windows" in platform
.system().lower(),
222 if _should_install():
224 "Failed installation attempt. Cannot find browsertime scripts. "
225 "Run `./mach browsertime --setup --clobber` to set it up."
228 # Bug 1766112 - For the time being, we need to trigger a
229 # clean build to upgrade browsertime. This should be disabled
232 "Setting --clean to True to rebuild Python "
233 "environment for Browsertime upgrade..."
235 self
.config
["clean"] = True
237 print("Using browsertime version %s from %s" % _get_browsertime_version())
240 sys
.path
= sys
.path
[1:]
245 "initial_config_file": self
.config_file_path
,
248 def write_config(self
):
250 config_file
= open(self
.config_file_path
, "w")
251 config_file
.write(json
.dumps(self
.config
))
254 err_str
= "Error writing to Raptor Mozharness config file {0}:{1}"
255 print(err_str
.format(self
.config_file_path
, str(e
)))
258 def run_mozharness(self
):
259 sys
.path
.insert(0, self
.mozharness_dir
)
260 from mozharness
.mozilla
.testing
.raptor
import Raptor
263 config
=self
.args
["config"],
264 initial_config_file
=self
.args
["initial_config_file"],
266 return raptor_mh
.run()
269 def setup_node(command_context
):
270 """Fetch the latest node-16 binary and install it into the .mozbuild directory."""
273 from mozbuild
.artifact_commands
import artifact_toolchain
274 from mozbuild
.nodeutil
import find_node_executable
275 from packaging
.version
import Version
277 print("Setting up node for browsertime...")
278 state_dir
= get_state_dir()
279 cache_path
= os
.path
.join(state_dir
, "browsertime", "node-16")
281 def __check_for_node():
282 # Check standard locations first
283 node_exe
= find_node_executable(min_version
=Version("16.0.0"))
284 if node_exe
and (node_exe
[0] is not None):
286 if not os
.path
.exists(cache_path
):
289 # Check the browsertime-specific node location next
291 if platform
.system() == "Windows":
292 node_name
= "node.exe"
293 node_exe_path
= os
.path
.join(
300 node_exe_path
= os
.path
.join(
308 node_exe
= os
.path
.join(node_exe_path
, node_name
)
309 if not os
.path
.exists(node_exe
):
314 node_exe
= __check_for_node()
316 toolchain_job
= "{}-node-16"
317 plat
= platform
.system()
318 if plat
== "Windows":
319 toolchain_job
= toolchain_job
.format("win64")
320 elif plat
== "Darwin":
321 if platform
.processor() == "arm":
322 toolchain_job
= toolchain_job
.format("macosx64-aarch64")
324 toolchain_job
= toolchain_job
.format("macosx64")
326 toolchain_job
= toolchain_job
.format("linux64")
329 "Downloading Node v16 from Taskcluster toolchain {}...".format(
334 if not os
.path
.exists(cache_path
):
335 os
.makedirs(cache_path
, exist_ok
=True)
337 # Change directories to where node should be installed
338 # before installing. Otherwise, it gets installed in the
339 # top level of the repo (or the current working directory).
340 cur_dir
= os
.getcwd()
345 from_build
=[toolchain_job
],
348 cache_dir
=cache_path
,
352 node_exe
= __check_for_node()
354 raise Exception("Could not find Node v16 binary for Raptor-Browsertime")
356 print("Finished downloading Node v16 from Taskcluster")
358 print("Node v16+ found at: %s" % node_exe
)
363 sys
.path
.insert(0, HERE
) # allow to import the raptor package
364 from raptor
.cmdline
import create_parser
366 return create_parser(mach_interface
=True)
372 description
="Run Raptor performance tests.",
373 parser
=create_parser
,
375 def run_raptor(command_context
, **kwargs
):
376 build_obj
= command_context
378 # Setup node for browsertime
379 kwargs
["browsertime_node"] = setup_node(command_context
)
381 is_android
= Conditions
.is_android(build_obj
) or kwargs
["app"] in ANDROID_BROWSERS
384 from mozrunner
.devices
.android_device
import (
386 verify_android_device
,
390 InstallIntent
.NO
if kwargs
.pop("noinstall", False) else InstallIntent
.YES
394 kwargs
.get("log_mach_verbose")
395 or kwargs
.get("log_tbpl_level") == "debug"
396 or kwargs
.get("log_mach_level") == "debug"
397 or kwargs
.get("log_raw_level") == "debug"
400 if not verify_android_device(
403 app
=kwargs
["binary"],
406 ): # Equivalent to 'run_local' = True.
408 "****************************************************************************"
411 "Unable to verify device, please check your attached/connected android device"
414 "****************************************************************************"
417 # Disable fission until geckoview supports fission by default.
418 # Need fission on Android? Use '--setpref fission.autostart=true'
419 kwargs
["fission"] = False
421 # Remove mach global arguments from sys.argv to prevent them
422 # from being consumed by raptor. Treat any item in sys.argv
423 # occuring before "raptor" as a mach global argument.
429 if arg
.startswith("raptor"):
432 raptor
= command_context
._spawn
(RaptorRunner
)
435 return raptor
.run_test(argv
, kwargs
)
436 except BinaryNotFoundException
as e
:
438 logging
.ERROR
, "raptor", {"error": str(e
)}, "ERROR: {error}"
440 command_context
.log(logging
.INFO
, "raptor", {"help": e
.help()}, "{help}")
442 except Exception as e
:
450 description
="Run Raptor performance tests.",
451 parser
=create_parser
,
453 def run_raptor_test(command_context
, **kwargs
):
454 return run_raptor(command_context
, **kwargs
)
457 class PythonVersionException(Exception):