# Test teardown method. with mock.patch.object(SimpleperfProfiler, "_symbolicate", return_value=None):
profiler.teardown()
# Verify that profile and binary files were removed.
cleanup_command = "rm -f /data/local/tmp/perf.data /data/local/tmp/simpleperf" assert cleanup_command in profiler.device.commands
# Make sure $MOZPERFTEST_SIMPLEPERF is undefined. assert"MOZPERFTEST_SIMPLEPERF"notin os.environ
# Verify binary was pushed to device properly.
mock_exists.assert_called_once_with(
custom_path / "bin" / "android" / "arm64" / "simpleperf"
)
@mock.patch("mozperftest.system.simpleperf.get_adb_device_or_emu", new=FakeDevice)
@mock.patch("os.path.exists", return_value=True) def test_simpleperf_setup_without_path(mock_exists): """Test setup_simpleperf_path when no path is provided and NDK needs to be installed."""
mach_cmd, metadata, env = running_env(
app="fenix", tests=[str(EXAMPLE_SHELL_TEST)], output=None
)
profiler = SimpleperfProfiler(env, mach_cmd)
# Setup mocks for the imports inside the method
mock_platform = mock.MagicMock()
mock_platform.system.return_value = "Linux"
mock_platform.machine.return_value = "x86_64"
# Mock the imports that happen with mock.patch.dict( "sys.modules", {"platform": mock_platform, "mozboot.android": mock_android}
): # Call the method directly
profiler.setup_simpleperf_path()
# Verify Android NDK was installed
mock_android.ensure_android_ndk.assert_called_once_with("linux")
# Verify simpleperf path was set correctly.
expected_path = mock_ndk / "simpleperf" assert profiler.get_arg("path") == expected_path
# Test start with default options
controller.start(None)
# Verify subprocess.Popen was called with su, proper paths and the default args.
mock_popen.assert_called_once_with(
[ "adb", "shell", "su", "-c",
f"/data/local/tmp/simpleperf record {DEFAULT_SIMPLEPERF_OPTS} -o /data/local/tmp/perf.data",
],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
)
# Verify profiler_process was set assert controller.profiler_process == mock_process
@mock.patch("mozperftest.system.simpleperf.get_adb_device_or_emu", new=FakeDevice)
@mock.patch("mozperftest.system.simpleperf.subprocess.Popen")
@mock.patch( "mozperftest.system.simpleperf.SimpleperfProfiler.is_enabled", return_value=True
) def test_simpleperf_controller_start_custom_options(mock_is_enabled, mock_popen): """Test that SimpleperfController.start() works with custom options."""
mock_process = MockProcess()
mock_popen.return_value = mock_process
controller = SimpleperfController()
custom_opts = "some random options here."
# Test start()
controller.start(custom_opts)
# Verify the correct arguments are used
mock_popen.assert_called_once_with(
[ "adb", "shell", "su", "-c",
f"/data/local/tmp/simpleperf record {custom_opts} -o /data/local/tmp/perf.data",
],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
) assert controller.profiler_process == mock_process
@mock.patch("mozperftest.system.simpleperf.get_adb_device_or_emu", new=FakeDevice)
@mock.patch("mozperftest.system.simpleperf.Path")
@mock.patch( "mozperftest.system.simpleperf.SimpleperfProfiler.is_enabled", return_value=True
) def test_simpleperf_controller_stop(mock_is_enabled, mock_path): """Test that the SimpleperfController.stop() method works correctly."""
mock_process = MockProcess()
# Invalid inputs should throw SimpleperfSymbolicationError exception with pytest.raises(SimpleperfSymbolicationError):
profiler._validate_symbolication_paths( "/fake/symbol/path", "/fake/symbolicator/path"
)
with pytest.raises(SimpleperfSymbolicationError):
profiler._validate_symbolication_paths("", "")
with pytest.raises(SimpleperfSymbolicationError):
profiler._validate_symbolication_paths(None, None)
# Verify local symbolication is skipped if args not provided with mock.patch.object(profiler, "_cleanup") as mock_cleanup, mock.patch( "mozperftest.system.simpleperf.ON_TRY", False
):
profiler.teardown()
mock_cleanup.assert_called_once()
# Check if exception was thrown and handled by verifying that # breakpad_symbol_dir and profiler_edit_dir do not exist assertnot hasattr(profiler, "breakpad_symbol_dir") assertnot hasattr(profiler, "profiler_node_tools_dir")
# Verify local symbolication is skipped if args are invalid with mock.patch.object(profiler, "_cleanup") as mock_cleanup, mock.patch( "mozperftest.system.simpleperf.ON_TRY", False
):
profiler.teardown()
mock_cleanup.assert_called_once()
# Test timeout error in local run with mock.patch("mozperftest.system.simpleperf.ON_TRY", False), mock.patch( "tempfile.mkdtemp", return_value=str(mock_work_dir_path)
), mock.patch("shutil.rmtree") as mock_rmtree, mock.patch( "subprocess.Popen"
) as mock_popen, mock.patch( "mozperftest.system.simpleperf.find_node_executable",
return_value=[str(node_path)],
), mock.patch.object(profiler, "_cleanup") as mock_cleanup: # Mock processes
import_process = make_mock_process(context=True)
# Check if timeout error has been thrown and caught by checking if # subsequent profiler-edit call has not occured. assert expected_profiler_edit notin mock_popen.call_args_list
# Check for clean exit
mock_rmtree.assert_not_called()
mock_cleanup.assert_called_once()
# Mock .zip file with a symbol file
mock_sym_zip_file = mock_fetch_path / "target.crashreporter-symbols.zip" with zipfile.ZipFile(mock_sym_zip_file, "w") as mock_zip:
mock_zip.writestr("libxul.so/ABCD1234/libxul.so.sym", "some_data")
# Mock tar file with a perf data file
mock_perf_data = output_dir / "unit_test.tgz" with tarfile.open(mock_perf_data, "w:gz") as tar:
perf_path = tmp_path / "mock_perf-0.data"
perf_path.write_text("mock-data")
tar.add(perf_path, arcname="unit_test/simpleperf/mock_perf-0.data")
# Set env and metadata
profiler.env.set_arg("output", output_dir)
profiler.test_name = "unit_test"
# Test symbolication in CI with mock.patch.dict(
os.environ,
{ "MOZ_FETCHES_DIR": str(mock_fetch_path),
},
clear=False,
), mock.patch("mozperftest.system.simpleperf.ON_TRY", True), mock.patch( "mozperftest.utils.ON_TRY", True
), mock.patch("tempfile.mkdtemp", return_value=str(mock_work_dir_path)), mock.patch( "shutil.rmtree"
) as mock_rmtree, mock.patch("subprocess.Popen") as mock_popen: # Mock processes
import_process = make_mock_process(context=True)
# Mock .zip file with a symbol file
mock_sym_zip_file = mock_fetch_path / "target.crashreporter-symbols.zip" with zipfile.ZipFile(mock_sym_zip_file, "w") as mock_zip:
mock_zip.writestr("libxul.so/ABCD1234/libxul.so.sym", "some_data")
# Mock tar file with a perf data file
mock_perf_data = output_dir / "unit_test.tgz" with tarfile.open(mock_perf_data, "w:gz") as tar:
perf_path = tmp_path / "mock_perf-0.data"
perf_path.write_text("mock-data")
tar.add(perf_path, arcname="unit_test/simpleperf/mock_perf-0.data")
# Set env and metadata
profiler.env.set_arg("output", output_dir)
profiler.test_name = "unit_test"
# Test timeout error in CI with mock.patch("mozperftest.system.simpleperf.ON_TRY", True), mock.patch( "mozperftest.utils.ON_TRY", True
), mock.patch( "tempfile.mkdtemp", return_value=str(mock_work_dir_path)
), mock.patch.dict(
os.environ,
{ "MOZ_FETCHES_DIR": str(mock_fetch_path),
},
clear=False,
), mock.patch("shutil.rmtree") as mock_rmtree, mock.patch( "subprocess.Popen"
) as mock_popen, mock.patch( "mozperftest.system.simpleperf.find_node_executable",
return_value=[str(node_path)],
), mock.patch.object(profiler, "_cleanup") as mock_cleanup: # Mock processes
import_process = make_mock_process(context=True)
# Check if timeout error has been thrown and caught by checking if # subsequent profiler-edit call has not occured. assert expected_profiler_edit notin mock_popen.call_args_list
# Check for clean exit
mock_rmtree.assert_called_once()
mock_cleanup.assert_called_once()
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.