alexwlchan / alexwlchan.net

Source code and plugins for my website, a static site built with Jekyll
https://alexwlchan.net/
MIT License
42 stars 13 forks source link

Measure changes in overall page size #854

Open alexwlchan opened 4 months ago

alexwlchan commented 4 months ago

e.g. the change to de-duplicate <style> tags in https://github.com/alexwlchan/alexwlchan.net/pull/852 should have reduced the weight of a bunch of pages, not just the new post I was working on – it’d be nice to get overall page size stats.

alexwlchan commented 3 weeks ago

Although I'm not doing it in GitHub Actions yet, here's a script I wrote to do it on a one-off basis:

#!/usr/bin/env python3

import pathlib
import humanize

def get_file_paths_under(root=".", *, suffix=""):
    """
    Generates the absolute paths to every matching file under ``root``.
    """
    root = pathlib.Path(root)

    if root.exists() and not root.is_dir():
        raise ValueError(f"Cannot find files under file: {root!r}")

    if not root.is_dir():
        raise FileNotFoundError(root)

    for dirpath, _, filenames in root.walk():
        for f in filenames:
            p = dirpath / f

            if p.is_file() and f.lower().endswith(suffix):
                yield p

sizes = []

for p in get_file_paths_under("_site", suffix=".html"):
    if "/files/" in str(p):
        continue
    sizes.append(p.stat().st_size)

print(sum(sizes) / len(sizes))

print(humanize.naturalsize(sum(sizes) / len(sizes)))