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"]
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, 10
38 sys_maj
, sys_min
= sys
.version_info
.major
, sys
.version_info
.minor
39 if sys_min
> max_py_minor
:
40 raise PythonVersionException(
41 f
"Please downgrade your Python version as Raptor does not yet support Python versions greater than {max_py_major}.{max_py_minor}. "
42 f
"You seem to currently be using Python {sys_maj}.{sys_min}"
44 self
.init_variables(raptor_args
, kwargs
)
48 return self
.run_mozharness()
50 def init_variables(self
, raptor_args
, kwargs
):
51 self
.raptor_args
= raptor_args
53 if kwargs
.get("host") == "HOST_IP":
54 kwargs
["host"] = os
.environ
["HOST_IP"]
55 self
.host
= kwargs
["host"]
56 self
.is_release_build
= kwargs
["is_release_build"]
57 self
.live_sites
= kwargs
["live_sites"]
58 self
.disable_perf_tuning
= kwargs
["disable_perf_tuning"]
59 self
.conditioned_profile
= kwargs
["conditioned_profile"]
60 self
.device_name
= kwargs
["device_name"]
61 self
.enable_marionette_trace
= kwargs
["enable_marionette_trace"]
62 self
.browsertime_visualmetrics
= kwargs
["browsertime_visualmetrics"]
63 self
.browsertime_node
= kwargs
["browsertime_node"]
64 self
.clean
= kwargs
["clean"]
66 if Conditions
.is_android(self
) or kwargs
["app"] in ANDROID_BROWSERS
:
67 self
.binary_path
= None
69 self
.binary_path
= kwargs
.get("binary") or self
.get_binary_path()
71 self
.python
= sys
.executable
73 self
.raptor_dir
= os
.path
.join(self
.topsrcdir
, "testing", "raptor")
74 self
.mozharness_dir
= os
.path
.join(self
.topsrcdir
, "testing", "mozharness")
75 self
.config_file_path
= os
.path
.join(
76 self
._topobjdir
, "testing", "raptor-in_tree_conf.json"
79 self
.virtualenv_script
= os
.path
.join(
80 self
.topsrcdir
, "third_party", "python", "virtualenv", "virtualenv.py"
82 self
.virtualenv_path
= os
.path
.join(self
._topobjdir
, "testing", "raptor-venv")
84 def make_config(self
):
88 "install-chromium-distribution",
93 "binary_path": self
.binary_path
,
94 "repo_path": self
.topsrcdir
,
95 "raptor_path": self
.raptor_dir
,
96 "obj_path": self
.topobjdir
,
98 "virtualenv_path": self
.virtualenv_path
,
99 "pypi_url": "http://pypi.org/simple",
100 "base_work_dir": self
.mozharness_dir
,
102 "python": self
.python
,
103 "virtualenv": [self
.python
, self
.virtualenv_script
],
105 "title": socket
.gethostname(),
106 "default_actions": default_actions
,
107 "raptor_cmd_line_args": self
.raptor_args
,
109 "live_sites": self
.live_sites
,
110 "disable_perf_tuning": self
.disable_perf_tuning
,
111 "conditioned_profile": self
.conditioned_profile
,
112 "is_release_build": self
.is_release_build
,
113 "device_name": self
.device_name
,
114 "enable_marionette_trace": self
.enable_marionette_trace
,
115 "browsertime_visualmetrics": self
.browsertime_visualmetrics
,
116 "browsertime_node": self
.browsertime_node
,
117 "mozbuild_path": get_state_dir(),
121 sys
.path
.insert(0, os
.path
.join(self
.topsrcdir
, "tools", "browsertime"))
125 import mach_commands
as browsertime
127 # We don't set `browsertime_{chromedriver,geckodriver} -- those will be found by
128 # browsertime in its `node_modules` directory, which is appropriate for local builds.
129 # We don't set `browsertime_ffmpeg` yet: it will need to be on the path. There is code
130 # to configure the environment including the path in
131 # `tools/browsertime/mach_commands.py` but integrating it here will take more effort.
134 "browsertime_browsertimejs": browsertime
.browsertime_path(),
135 "browsertime_vismet_script": browsertime
.visualmetrics_path(),
139 def _get_browsertime_package():
150 return json
.load(package
)
152 def _get_browsertime_resolved():
160 ".package-lock.json",
163 return json
.load(package_lock
)["packages"][
164 "node_modules/browsertime"
166 except FileNotFoundError
:
167 # Older versions of node/npm add this metadata to package.json
168 return _get_browsertime_package()["_from"]
170 def _should_install():
171 # If ffmpeg doesn't exist in the .mozbuild directory,
172 # then we should install
173 btime_cache
= os
.path
.join(self
.config
["mozbuild_path"], "browsertime")
174 if not os
.path
.exists(btime_cache
) or not any(
175 ["ffmpeg" in cache_dir
for cache_dir
in os
.listdir(btime_cache
)]
179 # If browsertime doesn't exist, install it
180 if not os
.path
.exists(
181 self
.config
["browsertime_browsertimejs"]
182 ) or not os
.path
.exists(self
.config
["browsertime_vismet_script"]):
185 # Browsertime exists, check if it's outdated
187 os
.path
.join(self
.topsrcdir
, "tools", "browsertime", "package.json")
189 new_pkg
= json
.load(new
)
191 return not _get_browsertime_resolved().endswith(
192 new_pkg
["devDependencies"]["browsertime"]
195 def _get_browsertime_version():
196 # Returns the (version number, current commit) used
198 _get_browsertime_package()["version"],
199 _get_browsertime_resolved(),
202 # Check if browsertime scripts exist and try to install them if
204 if _should_install():
205 # TODO: Make this "integration" nicer in the near future
206 print("Missing browsertime files...attempting to install")
207 subprocess
.check_output(
209 os
.path
.join(self
.topsrcdir
, "mach"),
214 shell
="windows" in platform
.system().lower(),
216 if _should_install():
218 "Failed installation attempt. Cannot find browsertime scripts. "
219 "Run `./mach browsertime --setup --clobber` to set it up."
222 # Bug 1766112 - For the time being, we need to trigger a
223 # clean build to upgrade browsertime. This should be disabled
226 "Setting --clean to True to rebuild Python "
227 "environment for Browsertime upgrade..."
229 self
.config
["clean"] = True
231 print("Using browsertime version %s from %s" % _get_browsertime_version())
234 sys
.path
= sys
.path
[1:]
239 "initial_config_file": self
.config_file_path
,
242 def write_config(self
):
244 config_file
= open(self
.config_file_path
, "w")
245 config_file
.write(json
.dumps(self
.config
))
248 err_str
= "Error writing to Raptor Mozharness config file {0}:{1}"
249 print(err_str
.format(self
.config_file_path
, str(e
)))
252 def run_mozharness(self
):
253 sys
.path
.insert(0, self
.mozharness_dir
)
254 from mozharness
.mozilla
.testing
.raptor
import Raptor
257 config
=self
.args
["config"],
258 initial_config_file
=self
.args
["initial_config_file"],
260 return raptor_mh
.run()
263 def setup_node(command_context
):
264 """Fetch the latest node-16 binary and install it into the .mozbuild directory."""
266 from distutils
.version
import StrictVersion
268 from mozbuild
.artifact_commands
import artifact_toolchain
269 from mozbuild
.nodeutil
import find_node_executable
271 print("Setting up node for browsertime...")
272 state_dir
= get_state_dir()
273 cache_path
= os
.path
.join(state_dir
, "browsertime", "node-16")
275 def __check_for_node():
276 # Check standard locations first
277 node_exe
= find_node_executable(min_version
=StrictVersion("16.0.0"))
278 if node_exe
and (node_exe
[0] is not None):
280 if not os
.path
.exists(cache_path
):
283 # Check the browsertime-specific node location next
285 if platform
.system() == "Windows":
286 node_name
= "node.exe"
287 node_exe_path
= os
.path
.join(
294 node_exe_path
= os
.path
.join(
302 node_exe
= os
.path
.join(node_exe_path
, node_name
)
303 if not os
.path
.exists(node_exe
):
308 node_exe
= __check_for_node()
310 toolchain_job
= "{}-node-16"
311 plat
= platform
.system()
312 if plat
== "Windows":
313 toolchain_job
= toolchain_job
.format("win64")
314 elif plat
== "Darwin":
315 toolchain_job
= toolchain_job
.format("macosx64")
317 toolchain_job
= toolchain_job
.format("linux64")
320 "Downloading Node v16 from Taskcluster toolchain {}...".format(
325 if not os
.path
.exists(cache_path
):
326 os
.makedirs(cache_path
, exist_ok
=True)
328 # Change directories to where node should be installed
329 # before installing. Otherwise, it gets installed in the
330 # top level of the repo (or the current working directory).
331 cur_dir
= os
.getcwd()
336 from_build
=[toolchain_job
],
339 cache_dir
=cache_path
,
343 node_exe
= __check_for_node()
345 raise Exception("Could not find Node v16 binary for Raptor-Browsertime")
347 print("Finished downloading Node v16 from Taskcluster")
349 print("Node v16+ found at: %s" % node_exe
)
354 sys
.path
.insert(0, HERE
) # allow to import the raptor package
355 from raptor
.cmdline
import create_parser
357 return create_parser(mach_interface
=True)
363 description
="Run Raptor performance tests.",
364 parser
=create_parser
,
366 def run_raptor(command_context
, **kwargs
):
367 build_obj
= command_context
369 # Setup node for browsertime
370 kwargs
["browsertime_node"] = setup_node(command_context
)
372 is_android
= Conditions
.is_android(build_obj
) or kwargs
["app"] in ANDROID_BROWSERS
375 from mozrunner
.devices
.android_device
import (
377 verify_android_device
,
381 InstallIntent
.NO
if kwargs
.pop("noinstall", False) else InstallIntent
.YES
385 kwargs
.get("log_mach_verbose")
386 or kwargs
.get("log_tbpl_level") == "debug"
387 or kwargs
.get("log_mach_level") == "debug"
388 or kwargs
.get("log_raw_level") == "debug"
391 if not verify_android_device(
394 app
=kwargs
["binary"],
397 ): # Equivalent to 'run_local' = True.
399 # Disable fission until geckoview supports fission by default.
400 # Need fission on Android? Use '--setpref fission.autostart=true'
401 kwargs
["fission"] = False
403 # Remove mach global arguments from sys.argv to prevent them
404 # from being consumed by raptor. Treat any item in sys.argv
405 # occuring before "raptor" as a mach global argument.
411 if arg
.startswith("raptor"):
414 raptor
= command_context
._spawn
(RaptorRunner
)
417 return raptor
.run_test(argv
, kwargs
)
418 except BinaryNotFoundException
as e
:
420 logging
.ERROR
, "raptor", {"error": str(e
)}, "ERROR: {error}"
422 command_context
.log(logging
.INFO
, "raptor", {"help": e
.help()}, "{help}")
424 except Exception as e
:
432 description
="Run Raptor performance tests.",
433 parser
=create_parser
,
435 def run_raptor_test(command_context
, **kwargs
):
436 return run_raptor(command_context
, **kwargs
)
439 class PythonVersionException(Exception):