-
Notifications
You must be signed in to change notification settings - Fork 2
/
marky.py
executable file
·542 lines (448 loc) · 18.5 KB
/
marky.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
#!/usr/bin/env python
import os, subprocess, re, sys, string, json, argparse, datetime, time, signal, tempfile
import types
import stats, mailer, args, ecd
#import speedup
from config import config
from debug import debug_msg, error_msg, warning_msg
# Codes used by the execute_and_capture_output methods.
TIMEOUT_ERROR = 1
FAILURE_ERROR = 2
FILTER_FAILED_ERROR = 3
# Function used to capture all output from a program it executes.
# Executes the whole program before returning any output.
def execute_and_capture_output(program):
output = None
try:
output = subprocess.check_output(program, shell=True, stderr=subprocess.STDOUT)
except subprocess.CalledProcessError as e:
# TODO: Work out how on earth I get information about the error!
raise Exception("Run failed!", FAILURE_ERROR)
return str(output)
def execute_and_capture_output_with_timeout(program, timeout):
try:
output_file = tempfile.SpooledTemporaryFile()
start = datetime.datetime.now()
process = subprocess.Popen(program, shell=True, stdout=output_file, stderr=subprocess.STDOUT)
while process.poll() is None:
time.sleep(0.5)
now = datetime.datetime.now()
if (now - start).seconds > timeout:
warning_msg("Timing out! (" + str((now-start).seconds) + "s)")
os.kill(process.pid, signal.SIGKILL)
os.waitpid(-1, os.WNOHANG)
raise Exception("Run timed out!", TIMEOUT_ERROR)
output_file.seek(0)
raw = output_file.read()
output_file.close()
return str(raw)
except subprocess.CalledProcessError as e:
# TODO: Work out how on earth I get information about the error!
raise Exception("Run failed!", FAILURE_ERROR)
# Implementation of pushd
directories = []
def enter_directory(directory):
global directories
directories.append(os.getcwd())
try:
os.chdir(directory)
except Error:
directories.pop()
# Implementation of popd
def leave_directory():
global directories
os.chdir(directories.pop())
# This program will run a filter (regular expression) on benchmark output,
# and will return the first value that was captured within parens.
def run_filter(raw, filter_function):
match = re.search(filter_function, raw)
if match:
return match.group(1)
else:
warning_msg("Filter returned None! Filter was: " + filter_function.pattern)
raise Exception("Filter failed!", FILTER_FAILED_ERROR)
# Should do similar to the above, but return a list of all matches
# that were between parens.
def run_multi_filter(raw, filter_function):
pass
# Attempt to convert a string of data to the correct type.
def convert_data(s):
n = s
try:
n = int(s)
# s must've been an int!
except ValueError:
try:
n = float(s)
# s must've been a float!
except ValueError:
# s isn't a float or an int, assume it's a string.
n = s
return n
# For a run, converts all the data in it to the correct type.
def cleanup_run(run):
for field in run.keys():
run[field] = convert_data(run[field])
return run
def get_raw_filename(name, iteration):
filename = name.replace(" ", "")
filename = filename.replace("/", "_")
filename = filename.replace(":", "_")
filename += "-i" + str(iteration+1)
return filename
def check_raw_output_exists(name, iteration):
if config["loadraw"]:
filename = get_raw_filename(name, iteration)
load_location = config["loadraw_dir"] + "/" + filename
return os.path.exists(load_location)
else:
return False
def save_raw_output(name, iteration, raw):
directory = config["saveraw_dir"]
if not os.path.exists(directory):
debug_msg(3, "Raw output directory doesn't exist! Creating...")
os.mkdir(directory)
filename = get_raw_filename(name, iteration)
save_location = directory + "/" + filename
f = open(save_location, "w")
f.write(raw)
f.close()
debug_msg(3, "Saved raw output to " + save_location)
def load_raw_output(invocation, iteration):
directory = config["loadraw_dir"]
filename = get_raw_filename(invocation, iteration)
load_location = directory + "/" + filename
f = open(load_location, "r")
raw = f.read()
f.close()
return raw
# This converts the provided results table to a string containing a CSV representation.
def convert_to_csv(results):
# TODO: work out how to produce the headings
s = '"experiment_name","benchmark","i",\n'
for (exp_name, exp) in results["experiments"].items():
for (bm_name, bm) in exp["benchmarks"].items():
run_counter = 1
for run in bm["runs"]:
s += '"' + exp_name + '","' + bm_name + '",' + str(run_counter) + ','
s += ','.join(map(lambda v: str(v), run.values()))
s += "\n"
run_counter += 1
if "aggregates" in bm:
for (agg_name, agg) in bm["aggregates"].items():
s += '"' + exp_name + '","' + bm_name + '","**' + agg_name + '**",'
s += ','.join(map(lambda v: str(v), agg.values()))
s += "\n"
return s
# TODO: implement this
def convert_to_widecsv(results):
pass
# This method modifies one of the config files of the program being run.
# all instances of 'pattern' have been replaced by the given 'value'.
def update_based_on_file_argument(template_file, output_file, pattern, value):
assert os.path.exists(template_file)
assert os.path.exists(output_file)
tf = open(template_file, "r")
of = open(output_file, "w")
modified = False
for line in tf.readlines():
newline = line.replace(pattern, str(value))
if newline != line:
of.write(newline)
modified = True
else:
of.write(line)
if not modified:
error_msg('Template file "' + template_file + '" did not contain instance of pattern "' + pattern + '".')
of.close()
tf.close()
# TODO: Add caching here?
# This reads arguments for use when executing a benchmark from the given filename.
# This returned string is expected to replace the name of the benchmark, so should contain it!
def get_arguments_from_file(filename):
assert os.path.exists(filename)
f = open(filename, "r")
args = f.read().strip()
f.close()
return args
def execute_post_function(post_function, post_function_arguments, name, iteration, timestamp):
actual_post_function_arguments = []
for arg in post_function_arguments:
# Awesome hack!!1
if (arg == "GETUNIQUEFILENAME"):
arg = "arcsimMemProfile-" + get_raw_filename(name, iteration) + ".csv"
elif (arg == "GETTIMESTAMP"):
arg = timestamp
if (isinstance(arg, types.FunctionType)):
actual_post_function_arguments.append(arg())
else:
actual_post_function_arguments.append(arg)
post_function(*actual_post_function_arguments)
# -----
# Functions that should be exposed to main are below:
# -----
# This function is actually used to execute the experiments.
# It returns a currently-amorphous table of information about the experiments.
def run(suite):
# The amorphous monstrosity of a table
total_result_table = {}
total_result_table["experiments"] = {}
# Go through the programs...
for (program_alias, program) in suite.programs.items():
# Check if there's argument variables that will require iterating over
if ((len(suite.argument_variables) + len(suite.file_argument_variables)) == 0):
exp_name = program_alias
# There were none, so simply run this program with no extra arguments
debug_msg(1, "BEGIN EXPERIMENT: " + exp_name)
total_result_table["experiments"][exp_name] = run_experiment(suite, program, program_alias, exp_name)
else:
# There are some, so use args.get_experiment_arguments to get a list of all combos, iterate over them.
for experiment_arguments in args.get_experiment_arguments(suite.argument_variables, suite.file_argument_variables):
exp_params = ""
experiment_arguments_string = ""
for exp_arg in experiment_arguments:
(name, value, is_file_arg_var) = exp_arg
if is_file_arg_var:
exp_params += (name + ":" + str(value) + " ")
# change a config file
(temp_file, output_file, pattern, values) = suite.file_argument_variables[name]
update_based_on_file_argument(temp_file, output_file, pattern, value)
else:
exp_params += args.emit_argument(name, value)
experiment_arguments_string += args.emit_argument(name, value)
exp_name = program_alias + " " + exp_params
debug_msg(1, "BEGIN EXPERIMENT: " + exp_name)
total_result_table["experiments"][exp_name] = run_experiment(suite, program, program_alias, exp_name, experiment_arguments_string)
return total_result_table
# An "experiment" is categorised as any program + the arguments we wish to use for that program.
def run_experiment(suite, program, program_alias, exp_name, experiment_arguments = ""):
experiment_table = {}
experiment_table["benchmarks"] = {}
# Convert the dict of benchmark groups, to a list of benchmark tuples called actual_benchmarks
# We will then iterate over that.
actual_benchmarks = []
for (group_name, group_benchmarks) in suite.benchmarks.items():
for (name, directory, executescript, timeout) in group_benchmarks:
actual_benchmarks.append((group_name, name, directory, executescript, timeout))
# Go through the benchmarks...
for (group_name, benchmark, directory, executescript, timeout) in actual_benchmarks:
benchmark_name = group_name + " ++ " + benchmark
enter_directory(directory)
debug_msg(3, "Entered into " + os.getcwd())
if (executescript):
benchmark = get_arguments_from_file(executescript)
# Construct the command used to execute the benchmark
invocation = " ".join([suite.pre_arguments, program, suite.core_arguments, experiment_arguments, suite.benchmark_argument, benchmark])
if config["should_warmup"]:
debug_msg(1, "RUN: '" + invocation + "' (WARMUP)")
try:
if timeout:
execute_and_capture_output_with_timeout(invocation, timeout)
else:
execute_and_capture_output(invocation)
except Exception as e:
if e.args[1] == TIMEOUT_ERROR:
debug_msg(1, "Run timed out... (timeout is " + str(timeout) + "s)")
timedout_iterations += 1
failed_iterations += 1
if e.args[1] == FAILURE_ERROR:
debug_msg(1, "Run failed...")
failed_iterations += 1
# We store all the runs in here.
run_table = []
failed_iterations = 0
timedout_iterations = 0
# Go through the iterations...
for i in range(suite.iterations):
# This stores the fields collected for this run.
run = {}
debug_msg(1, "RUN: '" + invocation + "' (ITER: " + str(i+1) + "/" + str(suite.iterations) + ")")
# string containing the raw output from the benchmark
raw = ""
try:
raw_output_name = exp_name + experiment_arguments + benchmark
start = datetime.datetime.now()
timestamp = time.time() * 1000
# Actually execute the benchmark
if config["loadraw"] and check_raw_output_exists(raw_output_name, i):
raw = load_raw_output(raw_output_name, i)
debug_msg(1, "(loaded from file...)")
elif timeout:
raw = execute_and_capture_output_with_timeout(invocation, timeout)
else:
raw = execute_and_capture_output(invocation)
end = datetime.datetime.now()
# Save the output, if required
if config["saveraw"]:
save_raw_output(raw_output_name, i, raw)
# Now collect the fields using our provided filters.
for (field, field_filter) in suite.filters.items():
run[field] = run_filter(raw, field_filter)
if not config["loadraw"]:
delta = end - start
runtime = float(delta.seconds)
runtime += ( float((delta.microseconds)) / 1000000.0 )
debug_msg(2, "Took " + str(runtime) + " seconds.")
if config["should_time"] and not config["loadraw"]:
run["marky runtime"] = str(runtime)
# Collected data is all strings currently; convert to the correct types.
run = cleanup_run(run)
# Save this run
run_table.append(run)
except Exception as e:
if len(e.args) < 2:
raise e
if e.args[1] == TIMEOUT_ERROR:
debug_msg(1, "Run timed out... (timeout is " + str(timeout) + "s)")
timedout_iterations += 1
failed_iterations += 1
if e.args[1] == FAILURE_ERROR:
debug_msg(1, "Run failed...")
failed_iterations += 1
if e.args[1] == FILTER_FAILED_ERROR:
debug_msg(1, "Filter failed...")
failed_iterations += 1
if ("post_function" in suite.__dict__ and "post_function_arguments" in suite.__dict__):
execute_post_function(suite.post_function, suite.post_function_arguments, raw_output_name, i, timestamp)
# Finished running this benchmark for X iterations...
benchmark_result = {}
benchmark_result["successes"] = len(run_table)
benchmark_result["failures"] = failed_iterations
benchmark_result["timeouts"] = timedout_iterations
benchmark_result["attempts"] = len(run_table) + failed_iterations
# Collect results from runs
if len(run_table) > 0:
benchmark_result["runs"] = run_table
leave_directory()
debug_msg(3, "Exited back to " + os.getcwd())
# Save this benchmark in the benchmark table
experiment_table["benchmarks"][benchmark_name] = benchmark_result
return experiment_table
def print_results(results, formatter=json.dumps):
print(formatter(results))
def save_results(filename, results, formatter=json.dumps):
f = open(filename, "w")
f.write(formatter(results))
f.close()
# Email the results!
email_results = mailer.send_email
formats = ["csv", "json", "widecsv"]
formatters = {"csv": convert_to_csv, "json": json.dumps, "widecsv": convert_to_widecsv}
default_print_format = "json"
default_save_format = "json"
default_email_format = "json"
#
# MAIN FUNCTION
#
def main():
parser = argparse.ArgumentParser(
description = "marky - a benchmark execution and statistics gathering framework")
parser.add_argument('file', type=str, nargs=1, metavar='FILE',
help='A file containing an execution configuration. (or ECD) (Minus the .py)')
parser.add_argument('--disable-aggregation', '-a', dest='disable_agg', action='store_true',
help='Turn off aggregation calculation for this session.')
parser.add_argument('--warmup', '-w', dest='should_warmup', action='store_true',
help='Perform a warmup run of each benchmark that is not recorded.')
parser.add_argument('--time', '-t', dest='should_time', action='store_true',
help='Use marky to measure the runtime of any experiments.')
parser.add_argument('--explain', '-e', dest='should_explain', action='store_true',
help='Explain the experiments that will be run by the provided ECD.')
parser.add_argument('--print', '-p', dest='should_print', action='store_true',
help='"Pretty-print" the results.')
parser.add_argument('--print-format', '-pf', dest='printfmt', nargs=1, choices=formats,
help='Choose which format to print the data in. (default: json)')
parser.add_argument('--load', '-l', dest='should_load', nargs=1, metavar='FILE',
help='Load previous results from a file. (supports JSON only)')
parser.add_argument('--load-raw', '-lr', dest='should_loadraw', nargs=1, metavar='DIR',
help='Load the raw output from each run from the given directory.')
parser.add_argument('--save', '-s', dest='should_save', nargs=1, metavar='FILE',
help='Output the results into a file.')
parser.add_argument('--save-format', '-sf', dest='savefmt', nargs=1, choices=formats,
help='Choose which format to save the data in. (default: json)')
parser.add_argument('--email', '-m', dest='should_email', nargs=1, metavar='ADDRESS',
help='Send an email to the address once complete. (Uses localhost unless --mailserver is given.)')
parser.add_argument('--email-format', '-mf', dest='emailfmt', nargs=1, choices=formats,
help='Choose which format to email the data in. (default: json)')
parser.add_argument('--mailserver', '-ms', dest='mailserver', nargs=1, metavar='HOST',
help='Use the provided host as a mailserver.')
parser.add_argument('--save-raw', '-r', dest='should_saveraw', nargs=1, metavar='DIR',
help='Save the raw output from each run into the given directory.')
parser.add_argument('--debug', '-d', dest='debuglevel', nargs=1, type=int, metavar='LEVEL',
help='Set debug info level. 1 = Announce each benchmark invocation. 2 = Include time taken. 3 = Everything else. (Default = 1) (Set to 0 for quiet, or use --quiet.)')
parser.add_argument('--quiet', '-q', dest='quiet', action='store_true',
help='Hide all output (apart from errors.)')
parser.add_argument('--speedups', '-cs', dest='should_calculate_speedups', action='store_true',
help='Assuming only two experiments will be run, calculate the speedups.')
args = parser.parse_args()
config["debuglevel"] = 2
if args.debuglevel:
config["debuglevel"] = args.debuglevel[0]
if args.quiet:
config["debuglevel"] = 0
suite = None
if args.file:
# (ecd = Execution Configuration Description)
ecd_name = args.file[0]
ecd_name = ecd_name.replace(".py", "")
suite = __import__(ecd_name)
ecd.check_ecd(suite)
if args.should_explain:
ecd.explain_ecd(suite)
exit(0)
config["original_dir"] = os.getcwd()
config["saveraw"] = False
if args.should_saveraw:
config["saveraw"] = True
config["saveraw_dir"] = config["original_dir"] + "/" + args.should_saveraw[0]
config["loadraw"] = False
if args.should_loadraw:
config["loadraw"] = True
config["loadraw_dir"] = config["original_dir"] + "/" + args.should_loadraw[0]
if not os.path.exists(config["loadraw_dir"]):
error_msg("Raw results directory required for loading doesn't exist!")
debug_msg(1, "Will load output from " + config["loadraw_dir"])
config["should_warmup"] = False
if args.should_warmup:
config["should_warmup"] = True
config["should_time"] = False
if args.should_time:
config["should_time"] = True
results = None
if args.should_load:
debug_msg(1, "Loading previous results table from " + args.should_load[0])
json_file = open(args.should_load[0], "r")
results = json.load(json_file)
json_file.close()
else:
debug_msg(1, "Running experiment to obtain results!")
os.chdir(suite.benchmark_root)
results = run(suite)
os.chdir(config["original_dir"])
results["description"] = ecd.convert_ecd_to_description(suite)
if not args.disable_agg:
stats.perform_aggregation(suite, results)
#if args.should_calculate_speedups:
# speedup.calculate(results)
if args.should_print:
formatter_name = default_print_format
if args.printfmt:
formatter_name = args.printfmt[0]
formatter = formatters[formatter_name]
print_results(results, formatter=formatter)
if args.should_save:
formatter_name = default_save_format
if args.savefmt:
formatter_name = args.savefmt[0]
formatter = formatters[formatter_name]
save_results(args.should_save[0], results, formatter=formatter)
if args.should_email:
mailserver = 'localhost'
if args.mailserver:
mailserver = args.mailserver[0]
formatter_name = default_email_format
if args.emailfmt:
formatter_name = args.emailfmt[0]
formatter = formatters[formatter_name]
email_results(args.should_email[0], results, mailserver=mailserver, formatter=formatter)
if __name__ == "__main__":
main()