benchmark.h

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