BALL  1.4.1
benchmark.h
Go to the documentation of this file.
00001 // -*- Mode: C++; tab-width: 2; -*-
00002 // vi: set ts=2:
00003 //
00004 
00005 #ifndef BALL_COMMON_H
00006 # include <BALL/common.h>
00007 #endif
00008 
00009 #ifndef BALL_SYSTEM_TIMER_H
00010 # include <BALL/SYSTEM/timer.h>
00011 #endif
00012 
00013 #include <string>
00014 
00019 #define START_SECTION(name, weight) \
00020   BENCHMARK::section_time = BENCHMARK::timer.getCPUTime();\
00021   BENCHMARK::section_name = #name;\
00022   BENCHMARK::section_weight = weight;
00023 
00024 
00028 #define END_SECTION \
00029   BENCHMARK::timer.stop();\
00030   BENCHMARK::section_time = BENCHMARK::timer.getCPUTime() - BENCHMARK::section_time;\
00031   if (BENCHMARK::verbose > 0)\
00032   {\
00033     std::cout << BENCHMARK::section_name << ": " \
00034       << BENCHMARK::section_time << " s"\
00035       << " (weight = " << BENCHMARK::section_weight << ")" << std::endl;\
00036   }\
00037   BENCHMARK::total_time += BENCHMARK::section_time * BENCHMARK::section_weight;\
00038 
00039 
00044 #define STATUS(a) \
00045   if (BENCHMARK::verbose > 0)\
00046   {\
00047     std::cout << "  status: " << a << std::endl;\
00048   }
00049 
00050 
00059 #define START_TIMER \
00060   BENCHMARK::timer.start();\
00061 
00062 
00071 #define STOP_TIMER \
00072   BENCHMARK::timer.stop();
00073 
00079 #define START_BENCHMARK(class_name, overall_weight, version)\
00080 /* define a special namespace for all internal variables */\
00081 /* to avoid potential collisions                         */\
00082 namespace BENCHMARK {\
00083   int           verbose = 0;\
00084   bool          all_tests = true;\
00085   int           exception = 0;\
00086   string        exception_name = "";\
00087   const char*   version_string = version;\
00088   string        section_name = "";\
00089   float         section_weight = 1.0;\
00090   float         weight = overall_weight;\
00091   float         total_time;\
00092   float         section_time;\
00093   BALL::Timer   timer;\
00094 }\
00095 \
00096 \
00097 int main(int argc, char **argv)\
00098 {\
00099 \
00100   if (argc == 2) {\
00101     if (!strcmp(argv[1], "-v"))\
00102       BENCHMARK::verbose = 1;\
00103   };\
00104 \
00105   if ((argc > 2) || ((argc == 2) && (BENCHMARK::verbose == 0))) {\
00106     std::cerr << "Execute a benchmark for the " #class_name " class." << std::endl;\
00107     std::cerr << "Overall weight of the test: " << BENCHMARK::weight << std::endl;\
00108 \
00109     std::cerr << "On successful operation, the total CPU time (in seconds)," << std::endl;\
00110     std::cerr << "is printed." << std::endl;\
00111     std::cerr << "If called with an argument of -v, " << argv[0] << " detailed" << std::endl;\
00112     std::cerr << "information about individual benchmarks is printed." << std::endl;\
00113     return 1;\
00114   }\
00115 \
00116   if (BENCHMARK::verbose > 0)\
00117     std::cout << "Version: " << BENCHMARK::version_string << std::endl;\
00118 \
00119   try {\
00120 
00121 
00124 #define END_BENCHMARK \
00125   /* global try block */\
00126   }\
00127   /* catch FileNotFound exceptions to print out the file name */\
00128   catch (BALL::Exception::FileNotFound& e)\
00129   {\
00130     BENCHMARK::all_tests = false;\
00131     if (BENCHMARK::verbose > 1)\
00132     {\
00133       if (BENCHMARK::exception == 1) /* dummy to avoid compiler warnings */\
00134         BENCHMARK::exception++;\
00135       std::cout << std::endl << "    (caught exception of type ";\
00136       std::cout << e.getName();\
00137       if ((e.getLine() > 0) && (!(e.getFile() == "")))\
00138         std::cout << " outside a benchmark block, which was thrown in line " << e.getLine() << " of file " << e.getFile();\
00139       std::cout << " while looking for file " << e.getFilename();\
00140       std::cout << " - unexpected!) " << std::endl;\
00141     }\
00142   }\
00143   /* catch BALL exceptions to retrieve additional information */\
00144   catch (BALL::Exception::GeneralException& e)\
00145   {\
00146     BENCHMARK::all_tests = false;\
00147     if (BENCHMARK::verbose > 1)\
00148     {\
00149       if (BENCHMARK::exception == 1) /* dummy to avoid compiler warnings */\
00150         BENCHMARK::exception++;\
00151       std::cout << std::endl << "    (caught exception of type ";\
00152       std::cout << e.getName();\
00153       if ((e.getLine() > 0) && (!(e.getFile() == "")))\
00154         std::cout << " outside a benchmark block, which was thrown in line " << e.getLine() << " of file " << e.getFile();\
00155       std::cout << " - unexpected!) " << std::endl;\
00156     }\
00157   }\
00158   /* catch all non-BALL exceptions */\
00159   catch (...)\
00160   {\
00161     BENCHMARK::all_tests = false;\
00162     if (BENCHMARK::verbose > 1)\
00163     {\
00164       std::cout << std::endl << "    (caught unidentified and unexpected exception outside a benchmark block!) " << std::endl;\
00165     }\
00166   }\
00167 \
00168   /* check for exit code */\
00169   if (!BENCHMARK::all_tests)\
00170   {\
00171     std::cout << "(" << BENCHMARK::weight * BENCHMARK::total_time << ")" << std::endl;\
00172     return 1;\
00173   } else {\
00174     std::cout << BENCHMARK::weight * BENCHMARK::total_time << std::endl;\
00175     return 0;\
00176   }\
00177 }\
00178 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines