benchmark.py 2.19 KB
Newer Older
dbarbera's avatar
dbarbera committed
1
2
3
4
5
6
7
8
9
10
11
12
import os
import subprocess
import sys
import time as t


def main():
    start = t.time()
    results = []
    errors = 0
    for testfolder in sys.argv[1:]:
        result = benchmark(testfolder)
dbarbera's avatar
dbarbera committed
13
        make(testfolder, 'clean')
dbarbera's avatar
dbarbera committed
14
15
16
17
18
19
20
21
22
23
        if result:
            results.append(result)
        else:
            errors += 1
        sys.stdout.write('.')
        sys.stdout.flush()

    sys.stdout.write('\n')

    elapsed = t.time() - start
dbarbera's avatar
dbarbera committed
24
    sys.exit(summarize(results, errors, elapsed))
dbarbera's avatar
dbarbera committed
25
26
27
28
29
30
31
32
33
34
35
36
37
38


def benchmark(testfolder):
    for rule in ("test-llvm", "test-ada"):
        if make(testfolder, rule) != 0:
            return

    llvm_bin = os.path.join(testfolder, "test_ada")
    ada_bin = os.path.join(testfolder, "test_llvm")

    for bin_name in (llvm_bin, ada_bin):
        if not os.path.isfile(bin_name):
            return

dbarbera's avatar
dbarbera committed
39
    result = {
dbarbera's avatar
dbarbera committed
40
41
42
43
44
45
46
47
48
49
        "size": {
            "ada": os.path.getsize(llvm_bin),
            "llvm": os.path.getsize(ada_bin),
        },
        "time": {
            "ada": time(ada_bin),
            "llvm": time(llvm_bin),
        }
    }

dbarbera's avatar
dbarbera committed
50
51
    return result

dbarbera's avatar
dbarbera committed
52
53
54
55
56
57
58
59
60

def time(file, iters=1000):
    start = t.time()
    call(["/bin/bash", "-c", "for i in {1..%s} ; do %s ; done" % (iters, file)])
    return (t.time() - start) / iters


def summarize(results, errors, elapsed):
    print "Finished in %.3fs" % elapsed
dbarbera's avatar
dbarbera committed
61
    print "%s benchmarks, %s errors" % (len(results) + errors, errors)
dbarbera's avatar
dbarbera committed
62
63
64

    if not results:
        print "No results"
dbarbera's avatar
dbarbera committed
65
        return 1
dbarbera's avatar
dbarbera committed
66
67
68
69
70

    print "Summary:"
    print "  Size: Ada %.2f%% LLVM %.2f%%" % diff(results, "size")
    print "  Time: Ada %.2f%% LLVM %.2f%%" % diff(results, "time")

dbarbera's avatar
dbarbera committed
71
72
    return 0 if results and not errors else 1

dbarbera's avatar
dbarbera committed
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

def diff(results, metric):
    metrics = [r[metric] for r in results]
    return 100, mean(([float(m["llvm"]) / float(m["ada"]) * 100 for m in metrics]))


def mean(values):
    return sum(values) / len(values)


def make(path, rule):
    return call(["make", "-C", path, rule])[0]


def call(args):
    proc = subprocess.Popen(
        args,
        stdout=subprocess.PIPE,
        stderr=subprocess.PIPE,
    )
    stdout, stderr = proc.communicate()
    errcode = proc.wait()
    return (errcode, stdout, stderr)


if __name__ == '__main__':
    main()