2016-08-09 18:33:57 +00:00
|
|
|
"""util.py - General utilities for running, loading, and processing benchmarks
|
|
|
|
"""
|
|
|
|
import json
|
|
|
|
import os
|
2023-02-06 16:57:07 +00:00
|
|
|
import re
|
2016-08-09 18:33:57 +00:00
|
|
|
import subprocess
|
|
|
|
import sys
|
2023-02-06 16:57:07 +00:00
|
|
|
import tempfile
|
|
|
|
|
2016-08-09 18:33:57 +00:00
|
|
|
|
|
|
|
# Input file type enumeration
|
2018-12-07 13:34:00 +00:00
|
|
|
IT_Invalid = 0
|
|
|
|
IT_JSON = 1
|
2016-08-09 18:33:57 +00:00
|
|
|
IT_Executable = 2
|
|
|
|
|
|
|
|
_num_magic_bytes = 2 if sys.platform.startswith('win') else 4
|
2018-12-07 13:34:00 +00:00
|
|
|
|
|
|
|
|
2016-08-09 18:33:57 +00:00
|
|
|
def is_executable_file(filename):
|
|
|
|
"""
|
|
|
|
Return 'True' if 'filename' names a valid file which is likely
|
|
|
|
an executable. A file is considered an executable if it starts with the
|
|
|
|
magic bytes for a EXE, Mach O, or ELF file.
|
|
|
|
"""
|
|
|
|
if not os.path.isfile(filename):
|
|
|
|
return False
|
2017-03-29 10:39:18 +00:00
|
|
|
with open(filename, mode='rb') as f:
|
2016-08-09 18:33:57 +00:00
|
|
|
magic_bytes = f.read(_num_magic_bytes)
|
|
|
|
if sys.platform == 'darwin':
|
|
|
|
return magic_bytes in [
|
2017-03-29 10:39:18 +00:00
|
|
|
b'\xfe\xed\xfa\xce', # MH_MAGIC
|
|
|
|
b'\xce\xfa\xed\xfe', # MH_CIGAM
|
|
|
|
b'\xfe\xed\xfa\xcf', # MH_MAGIC_64
|
|
|
|
b'\xcf\xfa\xed\xfe', # MH_CIGAM_64
|
|
|
|
b'\xca\xfe\xba\xbe', # FAT_MAGIC
|
|
|
|
b'\xbe\xba\xfe\xca' # FAT_CIGAM
|
2016-08-09 18:33:57 +00:00
|
|
|
]
|
|
|
|
elif sys.platform.startswith('win'):
|
2017-03-29 10:39:18 +00:00
|
|
|
return magic_bytes == b'MZ'
|
2016-08-09 18:33:57 +00:00
|
|
|
else:
|
2017-03-29 10:39:18 +00:00
|
|
|
return magic_bytes == b'\x7FELF'
|
2016-08-09 18:33:57 +00:00
|
|
|
|
|
|
|
|
|
|
|
def is_json_file(filename):
|
|
|
|
"""
|
|
|
|
Returns 'True' if 'filename' names a valid JSON output file.
|
|
|
|
'False' otherwise.
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
with open(filename, 'r') as f:
|
|
|
|
json.load(f)
|
|
|
|
return True
|
2018-12-07 13:34:00 +00:00
|
|
|
except BaseException:
|
2016-08-09 18:33:57 +00:00
|
|
|
pass
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
|
def classify_input_file(filename):
|
|
|
|
"""
|
|
|
|
Return a tuple (type, msg) where 'type' specifies the classified type
|
|
|
|
of 'filename'. If 'type' is 'IT_Invalid' then 'msg' is a human readable
|
2023-01-10 12:25:32 +00:00
|
|
|
string representing the error.
|
2016-08-09 18:33:57 +00:00
|
|
|
"""
|
|
|
|
ftype = IT_Invalid
|
|
|
|
err_msg = None
|
|
|
|
if not os.path.exists(filename):
|
|
|
|
err_msg = "'%s' does not exist" % filename
|
|
|
|
elif not os.path.isfile(filename):
|
|
|
|
err_msg = "'%s' does not name a file" % filename
|
|
|
|
elif is_executable_file(filename):
|
|
|
|
ftype = IT_Executable
|
|
|
|
elif is_json_file(filename):
|
|
|
|
ftype = IT_JSON
|
|
|
|
else:
|
2017-03-29 10:39:18 +00:00
|
|
|
err_msg = "'%s' does not name a valid benchmark executable or JSON file" % filename
|
2016-08-09 18:33:57 +00:00
|
|
|
return ftype, err_msg
|
|
|
|
|
|
|
|
|
|
|
|
def check_input_file(filename):
|
|
|
|
"""
|
|
|
|
Classify the file named by 'filename' and return the classification.
|
|
|
|
If the file is classified as 'IT_Invalid' print an error message and exit
|
|
|
|
the program.
|
|
|
|
"""
|
|
|
|
ftype, msg = classify_input_file(filename)
|
|
|
|
if ftype == IT_Invalid:
|
2017-03-29 10:39:18 +00:00
|
|
|
print("Invalid input file: %s" % msg)
|
2016-08-09 18:33:57 +00:00
|
|
|
sys.exit(1)
|
|
|
|
return ftype
|
|
|
|
|
2018-12-07 13:34:00 +00:00
|
|
|
|
2016-11-18 22:42:02 +00:00
|
|
|
def find_benchmark_flag(prefix, benchmark_flags):
|
|
|
|
"""
|
|
|
|
Search the specified list of flags for a flag matching `<prefix><arg>` and
|
|
|
|
if it is found return the arg it specifies. If specified more than once the
|
|
|
|
last value is returned. If the flag is not found None is returned.
|
|
|
|
"""
|
|
|
|
assert prefix.startswith('--') and prefix.endswith('=')
|
|
|
|
result = None
|
|
|
|
for f in benchmark_flags:
|
|
|
|
if f.startswith(prefix):
|
|
|
|
result = f[len(prefix):]
|
|
|
|
return result
|
|
|
|
|
2018-12-07 13:34:00 +00:00
|
|
|
|
2016-11-18 22:42:02 +00:00
|
|
|
def remove_benchmark_flags(prefix, benchmark_flags):
|
|
|
|
"""
|
|
|
|
Return a new list containing the specified benchmark_flags except those
|
|
|
|
with the specified prefix.
|
|
|
|
"""
|
|
|
|
assert prefix.startswith('--') and prefix.endswith('=')
|
|
|
|
return [f for f in benchmark_flags if not f.startswith(prefix)]
|
2016-08-09 18:33:57 +00:00
|
|
|
|
2018-12-07 13:34:00 +00:00
|
|
|
|
2023-02-06 16:57:07 +00:00
|
|
|
def load_benchmark_results(fname, benchmark_filter):
|
2016-08-09 18:33:57 +00:00
|
|
|
"""
|
|
|
|
Read benchmark output from a file and return the JSON object.
|
2023-02-06 16:57:07 +00:00
|
|
|
|
|
|
|
Apply benchmark_filter, a regular expression, with nearly the same
|
|
|
|
semantics of the --benchmark_filter argument. May be None.
|
|
|
|
Note: the Python regular expression engine is used instead of the
|
|
|
|
one used by the C++ code, which may produce different results
|
|
|
|
in complex cases.
|
|
|
|
|
2016-08-09 18:33:57 +00:00
|
|
|
REQUIRES: 'fname' names a file containing JSON benchmark output.
|
|
|
|
"""
|
2023-02-06 16:57:07 +00:00
|
|
|
def benchmark_wanted(benchmark):
|
|
|
|
if benchmark_filter is None:
|
|
|
|
return True
|
|
|
|
name = benchmark.get('run_name', None) or benchmark['name']
|
|
|
|
if re.search(benchmark_filter, name):
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
2016-08-09 18:33:57 +00:00
|
|
|
with open(fname, 'r') as f:
|
2023-02-06 16:57:07 +00:00
|
|
|
results = json.load(f)
|
|
|
|
if 'benchmarks' in results:
|
|
|
|
results['benchmarks'] = list(filter(benchmark_wanted,
|
|
|
|
results['benchmarks']))
|
|
|
|
return results
|
2016-08-09 18:33:57 +00:00
|
|
|
|
|
|
|
|
2021-06-03 13:22:52 +00:00
|
|
|
def sort_benchmark_results(result):
|
|
|
|
benchmarks = result['benchmarks']
|
|
|
|
|
|
|
|
# From inner key to the outer key!
|
|
|
|
benchmarks = sorted(
|
|
|
|
benchmarks, key=lambda benchmark: benchmark['repetition_index'] if 'repetition_index' in benchmark else -1)
|
|
|
|
benchmarks = sorted(
|
|
|
|
benchmarks, key=lambda benchmark: 1 if 'run_type' in benchmark and benchmark['run_type'] == "aggregate" else 0)
|
|
|
|
benchmarks = sorted(
|
|
|
|
benchmarks, key=lambda benchmark: benchmark['per_family_instance_index'] if 'per_family_instance_index' in benchmark else -1)
|
|
|
|
benchmarks = sorted(
|
|
|
|
benchmarks, key=lambda benchmark: benchmark['family_index'] if 'family_index' in benchmark else -1)
|
|
|
|
|
|
|
|
result['benchmarks'] = benchmarks
|
|
|
|
return result
|
|
|
|
|
|
|
|
|
2016-08-09 18:33:57 +00:00
|
|
|
def run_benchmark(exe_name, benchmark_flags):
|
|
|
|
"""
|
|
|
|
Run a benchmark specified by 'exe_name' with the specified
|
|
|
|
'benchmark_flags'. The benchmark is run directly as a subprocess to preserve
|
|
|
|
real time console output.
|
|
|
|
RETURNS: A JSON object representing the benchmark output
|
|
|
|
"""
|
2016-11-18 22:42:02 +00:00
|
|
|
output_name = find_benchmark_flag('--benchmark_out=',
|
|
|
|
benchmark_flags)
|
|
|
|
is_temp_output = False
|
|
|
|
if output_name is None:
|
|
|
|
is_temp_output = True
|
|
|
|
thandle, output_name = tempfile.mkstemp()
|
|
|
|
os.close(thandle)
|
|
|
|
benchmark_flags = list(benchmark_flags) + \
|
2018-12-07 13:34:00 +00:00
|
|
|
['--benchmark_out=%s' % output_name]
|
2016-11-18 22:42:02 +00:00
|
|
|
|
2016-08-09 18:33:57 +00:00
|
|
|
cmd = [exe_name] + benchmark_flags
|
|
|
|
print("RUNNING: %s" % ' '.join(cmd))
|
2016-11-18 22:42:02 +00:00
|
|
|
exitCode = subprocess.call(cmd)
|
2016-08-09 18:33:57 +00:00
|
|
|
if exitCode != 0:
|
|
|
|
print('TEST FAILED...')
|
|
|
|
sys.exit(exitCode)
|
2023-02-06 16:57:07 +00:00
|
|
|
json_res = load_benchmark_results(output_name, None)
|
2016-11-18 22:42:02 +00:00
|
|
|
if is_temp_output:
|
|
|
|
os.unlink(output_name)
|
2016-08-09 18:33:57 +00:00
|
|
|
return json_res
|
|
|
|
|
|
|
|
|
|
|
|
def run_or_load_benchmark(filename, benchmark_flags):
|
|
|
|
"""
|
|
|
|
Get the results for a specified benchmark. If 'filename' specifies
|
|
|
|
an executable benchmark then the results are generated by running the
|
|
|
|
benchmark. Otherwise 'filename' must name a valid JSON output file,
|
|
|
|
which is loaded and the result returned.
|
|
|
|
"""
|
|
|
|
ftype = check_input_file(filename)
|
|
|
|
if ftype == IT_JSON:
|
2023-02-06 16:57:07 +00:00
|
|
|
benchmark_filter = find_benchmark_flag('--benchmark_filter=',
|
|
|
|
benchmark_flags)
|
|
|
|
return load_benchmark_results(filename, benchmark_filter)
|
2020-09-09 08:43:26 +00:00
|
|
|
if ftype == IT_Executable:
|
2016-08-09 18:33:57 +00:00
|
|
|
return run_benchmark(filename, benchmark_flags)
|
2020-09-09 08:43:26 +00:00
|
|
|
raise ValueError('Unknown file type %s' % ftype)
|