Debloat any Android or Google TV over Wi-Fi and get an Apple TV-style home.
Python adbadblockandroid-tvandroid-tv-launcherapple-tvbloatware-removalclaude-codedebloatfire-tvgoogle-tvlauncherprivacysmart-tvsony-braviatclvibe-codingxiaomi
tools: bench.py judges its --out-dir before it opens the TV
xbshenge303ef8bdeb2
diff --git a/tests/test_bench_out_dir.py b/tests/test_bench_out_dir.pynew file mode 100644index 000000000..53bad9c2c--- /dev/null+++ b/tests/test_bench_out_dir.py@@ -0,0 +1,151 @@+"""bench.py judges its --out-dir before it opens the TV, and does not lose a finished run.++tools/bench.py is the tool docs/workflows/perf-ab.md measures a launcher change with, and its results+folder was the one thing it read as whatever was typed: `os.makedirs(args.out_dir, exist_ok=True)` sat+after the resolve-activity and the `dumpsys package` that read the launcher's version, so++ --out-dir results.txt FileExistsError, a file where the folder goes+ --out-dir results.txt/nested NotADirectoryError, a path through a file+ --out-dir "" FileNotFoundError: ''++were each a 12-line traceback with exit 1, at the start of a run that measured nothing - and exit 1 is+the code a finished run that could not be written returns, so a typo looked like a benchmark that ran.+The write at the end had the same problem at the worst moment: after the keys, the cold starts, the+returns and the idle window, a folder removed or a disk that filled up took every number with it.++The folder is now an argparse type= (judged before tv.py is imported, before anything is asked of the+TV), a folder that cannot be created is answered before measuring starts, and a result.json that cannot+be written is printed to stdout instead, with exit 1 saying the file is not it. These tests run the real+script beside a stub tools/tv.py that records every command, so "nothing was asked of the TV" means the+stub's log is not there - no TV, no network and no ADB key is needed.+"""+import json+import os+from pathlib import Path+import shutil+import subprocess+import sys+import tempfile+import unittest++ROOT = Path(__file__).resolve().parents[1]++# The client bench.py imports: it answers the two commands the start of a run sends, and records every+# command it was asked, so a refusal that happened before connecting leaves no log at all.+STUB = '''import json, os+from pathlib import Path++HOST = PORT = None++class Device:+ def shell(self, command, read_timeout_s=None, decode=True):+ log = Path(os.environ["BENCH_STUB_LOG"])+ calls = json.loads(log.read_text()) if log.exists() else []+ calls.append(command)+ log.write_text(json.dumps(calls))+ if "resolve-activity" in command:+ return "priority=0\\n com.gathin.tvhome/.MainActivity\\n"+ if "dumpsys package" in command:+ return "versionName=1.2.3"+ return ""++ def close(self):+ pass++def connect():+ return Device()+'''+++class BenchOutDirTest(unittest.TestCase):+ def setUp(self):+ self._tmp = tempfile.TemporaryDirectory()+ self.folder = Path(self._tmp.name)+ shutil.copyfile(ROOT / "tools" / "bench.py", self.folder / "bench.py")+ (self.folder / "tv.py").write_text(STUB, encoding="utf-8")+ self.work = self.folder / "work"+ self.work.mkdir()+ self.log = self.folder / "calls.json"++ def tearDown(self):+ self._tmp.cleanup()++ def run_bench(self, args):+ env = os.environ.copy()+ for key in ("TV_HOST", "TV_PORT"):+ env.pop(key, None)+ env["BENCH_STUB_LOG"] = str(self.log)+ return subprocess.run([sys.executable, str(self.folder / "bench.py"), "--host", "192.0.2.20", *args],+ cwd=self.work, env=env, capture_output=True, text=True, timeout=60)++ def calls(self):+ return json.loads(self.log.read_text()) if self.log.exists() else []++ def test_a_file_where_the_folder_goes_is_refused_before_the_tv(self):+ (self.work / "results.txt").write_text("hello\n", encoding="utf-8")+ result = self.run_bench(["--out-dir", "results.txt"])+ self.assertEqual(result.returncode, 2, result.stderr)+ self.assertIn("results.txt is a file, not a folder", result.stderr)+ self.assertNotIn("Traceback", result.stderr)+ self.assertEqual("", result.stdout)+ self.assertEqual([], self.calls(), "the TV was asked something for a folder that cannot be written")+ self.assertEqual("hello\n", (self.work / "results.txt").read_text(encoding="utf-8"))++ def test_a_path_through_a_file_is_refused_before_the_tv(self):+ (self.work / "results.txt").write_text("hello\n", encoding="utf-8")+ result = self.run_bench(["--out-dir", "results.txt/nested"])+ self.assertEqual(result.returncode, 2, result.stderr)+ self.assertIn("results.txt/nested runs through", result.stderr)+ self.assertIn("which is a file", result.stderr)+ self.assertNotIn("Traceback", result.stderr)+ self.assertEqual([], self.calls())++ def test_an_empty_out_dir_is_refused_before_the_tv(self):+ result = self.run_bench(["--out-dir", ""])+ self.assertEqual(result.returncode, 2, result.stderr)+ self.assertIn("needs a folder to write the results into", result.stderr)+ self.assertNotIn("FileNotFoundError", result.stderr)+ self.assertEqual([], self.calls())++ def test_a_folder_that_is_not_there_yet_is_still_accepted(self):+ # os.makedirs() has always created the folder, and docs/workflows/perf-ab.md's+ # `--out-dir work/bench-before` is a folder a run makes. The stub answers HOME, so the run+ # gets past the point where --out-dir is judged - which is all this test needs to know.+ result = self.run_bench(["--out-dir", "new-folder/today"])+ self.assertGreaterEqual(len(self.calls()), 2,+ "a folder that does not exist yet was refused before the TV: " + result.stderr)+ self.assertIn("resolve-activity", self.calls()[0])+ self.assertNotIn("--out-dir:", result.stderr)++ def test_an_existing_folder_is_accepted(self):+ result = self.run_bench(["--out-dir", "."])+ self.assertGreaterEqual(len(self.calls()), 2, result.stderr)+ self.assertNotIn("--out-dir:", result.stderr)++ def test_the_two_refusals_sit_where_the_run_cannot_lose_anything(self):+ source = (ROOT / "tools" / "bench.py").read_text(encoding="utf-8")+ # The folder is judged in argparse, so it is judged before TV() is built.+ option = source.index('parser.add_argument("--out-dir", type=out_dir')+ self.assertLess(option, source.index("tv = TV("),+ "the --out-dir check has to run in argparse, before the TV is opened")+ # A folder that cannot be created is answered before the key sequence starts.+ start = source.index(" try:\n os.makedirs(args.out_dir, exist_ok=True)")+ self.assertLess(start, source.index(" samples = []"),+ "a folder that cannot be created has to be answered before measuring")+ # The write at the end is guarded, and the numbers are printed when it fails.+ write = source.index(' try:\n with open(path, "w", encoding="utf-8") as f:')+ self.assertIn("except OSError as e:", source[write:write + 900])+ self.assertIn("this run's only copy", source)+ self.assertIn(" return 1\n", source[write:])+ self.assertIn("sys.exit(main())", source)++ def test_the_docstring_states_the_exit_status(self):+ source = (ROOT / "tools" / "bench.py").read_text(encoding="utf-8")+ docstring = source.split('"""')[1]+ self.assertIn("Exit status: 0", docstring)+ self.assertIn("2 an --out-dir that cannot be written into", docstring)+ self.assertIn("1 anything else", docstring)+++if __name__ == "__main__":+ unittest.main()diff --git a/tests/test_tool_results_ignored.py b/tests/test_tool_results_ignored.pyindex f854c64fb..cb31fce59 100644--- a/tests/test_tool_results_ignored.py+++ b/tests/test_tool_results_ignored.py@@ -44,9 +44,15 @@ def ignored(path):def default_of(tool, flag):- """The default of one argument, out of the tool's own parser."""+ """The default of one argument, out of the tool's own call to add_argument().++ Read from the whole call, not from the flag and the default side by side: an argument takes+ type=, metavar= and help= as well, in whatever order the tool writes them, and bench.py's+ --out-dir grew a type= when it started judging the folder before opening the TV.+ """text = (TOOLS / tool).read_text(encoding="utf-8")- found = re.search(r'"%s",\s*default="([^"]+)"' % re.escape(flag), text)+ call = re.search(r'add_argument\(\s*"%s"(.*?)\)\s*\n' % re.escape(flag), text, re.S)+ found = re.search(r'default="([^"]*)"', call[1]) if call else Nonereturn found[1] if found else ""diff --git a/tools/bench.py b/tools/bench.pyindex 8a33e07a3..7af86897d 100644--- a/tools/bench.py+++ b/tools/bench.py@@ -19,8 +19,12 @@ Usage:TV_HOST=192.168.1.50 python tools/bench.py --keys 36 --series 3TV_HOST=192.168.1.50 python tools/bench.py --keys 36 --series 3 --out-dir my-results-The output is a summary plus the raw per-key numbers, so a run can be compared with another one-or re-analysed later.+The output is a summary plus the raw per-key numbers, so a run can be compared with another one or+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, or a folder that cannot be created (nothing was measured); 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 launcherthat moves the focus in a single frame (no animation), the frame that reacts to a key is born@@ -505,6 +509,33 @@ def stats(values):"p99": pick(0.99), "max": ordered[-1], "n": len(ordered)}+def out_dir(value):+ """`--out-dir` as typed, or an argparse error before the TV is opened.++ The folder may not exist yet: bench.py creates it. What it cannot do is write into a file, run+ through a file, or nowhere - os.makedirs() answered those with a traceback after the TV had+ already been asked which component answers HOME and what version it runs, at the start of a run+ that then measured nothing. The same rule as tools/trace.py's --out (tools/perf-ab.md's three+ tools), and the same reason for judging it in argparse: it costs nothing and it happens first.+ """+ if not value:+ raise argparse.ArgumentTypeError("needs a folder to write the results into")+ if os.path.exists(value) and not os.path.isdir(value):+ raise argparse.ArgumentTypeError(f"{value} is a file, not a folder to write the results into")+ # makedirs() creates the whole chain, so the nearest folder that is already there decides whether+ # it can: a file anywhere above the last part is NotADirectoryError whatever follows it.+ existing = os.path.dirname(os.path.abspath(value))+ while existing and not os.path.exists(existing):+ parent = os.path.dirname(existing)+ if parent == existing:+ break+ existing = parent+ if os.path.exists(existing) and not os.path.isdir(existing):+ raise argparse.ArgumentTypeError(+ f"{value} runs through {existing}, which is a file, not a folder")+ return value++def main():parser = argparse.ArgumentParser(description=__doc__,formatter_class=argparse.RawDescriptionHelpFormatter)@@ -520,7 +551,8 @@ def main():parser.add_argument("--cold", type=int, default=5, help="cold starts")parser.add_argument("--returns", type=int, default=10, help="returns to Home")parser.add_argument("--idle", type=int, default=60, help="idle seconds")- parser.add_argument("--out-dir", default="bench-results", help="where to write the results")+ parser.add_argument("--out-dir", type=out_dir, default="bench-results",+ metavar="DIR", help="where to write the results (default bench-results)")args = parser.parse_args()if not args.host:raise SystemExit("pass --host or set TV_HOST")@@ -537,7 +569,15 @@ def main():raise SystemExit("no component answers HOME on this TV")package = tv.package_of(component)version = tv.version_of(package)- os.makedirs(args.out_dir, exist_ok=True)+ try:+ os.makedirs(args.out_dir, exist_ok=True)+ except OSError as e:+ # --out-dir itself was judged in argparse; a folder that exists but cannot be created into+ # (a read-only checkout, a full disk) is only knowable here. Say it before a run of several+ # minutes measures keys for a folder that will not take the result; the TV was only read.+ print(f"cannot create {args.out_dir}: {e.strerror or e}. Nothing was measured, and nothing "+ "on the TV was changed.", file=sys.stderr)+ return 2print(f"Launcher: {component} ({version})")print(f"Keys: {args.series} x {args.keys}; writing to {args.out_dir}")@@ -606,11 +646,22 @@ def main():"samples": samples,}path = os.path.join(args.out_dir, "result.json")- with open(path, "w", encoding="utf-8") as f:- json.dump(result, f, indent=2)- print(json.dumps({k: v for k, v in result.items() if k != "samples"}, indent=2))+ summary = {k: v for k, v in result.items() if k != "samples"}+ try:+ with open(path, "w", encoding="utf-8") as f:+ json.dump(result, f, indent=2)+ except OSError as e:+ # The folder was writable when the run started - it can be removed, or fill up, during the+ # minutes it measures. The numbers were measured on the TV and cannot be measured again the+ # same way, so they are printed: this is the only copy, and exit 1 says the file is not it.+ print(json.dumps(summary, indent=2))+ print(f"cannot write {path}: {e.strerror or e}. Nothing was written: the numbers above are "+ "this run's only copy.", file=sys.stderr)+ return 1+ print(json.dumps(summary, indent=2))print(f"Written: {path}")+ return 0if __name__ == "__main__":- main()+ sys.exit(main())