|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +Analyze TypeForm parsing efficiency from mypy build stats. |
| 4 | +
|
| 5 | +Usage: |
| 6 | + python3 analyze_typeform_stats.py '<mypy_output_with_stats>' |
| 7 | + python3 -m mypy --dump-build-stats file.py 2>&1 | python3 analyze_typeform_stats.py |
| 8 | +
|
| 9 | +Example output: |
| 10 | + TypeForm Expression Parsing Statistics: |
| 11 | + ================================================== |
| 12 | + Total calls to SA.try_parse_as_type_expression: 14,555 |
| 13 | + Quick rejections (no full parse): 14,255 |
| 14 | + Full parses attempted: 300 |
| 15 | + - Successful: 248 |
| 16 | + - Failed: 52 |
| 17 | +
|
| 18 | + Efficiency Metrics: |
| 19 | + - Quick rejection rate: 97.9% |
| 20 | + - Full parse rate: 2.1% |
| 21 | + - Full parse success rate: 82.7% |
| 22 | + - Overall success rate: 1.7% |
| 23 | +
|
| 24 | + Performance Implications: |
| 25 | + - Expensive failed full parses: 52 (0.4% of all calls) |
| 26 | +
|
| 27 | +See also: |
| 28 | + - mypy/semanal.py: SemanticAnalyzer.try_parse_as_type_expression() |
| 29 | + - mypy/semanal.py: DEBUG_TYPE_EXPRESSION_FULL_PARSE_FAILURES |
| 30 | +""" |
| 31 | + |
| 32 | +import re |
| 33 | +import sys |
| 34 | + |
| 35 | + |
| 36 | +def analyze_stats(output: str) -> None: |
| 37 | + """Parse mypy stats output and calculate TypeForm parsing efficiency.""" |
| 38 | + |
| 39 | + # Extract the three counters |
| 40 | + total_match = re.search(r"type_expression_parse_count:\s*(\d+)", output) |
| 41 | + success_match = re.search(r"type_expression_full_parse_success_count:\s*(\d+)", output) |
| 42 | + failure_match = re.search(r"type_expression_full_parse_failure_count:\s*(\d+)", output) |
| 43 | + |
| 44 | + if not (total_match and success_match and failure_match): |
| 45 | + print("Error: Could not find all required counters in output") |
| 46 | + return |
| 47 | + |
| 48 | + total = int(total_match.group(1)) |
| 49 | + successes = int(success_match.group(1)) |
| 50 | + failures = int(failure_match.group(1)) |
| 51 | + |
| 52 | + full_parses = successes + failures |
| 53 | + |
| 54 | + print("TypeForm Expression Parsing Statistics:") |
| 55 | + print("=" * 50) |
| 56 | + print(f"Total calls to SA.try_parse_as_type_expression: {total:,}") |
| 57 | + print(f"Quick rejections (no full parse): {total - full_parses:,}") |
| 58 | + print(f"Full parses attempted: {full_parses:,}") |
| 59 | + print(f" - Successful: {successes:,}") |
| 60 | + print(f" - Failed: {failures:,}") |
| 61 | + if total > 0: |
| 62 | + print() |
| 63 | + print("Efficiency Metrics:") |
| 64 | + print(f" - Quick rejection rate: {((total - full_parses) / total * 100):.1f}%") |
| 65 | + print(f" - Full parse rate: {(full_parses / total * 100):.1f}%") |
| 66 | + print(f" - Full parse success rate: {(successes / full_parses * 100):.1f}%") |
| 67 | + print(f" - Overall success rate: {(successes / total * 100):.1f}%") |
| 68 | + print() |
| 69 | + print("Performance Implications:") |
| 70 | + print( |
| 71 | + f" - Expensive failed full parses: {failures:,} ({(failures / total * 100):.1f}% of all calls)" |
| 72 | + ) |
| 73 | + |
| 74 | + |
| 75 | +if __name__ == "__main__": |
| 76 | + if len(sys.argv) == 1: |
| 77 | + # Read from stdin |
| 78 | + output = sys.stdin.read() |
| 79 | + elif len(sys.argv) == 2: |
| 80 | + # Read from command line argument |
| 81 | + output = sys.argv[1] |
| 82 | + else: |
| 83 | + print("Usage: python3 analyze_typeform_stats.py [mypy_output_with_stats]") |
| 84 | + print("Examples:") |
| 85 | + print( |
| 86 | + " python3 -m mypy --dump-build-stats file.py 2>&1 | python3 analyze_typeform_stats.py" |
| 87 | + ) |
| 88 | + print(" python3 analyze_typeform_stats.py 'output_string'") |
| 89 | + sys.exit(1) |
| 90 | + |
| 91 | + analyze_stats(output) |
0 commit comments