1 // Copyright 2015 Google Inc. All rights reserved 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 #ifndef STATS_H_ 16 #define STATS_H_ 17 18 #include <mutex> 19 #include <string> 20 21 using namespace std; 22 23 class Stats { 24 public: 25 explicit Stats(const char* name); 26 27 string String() const; 28 29 private: 30 void Start(); 31 double End(); 32 33 friend class ScopedStatsRecorder; 34 35 const char* name_; 36 double elapsed_; 37 int cnt_; 38 mutable mutex mu_; 39 }; 40 41 class ScopedStatsRecorder { 42 public: 43 explicit ScopedStatsRecorder(Stats* st, const char* msg = 0); 44 ~ScopedStatsRecorder(); 45 46 private: 47 Stats* st_; 48 const char* msg_; 49 }; 50 51 void ReportAllStats(); 52 53 #define COLLECT_STATS(name) \ 54 static Stats stats(name); \ 55 ScopedStatsRecorder ssr(&stats) 56 57 #define COLLECT_STATS_WITH_SLOW_REPORT(name, msg) \ 58 static Stats stats(name); \ 59 ScopedStatsRecorder ssr(&stats, msg) 60 61 #endif // STATS_H_ 62