BALL 1.5.0
benchmark.h
Go to the documentation of this file.
1// -*- Mode: C++; tab-width: 2; -*-
2// vi: set ts=2:
3//
4
5#ifndef BALL_COMMON_H
6# include <BALL/common.h>
7#endif
8
9#ifndef BALL_SYSTEM_TIMER_H
10# include <BALL/SYSTEM/timer.h>
11#endif
12
13#include <string>
14
19#define START_SECTION(name, weight) \
20 BENCHMARK::section_time = BENCHMARK::timer.getCPUTime();\
21 BENCHMARK::section_name = #name;\
22 BENCHMARK::section_weight = weight;
23
24
28#define END_SECTION \
29 BENCHMARK::timer.stop();\
30 BENCHMARK::section_time = BENCHMARK::timer.getCPUTime() - BENCHMARK::section_time;\
31 if (BENCHMARK::verbose > 0)\
32 {\
33 std::cout << BENCHMARK::section_name << ": " \
34 << BENCHMARK::section_time << " s"\
35 << " (weight = " << BENCHMARK::section_weight << ")" << std::endl;\
36 }\
37 BENCHMARK::total_time += BENCHMARK::section_time * BENCHMARK::section_weight;\
38
39
44#define STATUS(a) \
45 if (BENCHMARK::verbose > 0)\
46 {\
47 std::cout << " status: " << a << std::endl;\
48 }
49
50
59#define START_TIMER \
60 BENCHMARK::timer.start();\
61
62
71#define STOP_TIMER \
72 BENCHMARK::timer.stop();
73
79#define START_BENCHMARK(class_name, overall_weight, version)\
80/* define a special namespace for all internal variables */\
81/* to avoid potential collisions */\
82namespace BENCHMARK {\
83 int verbose = 0;\
84 bool all_tests = true;\
85 int exception = 0;\
86 string exception_name = "";\
87 const char* version_string = version;\
88 string section_name = "";\
89 float section_weight = 1.0;\
90 float weight = overall_weight;\
91 float total_time;\
92 float section_time;\
93 BALL::Timer timer;\
94}\
95\
96\
97int main(int argc, char **argv)\
98{\
99\
100 if (argc == 2) {\
101 if (!strcmp(argv[1], "-v"))\
102 BENCHMARK::verbose = 1;\
103 };\
104\
105 if ((argc > 2) || ((argc == 2) && (BENCHMARK::verbose == 0))) {\
106 std::cerr << "Execute a benchmark for the " #class_name " class." << std::endl;\
107 std::cerr << "Overall weight of the test: " << BENCHMARK::weight << std::endl;\
108\
109 std::cerr << "On successful operation, the total CPU time (in seconds)," << std::endl;\
110 std::cerr << "is printed." << std::endl;\
111 std::cerr << "If called with an argument of -v, " << argv[0] << " detailed" << std::endl;\
112 std::cerr << "information about individual benchmarks is printed." << std::endl;\
113 return 1;\
114 }\
115\
116 if (BENCHMARK::verbose > 0)\
117 std::cout << "Version: " << BENCHMARK::version_string << std::endl;\
118\
119 try {\
120
124#define END_BENCHMARK \
125 /* global try block */\
126 }\
127 /* catch FileNotFound exceptions to print out the file name */\
128 catch (BALL::Exception::FileNotFound& e)\
129 {\
130 BENCHMARK::all_tests = false;\
131 if (BENCHMARK::verbose > 1)\
132 {\
133 if (BENCHMARK::exception == 1) /* dummy to avoid compiler warnings */\
134 BENCHMARK::exception++;\
135 std::cout << std::endl << " (caught exception of type ";\
136 std::cout << e.getName();\
137 if ((e.getLine() > 0) && (!(e.getFile() == "")))\
138 std::cout << " outside a benchmark block, which was thrown in line " << e.getLine() << " of file " << e.getFile();\
139 std::cout << " while looking for file " << e.getFilename();\
140 std::cout << " - unexpected!) " << std::endl;\
141 }\
142 }\
143 /* catch BALL exceptions to retrieve additional information */\
144 catch (BALL::Exception::GeneralException& e)\
145 {\
146 BENCHMARK::all_tests = false;\
147 if (BENCHMARK::verbose > 1)\
148 {\
149 if (BENCHMARK::exception == 1) /* dummy to avoid compiler warnings */\
150 BENCHMARK::exception++;\
151 std::cout << std::endl << " (caught exception of type ";\
152 std::cout << e.getName();\
153 if ((e.getLine() > 0) && (!(e.getFile() == "")))\
154 std::cout << " outside a benchmark block, which was thrown in line " << e.getLine() << " of file " << e.getFile();\
155 std::cout << " - unexpected!) " << std::endl;\
156 }\
157 }\
158 /* catch all non-BALL exceptions */\
159 catch (...)\
160 {\
161 BENCHMARK::all_tests = false;\
162 if (BENCHMARK::verbose > 1)\
163 {\
164 std::cout << std::endl << " (caught unidentified and unexpected exception outside a benchmark block!) " << std::endl;\
165 }\
166 }\
167\
168 /* check for exit code */\
169 if (!BENCHMARK::all_tests)\
170 {\
171 std::cout << "(" << BENCHMARK::weight * BENCHMARK::total_time << ")" << std::endl;\
172 return 1;\
173 } else {\
174 std::cout << BENCHMARK::weight * BENCHMARK::total_time << std::endl;\
175 return 0;\
176 }\
177}\
178