From f992ac7da70459655febdfd8d34cb7ccdcc23915 Mon Sep 17 00:00:00 2001 From: Oliver Stoeneberg Date: Mon, 12 Apr 2010 22:13:42 +0200 Subject: [PATCH] showtime: refactoring and improving. Added summary/file/top5/average options. --- lib/cppcheck.cpp | 133 +++++++++++++++++++++++++++++++++++++++-------- lib/settings.cpp | 2 +- lib/settings.h | 4 +- 3 files changed, 114 insertions(+), 25 deletions(-) diff --git a/lib/cppcheck.cpp b/lib/cppcheck.cpp index 8cd674777..e2fe8ec3c 100644 --- a/lib/cppcheck.cpp +++ b/lib/cppcheck.cpp @@ -32,16 +32,82 @@ #include #include +/* + TODO: + - handle SHOWTIME_TOP5 and SHOWTIME_AVERAGE (show time per result average) in TimerResults + - sort list by time + - list number of results? + - find way to store number of results per entry + - do not sort the results alphabetically +*/ +enum +{ + SHOWTIME_NONE = 0, + SHOWTIME_FILE, + SHOWTIME_SUMMARY, + SHOWTIME_TOP5, + SHOWTIME_AVERAGE +}; + +class TimerResultsIntf +{ +public: + virtual ~TimerResultsIntf() { } + + virtual void AddResults(const std::string& str, clock_t clocks) = 0; +}; + +class TimerResults : public TimerResultsIntf +{ +public: + TimerResults() + // : _numerOfResults(0) + { + } + + void ShowResults() + { + std::map::const_iterator I = _results.begin(); + const std::map::const_iterator E = _results.end(); + + while (I != E) + { + double sec = (double)I->second / CLOCKS_PER_SEC; + std::cout << I->first << ": " << sec << "s" << std::endl; + + ++I; + } + } + + virtual void AddResults(const std::string& str, clock_t clocks) + { + if (_results.find(str) != _results.end()) + _results[str] += clocks; + else + _results[str] = clocks; + + //_numerOfResults++; + } + +private: + std::map _results; + //unsigned int _numerOfResults; +}; + +static TimerResults S_timerResults; + class Timer { public: - Timer(const std::string& str, const Settings& settings) + Timer(const std::string& str, unsigned int showtimeMode, TimerResultsIntf* timerResults = NULL) : _str(str) - , _settings(settings) + , _showtimeMode(showtimeMode) , _stopped(false) + , _start(0) + , _timerResults(timerResults) { - if (_settings._showtime) - start = clock(); + if (showtimeMode != SHOWTIME_NONE) + _start = clock(); } ~Timer() @@ -51,24 +117,35 @@ public: void Stop() { - if (_settings._showtime && !_stopped) + if ((_showtimeMode != SHOWTIME_NONE) && !_stopped) { clock_t end = clock(); - clock_t diff = end - start; - double sec = (double)diff / CLOCKS_PER_SEC; - std::cout << _str << ": " << sec << "s" << std::endl; + clock_t diff = end - _start; + + if ((_showtimeMode == SHOWTIME_SUMMARY) || (_showtimeMode == SHOWTIME_TOP5)) + { + if (_timerResults) + _timerResults->AddResults(_str, diff); + } + + if (_showtimeMode == SHOWTIME_FILE) + { + double sec = (double)diff / CLOCKS_PER_SEC; + std::cout << _str << ": " << sec << "s" << std::endl; + } } _stopped = true; } private: - Timer& operator=(const Timer&); + Timer& operator=(const Timer&); // disallow assignments const std::string _str; - const Settings& _settings; - clock_t start; + unsigned int _showtimeMode; + clock_t _start; bool _stopped; + TimerResultsIntf* _timerResults; }; //--------------------------------------------------------------------------- @@ -81,7 +158,7 @@ CppCheck::CppCheck(ErrorLogger &errorLogger) CppCheck::~CppCheck() { - + S_timerResults.ShowResults(); } void CppCheck::settings(const Settings ¤tSettings) @@ -231,8 +308,20 @@ bool CppCheck::parseFromArgs(int argc, const char* const argv[]) _settings.append(9 + argv[i]); // show timing information.. - else if (strcmp(argv[i], "--showtime") == 0) - _settings._showtime = true; + else if (strncmp(argv[i], "--showtime=", 11) == 0) + { + const std::string showtimeMode = argv[i] + 11; + if (showtimeMode == "file") + _settings._showtime = SHOWTIME_FILE; + else if (showtimeMode == "summary") + _settings._showtime = SHOWTIME_SUMMARY; + else if (showtimeMode == "top5") + _settings._showtime = SHOWTIME_TOP5; + else if (showtimeMode == "average") + _settings._showtime = SHOWTIME_AVERAGE; + else + _settings._showtime = SHOWTIME_NONE; + } // Print help else if (strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "--help") == 0) @@ -539,7 +628,7 @@ unsigned int CppCheck::check() { // Only file name was given, read the content from file std::ifstream fin(fname.c_str()); - Timer t("Preprocessor::preprocess", _settings); + Timer t("Preprocessor::preprocess", _settings._showtime, &S_timerResults); preprocessor.preprocess(fin, filedata, configurations, fname, _settings._includePaths); } @@ -557,7 +646,7 @@ unsigned int CppCheck::check() } cfg = *it; - Timer t("Preprocessor::getcode", _settings); + Timer t("Preprocessor::getcode", _settings._showtime, &S_timerResults); const std::string codeWithoutCfg = Preprocessor::getcode(filedata, *it, fname, &_errorLogger); t.Stop(); @@ -609,7 +698,7 @@ void CppCheck::checkFile(const std::string &code, const char FileName[]) // Tokenize the file std::istringstream istr(code); - Timer timer("Tokenizer::tokenize", _settings); + Timer timer("Tokenizer::tokenize", _settings._showtime, &S_timerResults); result = _tokenizer.tokenize(istr, FileName, cfg); timer.Stop(); if (!result) @@ -618,7 +707,7 @@ void CppCheck::checkFile(const std::string &code, const char FileName[]) return; } - Timer timer2("Tokenizer::fillFunctionList", _settings); + Timer timer2("Tokenizer::fillFunctionList", _settings._showtime, &S_timerResults); _tokenizer.fillFunctionList(); timer2.Stop(); @@ -628,17 +717,17 @@ void CppCheck::checkFile(const std::string &code, const char FileName[]) if (_settings.terminated()) return; - Timer timerRunChecks((*it)->name() + "::runChecks", _settings); + Timer timerRunChecks((*it)->name() + "::runChecks", _settings._showtime, &S_timerResults); (*it)->runChecks(&_tokenizer, &_settings, this); } - Timer timer3("Tokenizer::simplifyTokenList", _settings); + Timer timer3("Tokenizer::simplifyTokenList", _settings._showtime, &S_timerResults); result = _tokenizer.simplifyTokenList(); timer3.Stop(); if (!result) return; - Timer timer4("Tokenizer::fillFunctionList", _settings); + Timer timer4("Tokenizer::fillFunctionList", _settings._showtime, &S_timerResults); _tokenizer.fillFunctionList(); timer4.Stop(); @@ -651,7 +740,7 @@ void CppCheck::checkFile(const std::string &code, const char FileName[]) if (_settings.terminated()) return; - Timer timerSimpleChecks((*it)->name() + "::runSimplifiedChecks", _settings); + Timer timerSimpleChecks((*it)->name() + "::runSimplifiedChecks", _settings._showtime, &S_timerResults); (*it)->runSimplifiedChecks(&_tokenizer, &_settings, this); } } diff --git a/lib/settings.cpp b/lib/settings.cpp index 2d96d6d5e..b2ced23c1 100644 --- a/lib/settings.cpp +++ b/lib/settings.cpp @@ -35,7 +35,7 @@ Settings::Settings() _xml = false; _jobs = 1; _exitCode = 0; - _showtime = false; + _showtime = 0; // TODO: use enum _append = ""; _terminate = false; } diff --git a/lib/settings.h b/lib/settings.h index 080958c74..d30b1bb7f 100644 --- a/lib/settings.h +++ b/lib/settings.h @@ -111,8 +111,8 @@ public: e.g. "{severity} {file}:{line} {message} {id}" */ std::string _outputFormat; - /** @brief show timing information (--showtime) */ - bool _showtime; + /** @brief show timing information (--showtime=file|summary|top5|average) */ + unsigned int _showtime; /** @brief List of include paths, e.g. "my/includes/" which should be used for finding include files inside source files. (-I) */