Debian Codegraph @

bench/bench.py

raw
1 "Run all benchmarks"
2
3 # Copyright (C) 2010 Anders Logg
4 #
5 # This file is part of DOLFIN.
6 #
7 # DOLFIN is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU Lesser General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # DOLFIN is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU Lesser General Public License for more details.
16 #
17 # You should have received a copy of the GNU Lesser General Public License
18 # along with DOLFIN. If not, see <http://www.gnu.org/licenses/>.
19 #
20 # Modified by Johannes Ring, 2011, 2012
21
22 from __future__ import print_function
23 import os, sys, time
24
25 failed = []
26
27 def run_bench(arg, directory, files):
28
29 # Skip directories not containing a benchmark
30 bench_exec = "bench_" + "_".join(directory.split(os.path.sep)[1:])
31 if not bench_exec in files:
32 return
33
34 # Get name of benchmark
35 name = directory.replace("./", "").replace("/", "-")
36 print("Running benchmark %s..." % name)
37
38 # Remove old logfile
39 cwd = os.getcwd()
40 logfile = os.path.join(cwd, "logs", name + ".log")
41 try:
42 os.remove(logfile)
43 except:
44 pass
45
46 # Run benchmark
47 os.chdir(directory)
48 t0 = time.time()
49 status = os.system(os.path.join(os.curdir, bench_exec) + " > %s" % logfile)
50 elapsed_time = time.time() - t0
51
52 # Change to toplevel directory
53 os.chdir(cwd)
54
55 # Report timing
56 if status == 0:
57 print("Completed in %g seconds\n" % elapsed_time)
58 else:
59 global failed
60 failed.append(name)
61 print("*** Failed\n")
62 return
63
64 # Get description of benchmark
65 f = open(logfile)
66 description = f.read().split("\n")[0]
67 f.close()
68
69 # Get timings (if any)
70 f = open(logfile)
71 run_timings = [("", elapsed_time)]
72 for line in [line for line in f.read().split("\n") if "BENCH" in line]:
73 words = [word.strip() for word in line.split()]
74 # Override total time
75 if len(words) == 2:
76 run_timings[0] = ("", float(words[1]))
77 # Add sub timing
78 elif len(words) == 3:
79 run_timings.append((words[1].lower(), float(words[2])))
80 f.close()
81
82 # Append to log file
83 d = time.gmtime()
84 date = str((d.tm_year, d.tm_mon, d.tm_mday, d.tm_hour, d.tm_min, d.tm_sec))
85 f = open(os.path.join("logs", "bench.log"), "a")
86 for (postfix, timing) in run_timings:
87 if postfix == "":
88 n = name
89 d = description
90 else:
91 n = "%s-%s" % (name, postfix)
92 d = "%s ((%s))" % (description, postfix)
93 f.write('%s %s %g "%s"\n' % (date, n, timing, d))
94
95 return status == 0
96
97 # Iterate over benchmarks
98 os.path.walk(".", run_bench, None)
99
100 # Print summary
101 if len(failed) == 0:
102 print("All benchmarks OK")
103 else:
104 print("%d benchmark(s) failed:" % len(failed))
105 for name in failed:
106 print(" " + name)
107
108 sys.exit(len(failed))