blob: 057dd7675d5e1c76a8b3a07d81789f565a624232 [file] [log] [blame]
Shinichiro Hamaji0d8e79b2015-06-30 03:29:35 +09001// 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
25namespace {
26
27vector<Stats*>* g_stats;
28
29} // namespace
30
31Stats::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
38string Stats::String() const {
39 return StringPrintf("%s: %f", name_, elapsed_);
40}
41
42void Stats::Start() {
43 start_time_ = GetTime();
44}
45
Shinichiro Hamajib123fe52015-06-30 16:32:56 +090046double Stats::End() {
47 double e = GetTime() - start_time_;
48 elapsed_ += e;
49 return e;
Shinichiro Hamaji0d8e79b2015-06-30 03:29:35 +090050}
51
Shinichiro Hamajib123fe52015-06-30 16:32:56 +090052ScopedStatsRecorder::ScopedStatsRecorder(Stats* st, const char* msg)
53 : st_(st), msg_(msg) {
Shinichiro Hamaji0d8e79b2015-06-30 03:29:35 +090054 st_->Start();
55}
56
57ScopedStatsRecorder::~ScopedStatsRecorder() {
Shinichiro Hamajib123fe52015-06-30 16:32:56 +090058 double e = st_->End();
59 if (msg_ && e > 3.0) {
60 LOG_STAT("slow %s (%f): %s", st_->name_, e, msg_);
61 }
Shinichiro Hamaji0d8e79b2015-06-30 03:29:35 +090062}
63
64void 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}