Path: blob/master/Tools/scripts/build_tests/test_ccache.py
9498 views
#!/usr/bin/env python31# test ccache efficiency building two similar boards2# AP_FLAKE8_CLEAN34import subprocess5import re6import argparse7import sys8import os91011def ccache_stats():12'''return hits/misses from ccache -s'''13hits = 014miss = 015stats = str(subprocess.Popen(["ccache", "-s"], stdout=subprocess.PIPE).communicate()[0], encoding='ascii')16for line in stats.split('\n'):17m = re.match(r"cache.hit\D*(\d+)$", line)18if m is not None:19hits += int(m.group(1))2021m = re.match(r"cache.miss\D*(\d+)", line)22if m is not None:23miss += int(m.group(1))2425m = re.match(r"\s*Hits:\s*(\d+)", line)26if m is not None:27hits += int(m.group(1))2829m = re.match(r"\s*Misses:\s*(\d+)", line)30if m is not None:31miss += int(m.group(1))3233if line.startswith("Primary"):34break35return (hits, miss)363738def build_board(boardname):39subprocess.run(["./waf", "configure", "--board", boardname, '--disable-networking'])40subprocess.run(["./waf", "clean", "copter"])414243def main() -> None:44parser = argparse.ArgumentParser(description='test ccache performance')45parser.add_argument('--boards', default='MatekF405-bdshot,MatekF405-TE-bdshot', help='boards to test')46parser.add_argument('--min-cache-pct', type=int, default=75, help='minimum acceptable ccache hit rate')47parser.add_argument('--display', action='store_true', help='parse and show ccache stats')4849args = parser.parse_args()5051if args.display:52(hits, misses) = ccache_stats()53print("Hits=%u misses=%u" % (hits, misses))54sys.exit(0)5556boards = args.boards.split(",")57if len(boards) != 2:58print(boards)59print("Must specify exactly 2 boards (comma separated)")60sys.exit(1)6162os.environ['CCACHE_DIR'] = os.path.join(os.getcwd(), 'build', 'ccache')63subprocess.run(["ccache", "--version"])64subprocess.run(["ccache", "-C", "-z"])65build_board(boards[0])66subprocess.run(["ccache", "-z"])67build_board(boards[1])68result = subprocess.run(["ccache", "-s"], capture_output=True, text=True)69print(result.stdout)7071# Get the GitHub Actions summary file path72summary_file = os.getenv('GITHUB_STEP_SUMMARY')7374post = ccache_stats()75hit_pct = 100 * post[0] / float(post[0]+post[1])76print("ccache hit percentage: %.1f%% %s" % (hit_pct, post))77if summary_file:78# Append the output to the summary file79with open(summary_file, 'a') as f:80f.write(f"### ccache -s Output with {boards}\n")81f.write(f"```\n{result.stdout}\n```\n")82f.write(f"### ccache hit percentage (min {args.min_cache_pct})\n")83f.write("ccache hit percentage: %.1f%% %s\n" % (hit_pct, post))84if hit_pct < args.min_cache_pct:85print("ccache hits too low, need %d%%" % args.min_cache_pct)86sys.exit(1)87else:88print("ccache hits good")899091if __name__ == "__main__":92main()939495