diff --git a/Makefile b/Makefile index 5b8208e..a46692b 100644 --- a/Makefile +++ b/Makefile @@ -65,6 +65,10 @@ serve-docs: $(VENV) manpage: $(VENV) help2man $(BIN)/blag --no-info -n "blog-aware, static site generator" -o debian/blag.1 +.PHONY: benchmark +benchmark: $(VENV) + $(BIN)/pytest --no-cov -capture=no -rA tests/benchmark.py + .PHONY: clean clean: rm -rf build dist *.egg-info diff --git a/blag/blag.py b/blag/blag.py index ed6db15..9f87cc1 100644 --- a/blag/blag.py +++ b/blag/blag.py @@ -323,6 +323,18 @@ def process_markdown( for src, dst in convertibles: logger.debug(f"Processing {src}") + # see first if the dst actually needs re-building. for that we compare + # the mtimes and assume mtime_dst > mtime_src means that it needs not + # to be rebuilt + if os.path.exists(f"{output_dir}/{dst}"): + mtime_src = os.stat(f"{input_dir}/{src}").st_mtime + mtime_dst = os.stat(f"{output_dir}/{dst}").st_mtime + if mtime_dst >= mtime_src: + logger.debug( + "Skipping, as target exists and is newer than source." + ) + continue + with open(f"{input_dir}/{src}") as fh: body = fh.read() diff --git a/tests/benchmark.py b/tests/benchmark.py new file mode 100644 index 0000000..9dbc309 --- /dev/null +++ b/tests/benchmark.py @@ -0,0 +1,37 @@ +"""Benchmark the performance of the blag build command.""" + +import logging +import os +from argparse import Namespace + +from pytest import LogCaptureFixture + +import blag +from blag.blag import build + + +def test_performance(args: Namespace, caplog: LogCaptureFixture) -> None: + """Test performance of the build command.""" + caplog.set_level(logging.ERROR) + + FILES = 10000 + print(f"Generating {FILES} markdown files") + # create random markdown files in the content directory + with open(os.path.join(blag.__path__[0], "content", "testpage.md")) as fh: + markdown = fh.read() + for i in range(FILES): + with open(f"content/{i}.md", "w") as f: + f.write(markdown) + f.write(str(i)) + + from time import time + + t = time() + build(args) + t_first = time() - t + + t = time() + build(args) + t_second = time() - t + print(f"First run: {t_first:.2f}s, second run: {t_second:.2f}s") + print(f"Speedup: {t_first/t_second:.2f}")