fix(ci): Generate thumbnails for the goldens landing page (#13083) 7a78062e63 This is way overdue. Let's quit overusing bandwidth and quit DOSing mobile (and non-mobile) browsers. Let's also stop scraping "passing" tests in the rebaseline script. No need to churn LFS on a passing test. Co-authored-by: Chris Dalton <99840794+csmartdalton@users.noreply.github.com>
diff --git a/.rive_head b/.rive_head index 5b0c429..89e83e5 100644 --- a/.rive_head +++ b/.rive_head
@@ -1 +1 @@ -864d68d032e9a63589285a9ba8aaa87ba763a9f9 +7a78062e6323542c4aae5df9a0629042025e45a8
diff --git a/tests/diff.py b/tests/diff.py index 30c83b9..7a54af4 100644 --- a/tests/diff.py +++ b/tests/diff.py
@@ -92,6 +92,18 @@ self.candidates_path = pathlib.Path(os.path.relpath(os.path.join(candidates_path, f"{self.name}.png"), output_path)).as_posix() self.golden_path = pathlib.Path(os.path.relpath(os.path.join(golden_path, f"{self.name}.png"), output_path)).as_posix() + # image_diff.py writes small thumbnails next to the diff images in the + # output directory. The landing page uses these for its <img src> so it + # doesn't download every full resolution image at once; the full image is + # still linked via the surrounding <a href>. Thumbnails always live in + # the output directory regardless of --pack/--recursive, so their layout + # mirrors the diff images (device sub-folder when there is one). + thumb_dir = device_name if device_name is not None else "" + self.golden_thumb = pathlib.Path(os.path.join(thumb_dir, f"{self.name}.golden.thumb.png")).as_posix() + self.candidate_thumb = pathlib.Path(os.path.join(thumb_dir, f"{self.name}.candidate.thumb.png")).as_posix() + self.diff0_thumb = pathlib.Path(os.path.join(thumb_dir, f"{self.name}.diff0.thumb.png")).as_posix() + self.diff1_thumb = pathlib.Path(os.path.join(thumb_dir, f"{self.name}.diff1.thumb.png")).as_posix() + if len(words) == 2: self.avg = None self.histogram = None @@ -151,16 +163,20 @@ if self.type == 'missing_golden': # show candidate, since golden is missing vals['image'] = self.candidates_path + vals['image_thumb'] = self.candidate_thumb return self.missing_file_entry_template.format_map(vals) - + elif self.type == 'missing_candidate': # show golden, since candidate is missing vals['image'] = self.golden_path + vals['image_thumb'] = self.golden_thumb return self.missing_file_entry_template.format_map(vals) vals['golden'] = self.golden_path vals['candidate'] = self.candidates_path - + vals['golden_thumb'] = self.golden_thumb + vals['candidate_thumb'] = self.candidate_thumb + if self.type == "pass" or self.type == "failed": vals['max'] = self.max_diff vals['avg'] = self.avg @@ -169,6 +185,8 @@ vals['histogram'] = self.histogram if self.histogram is not None else 'None' vals['diff0'] = self.diff0_path vals['diff1'] = self.diff1_path + vals['diff0_thumb'] = self.diff0_thumb + vals['diff1_thumb'] = self.diff1_thumb if self.type == 'pass': return self.pass_entry_template.format_map(vals) @@ -351,35 +369,38 @@ return (total_lines, test_entries, success) def diff_directory_shallow(candidates_path, output_path, golden_path, device_name=None, browserstack_details=None): - original_filenames = set((file.name for file in os.scandir(candidates_path) - if file.is_file() and file.name.endswith('.png'))) - candidate_filenames = set(os.listdir(golden_path)) - intersect_filenames = original_filenames.intersection(candidate_filenames) + candidate_filenames = set((file.name for file in os.scandir(candidates_path) + if file.is_file() and file.name.endswith('.png'))) + golden_filenames = set((file.name for file in os.scandir(golden_path) + if file.is_file() and file.name.endswith('.png'))) + # Diff every file that appears in either directory. image_diff.py reports a + # missing_candidate / missing_golden status for files that only exist on one + # side and writes a thumbnail of the single image that does exist, so the + # landing page can use thumbnails for missing entries too (rather than + # eagerly loading the full resolution image). + all_filenames = candidate_filenames | golden_filenames - missing = [] - for file in original_filenames.difference(candidate_filenames): + # Log which files are missing where; image_diff.py records them as entries. + for file in candidate_filenames.difference(golden_filenames): print(f'Candidate file {file} missing in goldens.') - missing.append(TestEntry([file.split('.')[0], 'missing_golden'], candidates_path, golden_path, output_path, device_name, browserstack_details)) - - for file in candidate_filenames.difference(original_filenames): + for file in golden_filenames.difference(candidate_filenames): print(f'Golden file {file} missing in candidates.') - missing.append(TestEntry([file.split('.')[0], 'missing_candidate'], candidates_path, golden_path, output_path, device_name, browserstack_details)) if args.jobs > 1: - print("Diffing %i candidates in %i processes..." % (len(intersect_filenames), args.jobs)) + print("Diffing %i candidates in %i processes..." % (len(all_filenames), args.jobs)) else: - print("Diffing %i candidates..." % len(intersect_filenames)) + print("Diffing %i candidates..." % len(all_filenames)) sys.stdout.flush() # Fill a shared queue with every filename. work_queue = queue.Queue() - for filename in intersect_filenames: + for filename in all_filenames: work_queue.put(filename) # Generate the diffs (if any) and write to the status file(s). Start up to # `jobs` long-lived image_diff.py workers that each pull filenames off the # queue as fast as they can finish them. - njobs = min(args.jobs, len(intersect_filenames)) + njobs = min(args.jobs, len(all_filenames)) parent_pid = os.getpid() threads = [] for index in range(njobs): @@ -392,16 +413,14 @@ t.join() (total_lines, entries, success) = parse_status(candidates_path, golden_path, output_path, device_name, browserstack_details) - - entries.extend(missing) print(f'finished with Succes:{success} and {total_lines} lines') - if total_lines != len(intersect_filenames): - print(f"Internal failure: Got {total_lines} status lines. Expected {len(intersect_filenames)}.") + if total_lines != len(all_filenames): + print(f"Internal failure: Got {total_lines} status lines. Expected {len(all_filenames)}.") success = False - if original_filenames.symmetric_difference(candidate_filenames): + if candidate_filenames.symmetric_difference(golden_filenames): print("golden and candidate directories do not have identical files.") success = False @@ -409,6 +428,8 @@ for status_filename in glob.iglob(status_filename_pattern): os.remove(status_filename) + missing = [entry for entry in entries + if entry.type in ('missing_golden', 'missing_candidate')] return (entries, missing, success) # Sort descending total failure count (missing + failure), then by descending failure count, then by name
diff --git a/tests/image_diff.py b/tests/image_diff.py index 898f583..45d36a0 100644 --- a/tests/image_diff.py +++ b/tests/image_diff.py
@@ -1,11 +1,18 @@ #requires pip install python-opencv-headless import argparse -import cv2 as cv -import numpy as np import os import sys +# We deliberately imread() files that may only exist on one side (missing +# candidate/golden), which OpenCV otherwise logs as a noisy "can't open/read +# file" WARN. diff_one() handles the resulting None explicitly and records a +# missing status, so quiet OpenCV to errors and above before it is imported. +os.environ.setdefault("OPENCV_LOG_LEVEL", "ERROR") + +import cv2 as cv +import numpy as np + parser = argparse.ArgumentParser(description= """ Compares candidate images against goldens by pixels, writing one status line @@ -43,6 +50,25 @@ name = name[:-len(oldsuffix)] return name +# The landing page (index.html) embeds one small thumbnail per image so that +# opening it doesn't force the browser to download every full resolution golden, +# candidate and diff at once, which DOSes phones. The full resolution image is +# still a click away via the surrounding <a href>. Thumbnails are written next +# to the diff images in the output directory. +THUMBNAIL_HEIGHT = 192 + +def write_thumbnail(image, path): + # Downscale to THUMBNAIL_HEIGHT keeping aspect ratio; never upscale a smaller + # image. No-op when the source image failed to load. + if image is None: + return + height, width = image.shape[:2] + if height > THUMBNAIL_HEIGHT: + scale = THUMBNAIL_HEIGHT / height + new_size = (max(1, round(width * scale)), THUMBNAIL_HEIGHT) + image = cv.resize(image, new_size, interpolation=cv.INTER_AREA) + cv.imwrite(path, image) + def verbose_log(name, val): if not args.verbose: return @@ -120,6 +146,13 @@ print(f"Failed to load and process images {E}", file=sys.stderr) failed = True + # Write the golden/candidate thumbnails here, before the early returns below, + # so identical and missing entries get them too (they don't reach the diff + # writing block at the end). + if args.outdir: + write_thumbnail(candidate, os.path.join(args.outdir, name + ".candidate.thumb.png")) + write_thumbnail(golden, os.path.join(args.outdir, name + ".golden.thumb.png")) + status.write(name + "\t") if failed: status.write("failed\n") @@ -165,6 +198,8 @@ verbose_log(name, f"writing images {c_diff_path} and {c_mask_path}") cv.imwrite(c_diff_path, rgb_diff) cv.imwrite(c_mask_path, mask) + write_thumbnail(rgb_diff, os.path.join(args.outdir, name + ".diff0.thumb.png")) + write_thumbnail(mask, os.path.join(args.outdir, name + ".diff1.thumb.png")) verbose_log(name, "finished writing output images") def prepare_dirs():
diff --git a/tests/template/error_entry.html b/tests/template/error_entry.html index f1ffa84..fa89b1c 100644 --- a/tests/template/error_entry.html +++ b/tests/template/error_entry.html
@@ -30,10 +30,10 @@ </thead> <tbody> <tr> - <td> <a href="{golden}"><img src="{golden}" height="192"></a></td> - <td> <a href="{candidate}"><img src="{candidate}" height="192"></a></td> - <td> <a href="{diff0}"><img src="{diff0}" height="192"></a></td> - <td> <a href="{diff1}"><img src="{diff1}" height="192"></a></td> + <td> <a href="{golden}"><img src="{golden_thumb}" height="192"></a></td> + <td> <a href="{candidate}"><img src="{candidate_thumb}" height="192"></a></td> + <td> <a href="{diff0}"><img src="{diff0_thumb}" height="192"></a></td> + <td> <a href="{diff1}"><img src="{diff1_thumb}" height="192"></a></td> </tr> </tbody> </table>
diff --git a/tests/template/identical_entry.html b/tests/template/identical_entry.html index 787bda1..af8d5b3 100644 --- a/tests/template/identical_entry.html +++ b/tests/template/identical_entry.html
@@ -8,8 +8,8 @@ </thead> <tbody> <tr> - <td> <a href="{golden}"><img src="{golden}" height="192"></a></td> - <td> <a href="{candidate}"><img src="{candidate}" height="192"></a></td> + <td> <a href="{candidate}"><img src="{candidate_thumb}" height="192"></a></td> + <td> <a href="{golden}"><img src="{golden_thumb}" height="192"></a></td> </tr> </tbody> </table>
diff --git a/tests/template/missing_file_entry.html b/tests/template/missing_file_entry.html index f028727..1857785 100644 --- a/tests/template/missing_file_entry.html +++ b/tests/template/missing_file_entry.html
@@ -7,7 +7,7 @@ </thead> <tbody> <tr> - <td> <a href="{image}"><img src="{image}" height="192"></a></td> + <td> <a href="{image}"><img src="{image_thumb}" height="192"></a></td> </tr> </tbody> </table> \ No newline at end of file
diff --git a/tests/template/pass_entry.html b/tests/template/pass_entry.html index d582a13..7876d02 100644 --- a/tests/template/pass_entry.html +++ b/tests/template/pass_entry.html
@@ -30,10 +30,10 @@ </thead> <tbody> <tr> - <td> <a href="{golden}"><img src="{golden}" height="192"></a></td> - <td> <a href="{candidate}"><img src="{candidate}" height="192"></a></td> - <td> <a href="{diff0}"><img src="{diff0}" height="192"></a></td> - <td> <a href="{diff1}"><img src="{diff1}" height="192"></a></td> + <td> <a href="{golden}"><img src="{golden_thumb}" height="192"></a></td> + <td> <a href="{candidate}"><img src="{candidate_thumb}" height="192"></a></td> + <td> <a href="{diff0}"><img src="{diff0_thumb}" height="192"></a></td> + <td> <a href="{diff1}"><img src="{diff1_thumb}" height="192"></a></td> </tr> </tbody>