00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 #ifndef OPENMS_CONCEPT_BENCHMARK_H
00028 #define OPENMS_CONCEPT_BENCHMARK_H
00029
00030 #include <OpenMS/config.h>
00031 #include <OpenMS/SYSTEM/StopWatch.h>
00032 #include <OpenMS/CONCEPT/Exception.h>
00033
00034 #include <string>
00035 #include <iostream>
00036
00053 #define START_SECTION(name, weight) \
00054 BENCHMARK::section_time = BENCHMARK::timer.getCPUTime();\
00055 BENCHMARK::section_name = #name;\
00056 BENCHMARK::section_weight = weight;
00057
00058
00064 #define END_SECTION \
00065 BENCHMARK::timer.stop();\
00066 BENCHMARK::section_time = BENCHMARK::timer.getCPUTime() - BENCHMARK::section_time;\
00067 if (BENCHMARK::verbose > 0)\
00068 {\
00069 std::cout << BENCHMARK::section_name << ": " \
00070 << BENCHMARK::section_time << " s"\
00071 << " (weight = " << BENCHMARK::section_weight << ")" << std::endl;\
00072 }\
00073 BENCHMARK::total_time += BENCHMARK::section_time * BENCHMARK::section_weight;\
00074
00075
00083 #define STATUS(a) \
00084 if (BENCHMARK::verbose > 0)\
00085 {\
00086 std::cout << " status: " << a << std::endl;\
00087 }
00088
00089
00101 #define START_TIMER \
00102 BENCHMARK::timer.start();\
00103
00104
00116 #define STOP_TIMER \
00117 BENCHMARK::timer.stop();
00118
00127 #define START_BENCHMARK(class_name, overall_weight, version)\
00128 \
00129 \
00130 namespace BENCHMARK {\
00131 int verbose = 0;\
00132 bool all_tests = true;\
00133 int exception = 0;\
00134 std::string exception_name = "";\
00135 const char* version_string = version;\
00136 std::string section_name = "";\
00137 float section_weight = 1.0;\
00138 float weight = overall_weight;\
00139 float total_time;\
00140 float section_time;\
00141 OpenMS::StopWatch timer;\
00142 }\
00143 \
00144 \
00145 int main(int argc, char **argv)\
00146 {\
00147 \
00148 if (argc == 2) {\
00149 if (!strcmp(argv[1], "-v"))\
00150 BENCHMARK::verbose = 1;\
00151 };\
00152 \
00153 if ((argc > 2) || ((argc == 2) && (BENCHMARK::verbose == 0))) {\
00154 std::cerr << "Execute a benchmark for the " #class_name " class." << std::endl;\
00155 std::cerr << "Overall weight of the test: " << BENCHMARK::weight << std::endl;\
00156 \
00157 std::cerr << "On successful operation, the total CPU time (in seconds)," << std::endl;\
00158 std::cerr << "is printed." << std::endl;\
00159 std::cerr << "If called with an argument of -v, " << argv[0] << " detailed" << std::endl;\
00160 std::cerr << "information about individual benchmarks is printed." << std::endl;\
00161 return 1;\
00162 }\
00163 \
00164 if (BENCHMARK::verbose > 0)\
00165 std::cout << "Version: " << BENCHMARK::version_string << std::endl;\
00166 \
00167 try {\
00168
00169
00174 #define END_BENCHMARK \
00175 \
00176 }\
00177 \
00178 catch (OpenMS::Exception::FileNotFound& e)\
00179 {\
00180 BENCHMARK::all_tests = false;\
00181 if (BENCHMARK::verbose > 1)\
00182 {\
00183 if (BENCHMARK::exception == 1) \
00184 BENCHMARK::exception++;\
00185 std::cout << std::endl << " (caught exception of type ";\
00186 std::cout << e.getName();\
00187 if ((e.getLine() > 0) && (!(e.getFile() == "")))\
00188 std::cout << " outside a benchmark block, which was thrown in line " << e.getLine() << " of file " << e.getFile();\
00189 std::cout << " while looking for file " << e.getFilename();\
00190 std::cout << " - unexpected!) " << std::endl;\
00191 }\
00192 }\
00193 \
00194 catch (OpenMS::Exception::Base& e)\
00195 {\
00196 BENCHMARK::all_tests = false;\
00197 if (BENCHMARK::verbose > 1)\
00198 {\
00199 if (BENCHMARK::exception == 1) \
00200 BENCHMARK::exception++;\
00201 std::cout << std::endl << " (caught exception of type ";\
00202 std::cout << e.getName();\
00203 if ((e.getLine() > 0) && (!(e.getFile() == "")))\
00204 std::cout << " outside a benchmark block, which was thrown in line " << e.getLine() << " of file " << e.getFile();\
00205 std::cout << " - unexpected!) " << std::endl;\
00206 }\
00207 }\
00208 \
00209 catch (...)\
00210 {\
00211 BENCHMARK::all_tests = false;\
00212 if (BENCHMARK::verbose > 1)\
00213 {\
00214 std::cout << std::endl << " (caught unidentified and unexpected exception outside a benchmark block!) " << std::endl;\
00215 }\
00216 }\
00217 \
00218 \
00219 if (!BENCHMARK::all_tests)\
00220 {\
00221 std::cout << "(" << BENCHMARK::weight * BENCHMARK::total_time << ")" << std::endl;\
00222 return 1;\
00223 } else {\
00224 std::cout << BENCHMARK::weight * BENCHMARK::total_time << std::endl;\
00225 return 0;\
00226 }\
00227 }\
00228
00229
00230
00231 #endif // OPENMS_CONCEPT_BENCHMARK_H