Home  · Classes  · Annotated Classes  · Modules  · Members  · Namespaces  · Related Pages

Benchmark.h (Maintainer: Marc Sturm)

Go to the documentation of this file.
00001 // -*- Mode: C++; tab-width: 2; -*-
00002 // vi: set ts=2:
00003 //
00004 // --------------------------------------------------------------------------
00005 //                   OpenMS Mass Spectrometry Framework 
00006 // --------------------------------------------------------------------------
00007 //  Copyright (C) 2003-2008 -- Oliver Kohlbacher, Knut Reinert
00008 //
00009 //  This library is free software; you can redistribute it and/or
00010 //  modify it under the terms of the GNU Lesser General Public
00011 //  License as published by the Free Software Foundation; either
00012 //  version 2.1 of the License, or (at your option) any later version.
00013 //
00014 //  This library is distributed in the hope that it will be useful,
00015 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
00016 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00017 //  Lesser General Public License for more details.
00018 //
00019 //  You should have received a copy of the GNU Lesser General Public
00020 //  License along with this library; if not, write to the Free Software
00021 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00022 //
00023 // --------------------------------------------------------------------------
00024 // $Maintainer: Marc Sturm $
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 /* define a special namespace for all internal variables */\
00129 /* to avoid potential collisions                         */\
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   /* global try block */\
00176   }\
00177   /* catch FileNotFound exceptions to print out the file name */\
00178   catch (OpenMS::Exception::FileNotFound& e)\
00179   {\
00180     BENCHMARK::all_tests = false;\
00181     if (BENCHMARK::verbose > 1)\
00182     {\
00183       if (BENCHMARK::exception == 1) /* dummy to avoid compiler warnings */\
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   /* catch OpenMS exceptions to retrieve additional information */\
00194   catch (OpenMS::Exception::Base& e)\
00195   {\
00196     BENCHMARK::all_tests = false;\
00197     if (BENCHMARK::verbose > 1)\
00198     {\
00199       if (BENCHMARK::exception == 1) /* dummy to avoid compiler warnings */\
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   /* catch all non-OpenMS exceptions */\
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   /* check for exit code */\
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  // end of Benchmark
00230 
00231 #endif // OPENMS_CONCEPT_BENCHMARK_H

Generated Tue Apr 1 15:36:33 2008 -- using doxygen 1.5.4 OpenMS / TOPP 1.1