Skip to content

Debloat any Android or Google TV over Wi-Fi and get an Apple TV-style home.

tools: bench.py answers a run that never got as far as the TV with 2

xbsheng18ef0e5f2585
diff --git a/tests/test_bench_target_address.py b/tests/test_bench_target_address.py
index fa2110daf..8032c7366 100644
--- a/tests/test_bench_target_address.py
+++ b/tests/test_bench_target_address.py
@@ -16,7 +16,9 @@ both itself, and neither as tools/tv.py does:
bench.py now leaves both to tools/tv.py: `--port`/`TV_PORT` is passed on as text, and TV._connect reads
back the HOST and PORT tv.py parsed out of the environment bench.py set for it - where the split and the
1..65535 refusal already are, in tv.py's own words (`not a port number: '70000'`, which bench.py already
-reached for an out-of-range port, because importing tools/tv.py checks it).
+reached for an out-of-range port, because importing tools/tv.py checks it). That refusal comes out of the
+import before any connection, so the run measured nothing and answers 2, as it does with no address at
+all; 1 stays for the run that measured and could not write its result.json.
The tests run the real tools/tv.py against a stub adb_shell that records the host and port
`AdbDeviceTcp` was built with, so the target is observed rather than assumed; HOME points at a temporary
@@ -113,14 +115,20 @@ class BenchTargetAddressTest(unittest.TestCase):
return r.returncode, r.stdout, r.stderr, opened
def refused(self, args, tv_host="192.0.2.20", tv_port=None, value=None):
- """A port bench.py will not use: a sentence naming it, no traceback, nothing opened."""
+ """A port bench.py will not use: a sentence naming it, no traceback, nothing opened.
+
+ tools/tv.py refuses such a port while it is imported, before a connection exists, so nothing
+ was measured: bench.py answers 2, the status its docstring gives a run that never reached the
+ TV, rather than the 1 a run keeps when its numbers exist and result.json could not be written.
+ """
status, out, err, opened = self.run_bench(args, tv_host=tv_host, tv_port=tv_port)
self.assertIsNone(opened, f"nothing should have been opened: {out}{err}")
self.assertNotIn("Traceback", err, err)
self.assertIn("not a port number", err, err)
+ self.assertIn("Nothing was measured", err, err)
if value is not None:
self.assertIn(value, err, err)
- self.assertEqual(status, 1, f"{out}{err}")
+ self.assertEqual(status, 2, f"{out}{err}")
def test_a_host_with_a_port_is_split(self):
# tools/relay.md gives the TV's port with the address. This reached the client as a machine
diff --git a/tests/test_bench_unreached_tv.py b/tests/test_bench_unreached_tv.py
new file mode 100644
index 000000000..07559bf3b
--- /dev/null
+++ b/tests/test_bench_unreached_tv.py
@@ -0,0 +1,166 @@
+"""A bench.py run that never got as far as the TV answers 2, and says nothing was measured.
+
+tools/bench.py's exit status is the reader's answer to "did the numbers disappear, or was there never a
+run": 0 the run is measured and written, 1 anything else including a result.json that could not be
+written after the run, and 2 a run that never reached the TV. Three shapes still answered 1, all before a
+connection existed:
+
+- no `--host` and no `TV_HOST` - a bare `SystemExit("pass --host or set TV_HOST")`, whose text the
+ interpreter prints and whose status is 1;
+- a `--port` (or `TV_PORT`, or a `:port` in the address) that tools/tv.py cannot read - tv.py refuses it
+ while it is imported, so `import tv` leaves a SystemExit carrying "not a port number: 'abc'", again 1;
+- a checkout a reader has not built yet, with no adb_shell and no tools/venv for main() to start again
+ under - the ModuleNotFoundError handler raised a SystemExit with its sentence, again 1.
+
+1 is also what a finished run keeps when its result.json could not be written, so a run that measured
+nothing read as a run whose numbers were lost - the one thing perf-ab.md's author has to tell apart.
+
+The reader here is the checkout's own tools/tv.py, copied beside a copy of bench.py (that is where bench
+imports it from), and the stub cases pin the handler around it: a client module that stops with its own
+text (2, that text printed), one that stops with a number (2, since nothing was measured) and one that
+exits 0 (its status stands - the branch tools/theme_restart.py keeps). A TV that answered but named no
+HOME activity keeps 1: it was reached, and the docstring's 1 covers it.
+
+Run from the repository root: python3 -m unittest discover -s tests -v
+"""
+import os
+import shutil
+import subprocess
+import sys
+import tempfile
+import unittest
+from pathlib import Path
+
+ROOT = Path(__file__).resolve().parents[1]
+TOOLS = ROOT / "tools"
+NOTHING_MEASURED = "Nothing was measured, and nothing was written."
+
+
+def packages_installed():
+ """True when this interpreter can load adb_shell, which the checkout's tv.py needs to be imported."""
+ return subprocess.run([sys.executable, "-c", "import adb_shell"],
+ capture_output=True, timeout=120).returncode == 0
+
+
+class Base(unittest.TestCase):
+ def setUp(self):
+ self._tmp = tempfile.TemporaryDirectory()
+ self.addCleanup(self._tmp.cleanup)
+ self.base = Path(self._tmp.name)
+ self.tools = self.base / "tools"
+ self.tools.mkdir()
+ shutil.copy2(TOOLS / "bench.py", self.tools / "bench.py")
+ self.script = self.tools / "bench.py"
+ self.home = self.base / "home"
+ (self.home / ".android").mkdir(parents=True)
+
+ def run_bench(self, *args, python_flags=(), **environment):
+ """bench.py's own command line in the copied folder; no TV_HOST or TV_PORT from this machine."""
+ env = {k: v for k, v in os.environ.items()
+ if k not in ("TV_HOST", "TV_PORT", "PYTHONPATH")}
+ env.update(HOME=str(self.home), USERPROFILE=str(self.home))
+ env.update(environment)
+ return subprocess.run([sys.executable, *python_flags, "-I", str(self.script), *args],
+ cwd=str(self.tools), env=env, capture_output=True, text=True, timeout=120)
+
+ def assert_nothing_measured(self, result):
+ self.assertEqual(result.returncode, 2, result.stdout + result.stderr)
+ self.assertNotIn("Traceback", result.stderr)
+ self.assertIn(NOTHING_MEASURED, result.stderr,
+ "the reader is told in one line that nothing was measured")
+ self.assertEqual(result.stdout, "", "a run that measured nothing prints no numbers")
+ self.assertFalse((self.tools / "results").exists(), "no results folder for a run that did not happen")
+
+ def stub(self, body):
+ """A stand-in tools/tv.py beside the copy of bench.py, which imports it from there."""
+ (self.tools / "tv.py").write_text(body, encoding="utf-8")
+
+
+class RealClientTest(Base):
+ """The checkout's own tools/tv.py beside a copy of bench.py: what a reader's run does."""
+
+ @classmethod
+ def setUpClass(cls):
+ if not packages_installed():
+ raise unittest.SkipTest("needs the packages tools/venv installs (pip install -r "
+ "tools/requirements.txt)")
+
+ def setUp(self):
+ super().setUp()
+ shutil.copy2(TOOLS / "tv.py", self.tools / "tv.py")
+
+ def test_a_run_with_no_address_says_nothing_was_measured(self):
+ result = self.run_bench("--out-dir", "results")
+ self.assert_nothing_measured(result)
+ self.assertIn("--host", result.stderr, "the line names the option")
+ self.assertIn("TV_HOST", result.stderr, "and the environment variable")
+
+ def test_a_tv_port_that_is_not_a_number_says_nothing_was_measured(self):
+ result = self.run_bench("--out-dir", "results", TV_HOST="192.0.2.9", TV_PORT="abc")
+ self.assert_nothing_measured(result)
+ self.assertIn("not a port number: 'abc'", result.stderr, "tv.py's own line is printed")
+
+ def test_a_port_outside_the_range_says_nothing_was_measured(self):
+ for args, environment, named in ((["--port", "70000"], {"TV_HOST": "192.0.2.9"}, "'70000'"),
+ ([], {"TV_HOST": "192.0.2.9", "TV_PORT": "0"}, "'0'"),
+ (["--host", "192.0.2.9:70000"], {}, "'70000'")):
+ with self.subTest(args=args, environment=environment):
+ result = self.run_bench("--out-dir", "results", *args, **environment)
+ self.assert_nothing_measured(result)
+ self.assertIn(f"not a port number: {named}", result.stderr)
+
+ def test_a_checkout_without_adb_shell_says_nothing_was_measured(self):
+ # -S leaves site-packages out, so adb_shell is missing as it is in a fresh clone; there is no
+ # tools/venv beside the copied script for main() to start again under, so this is the branch a
+ # reader who never ran the pip line actually reaches.
+ result = self.run_bench("--host", "192.0.2.9", "--out-dir", "results", python_flags=("-S",))
+ self.assert_nothing_measured(result)
+ self.assertIn("bench.py needs adb_shell", result.stderr)
+ self.assertIn("tools/venv", result.stderr, "the line that builds the venv is still given")
+
+
+class StubClientTest(Base):
+ """The handler around `import tv` itself: a client module that decides to stop while imported."""
+
+ def test_a_client_that_stops_with_its_own_words_prints_them_and_answers_2(self):
+ self.stub('raise SystemExit("the client refuses this TV")\n')
+ result = self.run_bench("--host", "192.0.2.9", "--out-dir", "results")
+ self.assert_nothing_measured(result)
+ self.assertIn("the client refuses this TV", result.stderr, "tv.py's text is printed, not swallowed")
+
+ def test_a_client_that_stops_with_a_number_answers_2(self):
+ self.stub("raise SystemExit(3)\n")
+ result = self.run_bench("--host", "192.0.2.9", "--out-dir", "results")
+ self.assert_nothing_measured(result)
+ self.assertNotIn("3", result.stderr.replace("Nothing was measured", ""),
+ "a number is a status, not a sentence to print")
+
+ def test_a_client_that_exits_0_keeps_its_status(self):
+ self.stub("raise SystemExit(0)\n")
+ result = self.run_bench("--host", "192.0.2.9", "--out-dir", "results")
+ self.assertEqual(0, result.returncode, result.stdout + result.stderr)
+ self.assertNotIn(NOTHING_MEASURED, result.stderr, "nothing is added to a client that answered")
+
+ def test_a_tv_that_answered_but_names_no_home_keeps_1(self):
+ # Reached and read: this is the docstring's "anything else", not a run that never got there.
+ self.stub("HOST, PORT = '192.0.2.9', 5555\n"
+ "\n"
+ "class Device:\n"
+ " def shell(self, command, **kwargs):\n"
+ " return b''\n"
+ "\n"
+ " def close(self):\n"
+ " pass\n"
+ "\n"
+ "def connect():\n"
+ " return Device()\n"
+ "\n"
+ "def connect_error(error):\n"
+ " return None\n")
+ result = self.run_bench("--host", "192.0.2.9", "--out-dir", "results")
+ self.assertEqual(1, result.returncode, result.stdout + result.stderr)
+ self.assertIn("no component answers HOME on this TV", result.stderr)
+
+
+if __name__ == "__main__":
+ unittest.main()
diff --git a/tools/bench.py b/tools/bench.py
index 19b3ef991..6d9e0369e 100644
--- a/tools/bench.py
+++ b/tools/bench.py
@@ -27,9 +27,11 @@ The output is a summary plus the raw per-key numbers, so a run can be compared w
re-analysed later.
Exit status: 0 the run is measured and written; 2 an --out-dir that cannot be written into, judged
-before the TV is opened, a folder that cannot be created, or a TV that cannot be reached (tools/tv.py's
-sentence says which failure it was) - nothing was measured, and nothing was written; 1 anything else,
-including a result.json that could not be written after the run, whose numbers are then printed.
+before the TV is opened, a folder that cannot be created, a TV that cannot be reached (tools/tv.py's
+sentence says which failure it was), and a run that never got as far as the TV - no --host and no
+TV_HOST, a --port (or TV_PORT) tools/tv.py cannot read, a checkout tools/tv.py cannot be loaded from -
+nothing was measured, and nothing was written; 1 anything else, including a result.json that could not
+be written after the run, whose numbers are then printed.
Why the frame is matched by its completion time and not by its intended vsync: on a launcher
that moves the focus in a single frame (no animation), the frame that reacts to a key is born
@@ -90,8 +92,31 @@ class TV:
try:
import tv as tv_client # noqa: E402
except ModuleNotFoundError as e:
- raise SystemExit(f"bench.py needs {e.name}: python3 -m venv tools/venv && "
- "tools/venv/bin/pip install -r tools/requirements.txt") from e
+ # A checkout a reader has not built yet: no adb_shell, and no tools/venv beside this
+ # file for main() to start again under. Nothing has been measured, so this is the 2 the
+ # docstring gives a run that never reached the TV - it used to be a SystemExit carrying
+ # this text, which the interpreter answers with 1, the status of a run whose numbers
+ # exist and whose result.json could not be written.
+ print(f"bench.py needs {e.name}: python3 -m venv tools/venv && "
+ "tools/venv/bin/pip install -r tools/requirements.txt. Nothing was measured, "
+ "and nothing was written.", file=sys.stderr)
+ raise SystemExit(2) from e
+ except SystemExit as exit_request:
+ # tv.py refuses two shapes while it is imported, before it has a connection: a TV_PORT
+ # it cannot read ("not a port number: 'abc'", which is also what `--host <ip>:70000`
+ # and a TV_PORT of abc get) and, had main() not already stopped there, no TV_HOST.
+ # bench.py hands the client its own --host/--port just above, so the second is main()'s
+ # line and the first is a port typed wrong. Nothing was asked of the TV either time,
+ # and the interpreter answers such a text with 1 - the status a run that measured and
+ # could not write its result.json keeps - so a run that never started read as one that
+ # did. tv.py's own text is printed here; a module that exits 0 answered the call
+ # itself, and that status stands (the branch tools/theme_restart.py keeps).
+ if not exit_request.code:
+ raise
+ if isinstance(exit_request.code, str):
+ print(exit_request.code, file=sys.stderr)
+ print("Nothing was measured, and nothing was written.", file=sys.stderr)
+ raise SystemExit(2) from exit_request
# Read back the target tv.py made of that environment: it splits `host:port` and refuses a
# port outside 1..65535 there. Overriding these from the arguments (as this did) put the
# `:port` back into the host, so `--host 192.0.2.20:5877` asked for a machine of that name.
@@ -607,7 +632,12 @@ def main():
if args.idle < 1:
parser.error(f"--idle is the number of idle seconds: pass at least 1, not {args.idle}")
if not args.host:
- raise SystemExit("pass --host or set TV_HOST")
+ # Nothing has been measured: a run with no address is the 2 the docstring gives a run that never
+ # got as far as the TV, not the 1 a run keeps when its numbers exist and result.json could not be
+ # written. The line names both ways to pass the address, the two this run was given.
+ print("bench.py needs the TV's address: pass --host <ip> (or set TV_HOST). Nothing was "
+ "measured, and nothing was written.", file=sys.stderr)
+ raise SystemExit(2)
venv = venv_python()
if importlib.util.find_spec("adb_shell") is None and venv \
and os.path.realpath(venv) != os.path.realpath(sys.executable):