|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +# ===--- Benchmark_DTrace.in ----------------------------------------------===// |
| 4 | +# |
| 5 | +# This source file is part of the Swift.org open source project |
| 6 | +# |
| 7 | +# Copyright (c) 2014 - 2016 Apple Inc. and the Swift project authors |
| 8 | +# Licensed under Apache License v2.0 with Runtime Library Exception |
| 9 | +# |
| 10 | +# See http://swift.org/LICENSE.txt for license information |
| 11 | +# See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 12 | +# |
| 13 | +# ===----------------------------------------------------------------------===// |
| 14 | + |
| 15 | +import os |
| 16 | +import sys |
| 17 | +import subprocess |
| 18 | +import argparse |
| 19 | + |
| 20 | +DRIVER_LIBRARY_PATH = "@PATH_TO_DRIVER_LIBRARY@" |
| 21 | +sys.path.append(DRIVER_LIBRARY_PATH) |
| 22 | +DTRACE_PATH = os.path.join(DRIVER_LIBRARY_PATH, 'swift_stats.d') |
| 23 | + |
| 24 | +import perf_test_driver |
| 25 | + |
| 26 | +# Regexes for the XFAIL_LIST. Matches against '([Onone|O|Ounchecked],TestName)' |
| 27 | +XFAIL_LIST = [ |
| 28 | +] |
| 29 | + |
| 30 | +class DTraceResult(perf_test_driver.Result): |
| 31 | + |
| 32 | + def __init__(self, name, status, output, csv_output): |
| 33 | + perf_test_driver.Result.__init__(self, name, status, output, XFAIL_LIST) |
| 34 | + self.csv_output = csv_output |
| 35 | + |
| 36 | + @classmethod |
| 37 | + def data_headers(cls): |
| 38 | + return ['Name', 'Result', 'strong_retain', 'strong_retain/iter', 'strong_release', 'strong_release/iter'] |
| 39 | + |
| 40 | + @classmethod |
| 41 | + def data_format(cls, max_test_len): |
| 42 | + non_name_headers = DTraceResult.data_headers()[1:] |
| 43 | + fmt = ('{:<%d}' % (max_test_len+5)) + ''.join(['{:<%d}' % (len(h)+2) for h in non_name_headers]) |
| 44 | + return fmt |
| 45 | + |
| 46 | + @classmethod |
| 47 | + def print_data_header(cls, max_test_len, csv_output): |
| 48 | + headers = cls.data_headers() |
| 49 | + if csv_output: |
| 50 | + print(','.join(headers)) |
| 51 | + return |
| 52 | + print(cls.data_format(max_test_len).format(*headers)) |
| 53 | + |
| 54 | + def print_data(self, max_test_len): |
| 55 | + result = [self.get_name(), self.get_result()] + map(str, self.output) |
| 56 | + if self.csv_output: |
| 57 | + print(','.join(result)) |
| 58 | + return |
| 59 | + |
| 60 | + print(DTraceResult.data_format(max_test_len).format(*result)) |
| 61 | + |
| 62 | +class DTracePerfTestDriver(perf_test_driver.PerfTestDriver): |
| 63 | + def __init__(self, binary, xfail_list, csv_output): |
| 64 | + perf_test_driver.PerfTestDriver.__init__(self, binary, xfail_list, |
| 65 | + enable_parallel=False, |
| 66 | + opt_levels = ['O']) |
| 67 | + self.csv_output = csv_output |
| 68 | + |
| 69 | + def print_data_header(self, max_test_len): |
| 70 | + DTraceResult.print_data_header(max_test_len, self.csv_output) |
| 71 | + |
| 72 | + def prepare_input(self, name): |
| 73 | + return {} |
| 74 | + |
| 75 | + def process_input(self, data): |
| 76 | + test_name = '({}_{})'.format(data['opt'], data['test_name']) |
| 77 | + print "Running {}...".format(test_name) |
| 78 | + sys.stdout.flush() |
| 79 | + |
| 80 | + def get_results_with_iters(iters): |
| 81 | + p = subprocess.Popen(['sudo', 'dtrace', '-s', DTRACE_PATH, '-c', '%s %s %s' % (data['path'], data['test_name'], '--num-iters=%d' % iters)], |
| 82 | + stdout=subprocess.PIPE, stderr=open('/dev/null', 'w')) |
| 83 | + results = [x for x in p.communicate()[0].split("\n") if len(x) > 0] |
| 84 | + return [x.split(',')[1] for x in results[results.index('DTRACE RESULTS')+1:]] |
| 85 | + iter_2_results = get_results_with_iters(2) |
| 86 | + iter_3_results = get_results_with_iters(3) |
| 87 | + |
| 88 | + results = [] |
| 89 | + for x in zip(iter_2_results, iter_3_results): |
| 90 | + results.append(x[1]) |
| 91 | + results.append(int(x[1]) - int(x[0])) |
| 92 | + |
| 93 | + return DTraceResult(test_name, 0, results, self.csv_output) |
| 94 | + |
| 95 | +SWIFT_BIN_DIR = os.path.dirname(os.path.abspath(__file__)) |
| 96 | + |
| 97 | +def parse_args(): |
| 98 | + parser = argparse.ArgumentParser() |
| 99 | + parser.add_argument('-filter', type=str, default=None, |
| 100 | + help='Filter out any test that does not match the given regex') |
| 101 | + parser.add_argument('-csv', default=False, action='store_true', |
| 102 | + help="Emit csv output", dest='csv_output') |
| 103 | + return parser.parse_args() |
| 104 | + |
| 105 | +if __name__ == "__main__": |
| 106 | + args = parse_args() |
| 107 | + g = DTracePerfTestDriver(SWIFT_BIN_DIR, XFAIL_LIST, args.csv_output) |
| 108 | + if g.run(args.filter): |
| 109 | + sys.exit(0) |
| 110 | + else: |
| 111 | + sys.exit(-1) |
0 commit comments