1 """util.py - General utilities for running, loading, and processing benchmarks
9 # Input file type enumeration
14 _num_magic_bytes
= 2 if sys
.platform
.startswith('win') else 4
15 def is_executable_file(filename
):
17 Return 'True' if 'filename' names a valid file which is likely
18 an executable. A file is considered an executable if it starts with the
19 magic bytes for a EXE, Mach O, or ELF file.
21 if not os
.path
.isfile(filename
):
23 with
open(filename
, mode
='rb') as f
:
24 magic_bytes
= f
.read(_num_magic_bytes
)
25 if sys
.platform
== 'darwin':
26 return magic_bytes
in [
27 b
'\xfe\xed\xfa\xce', # MH_MAGIC
28 b
'\xce\xfa\xed\xfe', # MH_CIGAM
29 b
'\xfe\xed\xfa\xcf', # MH_MAGIC_64
30 b
'\xcf\xfa\xed\xfe', # MH_CIGAM_64
31 b
'\xca\xfe\xba\xbe', # FAT_MAGIC
32 b
'\xbe\xba\xfe\xca' # FAT_CIGAM
34 elif sys
.platform
.startswith('win'):
35 return magic_bytes
== b
'MZ'
37 return magic_bytes
== b
'\x7FELF'
40 def is_json_file(filename
):
42 Returns 'True' if 'filename' names a valid JSON output file.
46 with
open(filename
, 'r') as f
:
54 def classify_input_file(filename
):
56 Return a tuple (type, msg) where 'type' specifies the classified type
57 of 'filename'. If 'type' is 'IT_Invalid' then 'msg' is a human readable
58 string represeting the error.
62 if not os
.path
.exists(filename
):
63 err_msg
= "'%s' does not exist" % filename
64 elif not os
.path
.isfile(filename
):
65 err_msg
= "'%s' does not name a file" % filename
66 elif is_executable_file(filename
):
68 elif is_json_file(filename
):
71 err_msg
= "'%s' does not name a valid benchmark executable or JSON file" % filename
75 def check_input_file(filename
):
77 Classify the file named by 'filename' and return the classification.
78 If the file is classified as 'IT_Invalid' print an error message and exit
81 ftype
, msg
= classify_input_file(filename
)
82 if ftype
== IT_Invalid
:
83 print("Invalid input file: %s" % msg
)
87 def find_benchmark_flag(prefix
, benchmark_flags
):
89 Search the specified list of flags for a flag matching `<prefix><arg>` and
90 if it is found return the arg it specifies. If specified more than once the
91 last value is returned. If the flag is not found None is returned.
93 assert prefix
.startswith('--') and prefix
.endswith('=')
95 for f
in benchmark_flags
:
96 if f
.startswith(prefix
):
97 result
= f
[len(prefix
):]
100 def remove_benchmark_flags(prefix
, benchmark_flags
):
102 Return a new list containing the specified benchmark_flags except those
103 with the specified prefix.
105 assert prefix
.startswith('--') and prefix
.endswith('=')
106 return [f
for f
in benchmark_flags
if not f
.startswith(prefix
)]
108 def load_benchmark_results(fname
):
110 Read benchmark output from a file and return the JSON object.
111 REQUIRES: 'fname' names a file containing JSON benchmark output.
113 with
open(fname
, 'r') as f
:
117 def run_benchmark(exe_name
, benchmark_flags
):
119 Run a benchmark specified by 'exe_name' with the specified
120 'benchmark_flags'. The benchmark is run directly as a subprocess to preserve
121 real time console output.
122 RETURNS: A JSON object representing the benchmark output
124 output_name
= find_benchmark_flag('--benchmark_out=',
126 is_temp_output
= False
127 if output_name
is None:
128 is_temp_output
= True
129 thandle
, output_name
= tempfile
.mkstemp()
131 benchmark_flags
= list(benchmark_flags
) + \
132 ['--benchmark_out=%s' % output_name
]
134 cmd
= [exe_name
] + benchmark_flags
135 print("RUNNING: %s" % ' '.join(cmd
))
136 exitCode
= subprocess
.call(cmd
)
138 print('TEST FAILED...')
140 json_res
= load_benchmark_results(output_name
)
142 os
.unlink(output_name
)
146 def run_or_load_benchmark(filename
, benchmark_flags
):
148 Get the results for a specified benchmark. If 'filename' specifies
149 an executable benchmark then the results are generated by running the
150 benchmark. Otherwise 'filename' must name a valid JSON output file,
151 which is loaded and the result returned.
153 ftype
= check_input_file(filename
)
155 return load_benchmark_results(filename
)
156 elif ftype
== IT_Executable
:
157 return run_benchmark(filename
, benchmark_flags
)
159 assert False # This branch is unreachable