blob: 2d747a4ea1a1d880dd890cd4e0351fe8ebb72656 [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
46void Stats::End() {
47 elapsed_ += GetTime() - start_time_;
48}
49
50ScopedStatsRecorder::ScopedStatsRecorder(Stats* st)
51 : st_(st) {
52 st_->Start();
53}
54
55ScopedStatsRecorder::~ScopedStatsRecorder() {
56 st_->End();
57}
58
59void ReportAllStats() {
60 if (!g_stats)
61 return;
62 for (Stats* st : *g_stats) {
63 LOG_STAT("%s", st->String().c_str());
64 }
65 delete g_stats;
66}