Shinichiro Hamaji | 0d8e79b | 2015-06-30 03:29:35 +0900 | [diff] [blame] | 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 | |
Shinichiro Hamaji | 8380fb8 | 2016-02-26 16:51:50 +0900 | [diff] [blame] | 18 | #include <mutex> |
Shinichiro Hamaji | 0d8e79b | 2015-06-30 03:29:35 +0900 | [diff] [blame] | 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(); |
Shinichiro Hamaji | b123fe5 | 2015-06-30 16:32:56 +0900 | [diff] [blame] | 31 | double End(); |
Shinichiro Hamaji | 0d8e79b | 2015-06-30 03:29:35 +0900 | [diff] [blame] | 32 | |
| 33 | friend class ScopedStatsRecorder; |
| 34 | |
| 35 | const char* name_; |
Shinichiro Hamaji | 0d8e79b | 2015-06-30 03:29:35 +0900 | [diff] [blame] | 36 | double elapsed_; |
Shinichiro Hamaji | 8c8358d | 2015-06-30 17:08:00 +0900 | [diff] [blame] | 37 | int cnt_; |
Shinichiro Hamaji | 8380fb8 | 2016-02-26 16:51:50 +0900 | [diff] [blame] | 38 | mutable mutex mu_; |
Shinichiro Hamaji | 0d8e79b | 2015-06-30 03:29:35 +0900 | [diff] [blame] | 39 | }; |
| 40 | |
| 41 | class ScopedStatsRecorder { |
| 42 | public: |
Shinichiro Hamaji | b123fe5 | 2015-06-30 16:32:56 +0900 | [diff] [blame] | 43 | explicit ScopedStatsRecorder(Stats* st, const char* msg = 0); |
Shinichiro Hamaji | 0d8e79b | 2015-06-30 03:29:35 +0900 | [diff] [blame] | 44 | ~ScopedStatsRecorder(); |
| 45 | |
| 46 | private: |
| 47 | Stats* st_; |
Shinichiro Hamaji | b123fe5 | 2015-06-30 16:32:56 +0900 | [diff] [blame] | 48 | const char* msg_; |
Shinichiro Hamaji | 0d8e79b | 2015-06-30 03:29:35 +0900 | [diff] [blame] | 49 | }; |
| 50 | |
| 51 | void ReportAllStats(); |
| 52 | |
| 53 | #define COLLECT_STATS(name) \ |
| 54 | static Stats stats(name); \ |
| 55 | ScopedStatsRecorder ssr(&stats) |
| 56 | |
Shinichiro Hamaji | b123fe5 | 2015-06-30 16:32:56 +0900 | [diff] [blame] | 57 | #define COLLECT_STATS_WITH_SLOW_REPORT(name, msg) \ |
| 58 | static Stats stats(name); \ |
| 59 | ScopedStatsRecorder ssr(&stats, msg) |
| 60 | |
Shinichiro Hamaji | 0d8e79b | 2015-06-30 03:29:35 +0900 | [diff] [blame] | 61 | #endif // STATS_H_ |