okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
2b36e8a02
main
1#!/usr/bin/env python3 2"""Index page for bench/results/: bench/results/README.md 3 4python tools/bench_results_index.py write the index 5python tools/bench_results_index.py --check fail when the index is out of date (a new result folder must be listed) 6 7One row per result folder: its date (from the folder name), its own title (the first heading of its README or of its first 8Markdown file), and how many files it holds. The results themselves stay where they are. Standard library only. 9""" 10import argparse 11import os 12import re 13import sys 14 15sys .path .insert (0 ,os .path .dirname (os .path .abspath (__file__ ))) 16import doc_pages # noqa: E402 17 18ROOT = doc_pages .ROOT 19RESULTS = os .path .join (ROOT ,"bench" ,"results" ) 20OUT = os .path .join (RESULTS ,"README.md" ) 21 22 23def title_of (folder ): 24names = sorted (n for n in os .listdir (folder )if n .lower ().endswith (".md" )) 25names .sort (key = lambda n : (n .lower ()!= "readme.md" ,n )) 26for name in names : 27try : 28with open (os .path .join (folder ,name ),encoding = "utf-8" ,errors = "replace" )as f : 29for line in f : 30m = re .match (r"^#{1,3}\s+(.*\S)" ,line ) 31if m : 32t = re .sub (r"[`*]" ,"" ,m .group (1 )).replace ("|" ,"/" ) 33return t if len (t )<= 150 else t [:147 ].rstrip ()+ "..." 34except OSError : 35pass 36return "(no Markdown page)" 37 38 39def count_files (folder ): 40return sum (len (files )for _ ,_ ,files in os .walk (folder )) 41 42 43def build (): 44entries = sorted (e for e in os .listdir (RESULTS )if e != "README.md" ) 45folders = [e for e in entries if os .path .isdir (os .path .join (RESULTS ,e ))] 46loose = [e for e in entries if not os .path .isdir (os .path .join (RESULTS ,e ))] 47total = sum (count_files (os .path .join (RESULTS ,f ))for f in folders )+ len (loose ) 48lines = ["# Benchmark results" ,"" , 49"Raw results and short reports from measurement runs, one folder per run, named by date. They stay here as they were written: each states" , 50"the Strata version, hardware and method of its own day, and none is a statement about a later release. Current numbers are in the" , 51"[docs](../../docs/README.md) and the [README](../../README.md); how to measure is in [BENCHMARK_PROTOCOL.md](../../docs/BENCHMARK_PROTOCOL.md)." , 52"" ,"This page is built by `python tools/bench_results_index.py` (`--check` fails when a folder is missing from it). " 53"%d folders and %d loose files, %d files in all." % (len (folders ),len (loose ),total ),"" ] 54month = None 55for f in folders : 56m = re .match (r"^(\d{4}-\d{2})-(\d{2})" ,f ) 57key = m .group (1 )if m else "undated" 58if key != month : 59month = key 60lines += ["" ,"## " + key ,"" ,"| Folder | What | Files |" ,"| --- | --- | ---: |" ] 61lines .append ("| [%s](%s/) | %s | %d |" % (f ,f ,title_of (os .path .join (RESULTS ,f )),count_files (os .path .join (RESULTS ,f )))) 62if loose : 63lines += ["" ,"## Loose files" ,"" ,"| File |" ,"| --- |" ]+ ["| [%s](%s) |" % (n ,n )for n in loose ] 64return "\n" .join (lines )+ "\n" 65 66 67def main (argv = None ): 68ap = argparse .ArgumentParser (description = __doc__ .split ("\n" )[0 ]) 69ap .add_argument ("--check" ,action = "store_true" ) 70args = ap .parse_args (argv ) 71want = build () 72if args .check : 73have = open (OUT ,encoding = "utf-8" ,newline = "" ).read ().replace ("\r\n" ,"\n" )if os .path .exists (OUT )else "" 74if have != want : 75"FAIL bench/results/README.md is out of date (run python tools/bench_results_index.py)" ) 76return 1 77"bench results index: ok" ) 78return 0 79with open (OUT ,"w" ,encoding = "utf-8" ,newline = "\n" )as f : 80f .write (want ) 81"wrote %s" % OUT ) 82return 0 83 84 85if __name__ == "__main__" : 86sys .exit (main ())