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 | // +build ignore |
| 16 | |
| 17 | #include "stats.h" |
| 18 | |
| 19 | #include <vector> |
| 20 | |
| 21 | #include "log.h" |
| 22 | #include "stringprintf.h" |
| 23 | #include "time.h" |
| 24 | |
| 25 | namespace { |
| 26 | |
| 27 | vector<Stats*>* g_stats; |
| 28 | |
| 29 | } // namespace |
| 30 | |
| 31 | Stats::Stats(const char* name) |
| 32 | : name_(name), start_time_(0), elapsed_(0) { |
| 33 | if (g_stats == NULL) |
| 34 | g_stats = new vector<Stats*>; |
| 35 | g_stats->push_back(this); |
| 36 | } |
| 37 | |
| 38 | string Stats::String() const { |
| 39 | return StringPrintf("%s: %f", name_, elapsed_); |
| 40 | } |
| 41 | |
| 42 | void Stats::Start() { |
| 43 | start_time_ = GetTime(); |
| 44 | } |
| 45 | |
Shinichiro Hamaji | b123fe5 | 2015-06-30 16:32:56 +0900 | [diff] [blame^] | 46 | double Stats::End() { |
| 47 | double e = GetTime() - start_time_; |
| 48 | elapsed_ += e; |
| 49 | return e; |
Shinichiro Hamaji | 0d8e79b | 2015-06-30 03:29:35 +0900 | [diff] [blame] | 50 | } |
| 51 | |
Shinichiro Hamaji | b123fe5 | 2015-06-30 16:32:56 +0900 | [diff] [blame^] | 52 | ScopedStatsRecorder::ScopedStatsRecorder(Stats* st, const char* msg) |
| 53 | : st_(st), msg_(msg) { |
Shinichiro Hamaji | 0d8e79b | 2015-06-30 03:29:35 +0900 | [diff] [blame] | 54 | st_->Start(); |
| 55 | } |
| 56 | |
| 57 | ScopedStatsRecorder::~ScopedStatsRecorder() { |
Shinichiro Hamaji | b123fe5 | 2015-06-30 16:32:56 +0900 | [diff] [blame^] | 58 | double e = st_->End(); |
| 59 | if (msg_ && e > 3.0) { |
| 60 | LOG_STAT("slow %s (%f): %s", st_->name_, e, msg_); |
| 61 | } |
Shinichiro Hamaji | 0d8e79b | 2015-06-30 03:29:35 +0900 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | void ReportAllStats() { |
| 65 | if (!g_stats) |
| 66 | return; |
| 67 | for (Stats* st : *g_stats) { |
| 68 | LOG_STAT("%s", st->String().c_str()); |
| 69 | } |
| 70 | delete g_stats; |
| 71 | } |