|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +import argparse |
| 4 | +import csv |
| 5 | +import sys |
| 6 | + |
| 7 | +def parse_table(rows, table_title): |
| 8 | + """ |
| 9 | + Parse a CSV table out of an iterator over rows. |
| 10 | +
|
| 11 | + Return a tuple containing (extracted headers, extracted rows). |
| 12 | + """ |
| 13 | + in_table = False |
| 14 | + rows_iter = iter(rows) |
| 15 | + extracted = [] |
| 16 | + headers = None |
| 17 | + while True: |
| 18 | + try: |
| 19 | + row = next(rows_iter) |
| 20 | + except StopIteration: |
| 21 | + break |
| 22 | + |
| 23 | + if not in_table and row == [table_title]: |
| 24 | + in_table = True |
| 25 | + next_row = next(rows_iter) |
| 26 | + assert next_row == [], f'There should be an empty row after the title of the table, found {next_row}' |
| 27 | + headers = next(rows_iter) # Extract the headers |
| 28 | + continue |
| 29 | + |
| 30 | + elif in_table and row == []: # An empty row marks the end of the table |
| 31 | + in_table = False |
| 32 | + break |
| 33 | + |
| 34 | + elif in_table: |
| 35 | + extracted.append(row) |
| 36 | + |
| 37 | + assert len(extracted) != 0, f'Could not extract rows from the table, this is suspicious. Table title was {table_title}' |
| 38 | + assert headers is not None, f'Could not extract headers from the table, this is suspicious. Table title was {table_title}' |
| 39 | + |
| 40 | + return (headers, extracted) |
| 41 | + |
| 42 | +def main(argv): |
| 43 | + parser = argparse.ArgumentParser( |
| 44 | + prog='parse-spec-results', |
| 45 | + description='Parse SPEC result files (in CSV format) and extract the selected result table, in the selected format.') |
| 46 | + parser.add_argument('filename', type=argparse.FileType('r'), nargs='+', |
| 47 | + help='One of more CSV files to extract the results from. The results parsed from each file are concatenated ' |
| 48 | + 'together, creating a single CSV table.') |
| 49 | + parser.add_argument('--table', type=str, choices=['full', 'selected'], default='full', |
| 50 | + help='The name of the table to extract from SPEC results. `full` means extracting the Full Results Table ' |
| 51 | + 'and `selected` means extracting the Selected Results Table. Default is `full`.') |
| 52 | + parser.add_argument('--output-format', type=str, choices=['csv', 'lnt'], default='csv', |
| 53 | + help='The desired output format for the data. `csv` is CSV format and `lnt` is a format compatible with ' |
| 54 | + '`lnt importreport` (see https://llvm.org/docs/lnt/importing_data.html#importing-data-in-a-text-file).') |
| 55 | + parser.add_argument('--extract', type=str, |
| 56 | + help='A comma-separated list of headers to extract from the table. If provided, only the data associated to ' |
| 57 | + 'those headers will be present in the resulting data. Invalid header names are diagnosed. Please make ' |
| 58 | + 'sure to use appropriate quoting for header names that contain spaces. This option only makes sense ' |
| 59 | + 'when the output format is CSV.') |
| 60 | + parser.add_argument('--keep-not-run', action='store_true', |
| 61 | + help='Keep entries whose \'Base Status\' is marked as \'NR\', aka \'Not Run\'. By default, such entries are discarded.') |
| 62 | + args = parser.parse_args(argv) |
| 63 | + |
| 64 | + if args.table == 'full': |
| 65 | + table_title = 'Full Results Table' |
| 66 | + elif args.table == 'selected': |
| 67 | + table_title = 'Selected Results Table' |
| 68 | + |
| 69 | + # Parse the headers and the rows in each file, aggregating all the results |
| 70 | + headers = None |
| 71 | + rows = [] |
| 72 | + for file in args.filename: |
| 73 | + reader = csv.reader(file) |
| 74 | + (parsed_headers, parsed_rows) = parse_table(reader, table_title) |
| 75 | + assert headers is None or headers == parsed_headers, f'Found files with different headers: {headers} and {parsed_headers}' |
| 76 | + headers = parsed_headers |
| 77 | + rows.extend(parsed_rows) |
| 78 | + |
| 79 | + # Remove rows that were not run unless we were asked to keep them |
| 80 | + if not args.keep_not_run: |
| 81 | + not_run = headers.index('Base Status') |
| 82 | + rows = [row for row in rows if row[not_run] != 'NR'] |
| 83 | + |
| 84 | + if args.extract is not None: |
| 85 | + if args.output_format != 'csv': |
| 86 | + raise RuntimeError('Passing --extract requires the output format to be csv') |
| 87 | + for h in args.extract.split(','): |
| 88 | + if h not in headers: |
| 89 | + raise RuntimeError(f'Header name {h} was not present in the parsed headers {headers}') |
| 90 | + |
| 91 | + extracted_fields = [headers.index(h) for h in args.extract.split(',')] |
| 92 | + headers = [headers[i] for i in extracted_fields] |
| 93 | + rows = [[row[i] for i in extracted_fields] for row in rows] |
| 94 | + |
| 95 | + # Print the results in the right format |
| 96 | + if args.output_format == 'csv': |
| 97 | + writer = csv.writer(sys.stdout) |
| 98 | + writer.writerow(headers) |
| 99 | + for row in rows: |
| 100 | + writer.writerow(row) |
| 101 | + elif args.output_format == 'lnt': |
| 102 | + benchmark = headers.index('Benchmark') |
| 103 | + time = headers.index('Est. Base Run Time') |
| 104 | + for row in rows: |
| 105 | + print(f'{row[benchmark].replace('.', '_')}.execution_time {row[time]}') |
| 106 | + |
| 107 | +if __name__ == '__main__': |
| 108 | + main(sys.argv[1:]) |
0 commit comments