|
2 | 2 | import json |
3 | 3 | import sys |
4 | 4 | import os |
| 5 | +import glob |
5 | 6 |
|
6 | | -def convert_to_markdown(json_file): |
| 7 | +def load_all_summaries(json_files): |
| 8 | + """Load summaries from all JSON files and group by test name""" |
| 9 | + summaries_by_name = {} |
| 10 | + |
| 11 | + for json_file in json_files: |
| 12 | + try: |
| 13 | + with open(json_file, 'r') as f: |
| 14 | + data = json.load(f) |
| 15 | + |
| 16 | + if 'summaries' in data and data['summaries']: |
| 17 | + for summary in data['summaries']: |
| 18 | + name = summary.get('name', 'Unknown Test') |
| 19 | + if name not in summaries_by_name: |
| 20 | + summaries_by_name[name] = [] |
| 21 | + summaries_by_name[name].append(summary) |
| 22 | + except (FileNotFoundError, json.JSONDecodeError) as e: |
| 23 | + print(f"Warning: Error processing {json_file}: {e}", file=sys.stderr) |
| 24 | + continue |
| 25 | + |
| 26 | + return summaries_by_name |
| 27 | + |
| 28 | +def convert_to_markdown(json_files): |
7 | 29 | """Convert performance test JSON results to markdown format""" |
8 | | - try: |
9 | | - with open(json_file, 'r') as f: |
10 | | - data = json.load(f) |
11 | | - except FileNotFoundError: |
| 30 | + summaries_by_name = load_all_summaries(json_files) |
| 31 | + |
| 32 | + if not summaries_by_name: |
12 | 33 | return "## Performance Test Results\n\nNo performance test results found." |
13 | | - except json.JSONDecodeError: |
14 | | - return "## Performance Test Results\n\nError parsing performance test results." |
15 | 34 |
|
16 | 35 | markdown = "## Performance Test Results\n\n" |
17 | 36 |
|
18 | | - if 'summaries' not in data or not data['summaries']: |
19 | | - return markdown + "No test summaries available." |
| 37 | + # Create a table for each test name |
| 38 | + for name, summaries in sorted(summaries_by_name.items()): |
| 39 | + markdown += f"### {name}\n\n" |
| 40 | + markdown += "| Duration (ms) | Max Memory (GB) | Processors | Parameters |\n" |
| 41 | + markdown += "|---------------|-----------------|------------|------------|\n" |
20 | 42 |
|
21 | | - for summary in data['summaries']: |
22 | | - name = summary.get('name', 'Unknown Test') |
23 | | - duration = summary.get('duration', 0) |
24 | | - processors = summary.get('numberOfProcessors', 0) |
25 | | - max_memory = summary.get('maxMemory', 0) |
| 43 | + for summary in summaries: |
| 44 | + duration = summary.get('duration', 0) |
| 45 | + processors = summary.get('numberOfProcessors', 0) |
| 46 | + max_memory = summary.get('maxMemory', 0) |
26 | 47 |
|
27 | | - # Convert memory from bytes to GB |
28 | | - max_memory_gb = max_memory / (1024 ** 3) if max_memory > 0 else 0 |
| 48 | + # Convert memory from bytes to GB |
| 49 | + max_memory_gb = max_memory / (1024 ** 3) if max_memory > 0 else 0 |
| 50 | + |
| 51 | + # Extract dynamic properties (excluding standard fields) |
| 52 | + standard_fields = {'name', 'duration', 'numberOfProcessors', 'maxMemory'} |
| 53 | + params = [] |
| 54 | + for key, value in summary.items(): |
| 55 | + if key not in standard_fields: |
| 56 | + params.append(f"{key}={value}") |
| 57 | + |
| 58 | + params_str = ", ".join(params) if params else "-" |
| 59 | + |
| 60 | + markdown += f"| {duration} | {max_memory_gb:.2f} | {processors} | {params_str} |\n" |
29 | 61 |
|
30 | | - markdown += f"### {name}\n\n" |
31 | | - markdown += "| Metric | Value |\n" |
32 | | - markdown += "|--------|-------|\n" |
33 | | - markdown += f"| Duration | {duration} ms |\n" |
34 | | - markdown += f"| Processors | {processors} |\n" |
35 | | - markdown += f"| Max Memory | {max_memory_gb:.2f} GB |\n" |
36 | 62 | markdown += "\n" |
37 | 63 |
|
38 | 64 | return markdown |
39 | 65 |
|
40 | 66 | if __name__ == "__main__": |
41 | | - if len(sys.argv) != 2: |
42 | | - print("Usage: performance-to-markdown.py <json_file>") |
| 67 | + if len(sys.argv) < 2: |
| 68 | + print("Usage: performance-to-markdown.py <json_file> [json_file2 ...]") |
| 69 | + print(" or: performance-to-markdown.py <glob_pattern>") |
43 | 70 | sys.exit(1) |
44 | 71 |
|
45 | | - json_file = sys.argv[1] |
46 | | - markdown = convert_to_markdown(json_file) |
| 72 | + # Collect all JSON files from arguments (supporting both direct files and glob patterns) |
| 73 | + json_files = [] |
| 74 | + for arg in sys.argv[1:]: |
| 75 | + if '*' in arg: |
| 76 | + json_files.extend(glob.glob(arg, recursive=True)) |
| 77 | + else: |
| 78 | + json_files.append(arg) |
| 79 | + |
| 80 | + # Filter to only existing files |
| 81 | + json_files = [f for f in json_files if os.path.isfile(f)] |
| 82 | + |
| 83 | + if not json_files: |
| 84 | + print("## Performance Test Results\n\nNo performance test results found.") |
| 85 | + sys.exit(0) |
| 86 | + |
| 87 | + markdown = convert_to_markdown(json_files) |
47 | 88 | print(markdown) |
0 commit comments