Skip to content

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

tools: bench.py's result.json is written through a part file, so a failed write keeps the last run's

xbshenge321e63caab9
diff --git a/tests/test_bench_out_dir.py b/tests/test_bench_out_dir.py
index 53bad9c2c..378a44b2e 100644
--- a/tests/test_bench_out_dir.py
+++ b/tests/test_bench_out_dir.py
@@ -133,8 +133,10 @@ class BenchOutDirTest(unittest.TestCase):
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('part = path + ".part"', source)
+ self.assertIn("os.replace(part, path)", source)
+ write = source.index(" failed = write_result(path, result)\n")
+ self.assertIn("if failed is not None:", source[write:write + 400])
self.assertIn("this run's only copy", source)
self.assertIn(" return 1\n", source[write:])
self.assertIn("sys.exit(main())", source)
@@ -147,5 +149,55 @@ class BenchOutDirTest(unittest.TestCase):
self.assertIn("1 anything else", docstring)
+class BenchResultWriteTest(unittest.TestCase):
+ """result.json is written through a part file, so a write that cannot finish keeps the last run's.
+
+ `open(path, "w")` truncates first: a run whose write failed - the disk that filled up during the
+ minutes it measures, which is the failure bench.py's own message is about - left a half file
+ under the name a reader opens, and took the result.json of the run before with it. Those numbers
+ cannot be measured again the same way (the tool says so itself), so the write ends in
+ os.replace() and either lands whole or not at all.
+ """
+
+ def setUp(self):
+ self._tmp = tempfile.TemporaryDirectory()
+ self.folder = Path(self._tmp.name)
+ sys.path.insert(0, str(ROOT / "tools"))
+ import bench # noqa: E402 the write_result() under test
+ self.bench = bench
+
+ def tearDown(self):
+ self._tmp.cleanup()
+
+ def write(self, result):
+ return self.bench.write_result(str(self.folder / "result.json"), result)
+
+ def test_a_write_that_cannot_finish_leaves_the_earlier_result(self):
+ earlier = {"keys": {"series": 3, "per_series": 36}, "response_ms": {"p50": 41}}
+ (self.folder / "result.json").write_text(json.dumps(earlier, indent=2), encoding="utf-8")
+ # A part file that cannot be opened stands for a write that stops before a byte of this run's
+ # numbers reaches result.json: a disk that filled up mid-write answers the same way.
+ (self.folder / "result.json.part").mkdir()
+ failed = self.write({"keys": {}, "response_ms": {"p50": 99}})
+ self.assertIsInstance(failed, OSError)
+ self.assertEqual(earlier, json.loads((self.folder / "result.json").read_text(encoding="utf-8")),
+ "a write that failed replaced the numbers of the run before it")
+
+ def test_a_finished_write_replaces_the_result_and_leaves_no_part_file(self):
+ (self.folder / "result.json").write_text('{"response_ms": {"p50": 41}}', encoding="utf-8")
+ self.assertIsNone(self.write({"response_ms": {"p50": 99}}))
+ self.assertEqual({"response_ms": {"p50": 99}},
+ json.loads((self.folder / "result.json").read_text(encoding="utf-8")))
+ self.assertEqual(["result.json"], sorted(p.name for p in self.folder.iterdir()),
+ "a part file was left in the results folder")
+
+ def test_a_part_file_a_killed_run_left_behind_is_overwritten(self):
+ # A run that was interrupted after its part file was opened leaves one behind; the next run
+ # has to write its own, not trip over it.
+ (self.folder / "result.json.part").write_text('{"response_ms": {"p50": 1', encoding="utf-8")
+ self.assertIsNone(self.write({"response_ms": {"p50": 99}}))
+ self.assertEqual(["result.json"], sorted(p.name for p in self.folder.iterdir()))
+
+
if __name__ == "__main__":
unittest.main()
diff --git a/tools/bench.py b/tools/bench.py
index 2532f5675..64ca1b62a 100644
--- a/tools/bench.py
+++ b/tools/bench.py
@@ -31,7 +31,8 @@ before the TV is opened, a folder that cannot be created, a TV that cannot be re
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.
+be written after the run, whose numbers are then printed and whose folder keeps the result.json that was
+already in it.
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
@@ -567,6 +568,31 @@ def stats(values):
"p99": pick(0.99), "max": ordered[-1], "n": len(ordered)}
+def write_result(path, result):
+ """Write result.json, or return the OSError that stopped it and leave the earlier one alone.
+
+ `open(path, "w")` truncates before it writes, so a write that cannot be finished - a disk that
+ filled up during the minutes the run takes, which is the failure the caller's message is about -
+ took the result.json of the run before with it and left a half file under the name a reader
+ opens. The numbers of that earlier run cannot be measured again the same way, so the write goes
+ to a part file and is renamed into place, the way tools/trace.py's --out and tools/recovery.py's
+ record already write theirs: either the new result.json is complete, or the one that was there
+ is untouched. A part file a killed run left behind is overwritten by the next one.
+ """
+ part = path + ".part"
+ try:
+ with open(part, "w", encoding="utf-8") as f:
+ json.dump(result, f, indent=2)
+ os.replace(part, path)
+ except OSError as e:
+ try:
+ os.unlink(part)
+ except OSError:
+ pass # a part file that is a folder, or in a folder that went away: nothing to clean
+ return e
+ return None
+
+
def out_dir(value):
"""`--out-dir` as typed, or an argparse error before the TV is opened.
@@ -731,16 +757,16 @@ def main():
}
path = os.path.join(args.out_dir, "result.json")
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:
+ failed = write_result(path, result)
+ if failed is not None:
# 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.
+ # The write goes through a part file, so the result.json of an earlier run is still there.
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)
+ print(f"cannot write {path}: {failed.strerror or failed}. The result.json that was already "
+ "in that folder is left as it was: the numbers above are this run's only copy.",
+ file=sys.stderr)
return 1
print(json.dumps(summary, indent=2))
print(f"Written: {path}")