blob: 1ccd044718e055983f9f666125f82f37047323c8 [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#ifndef STATS_H_
16#define STATS_H_
17
18#include <string>
19
20using namespace std;
21
22class Stats {
23 public:
24 explicit Stats(const char* name);
25
26 string String() const;
27
28 private:
29 void Start();
Shinichiro Hamajib123fe52015-06-30 16:32:56 +090030 double End();
Shinichiro Hamaji0d8e79b2015-06-30 03:29:35 +090031
32 friend class ScopedStatsRecorder;
33
34 const char* name_;
35 double start_time_;
36 double elapsed_;
37};
38
39class ScopedStatsRecorder {
40 public:
Shinichiro Hamajib123fe52015-06-30 16:32:56 +090041 explicit ScopedStatsRecorder(Stats* st, const char* msg = 0);
Shinichiro Hamaji0d8e79b2015-06-30 03:29:35 +090042 ~ScopedStatsRecorder();
43
44 private:
45 Stats* st_;
Shinichiro Hamajib123fe52015-06-30 16:32:56 +090046 const char* msg_;
Shinichiro Hamaji0d8e79b2015-06-30 03:29:35 +090047};
48
49void ReportAllStats();
50
51#define COLLECT_STATS(name) \
52 static Stats stats(name); \
53 ScopedStatsRecorder ssr(&stats)
54
Shinichiro Hamajib123fe52015-06-30 16:32:56 +090055#define COLLECT_STATS_WITH_SLOW_REPORT(name, msg) \
56 static Stats stats(name); \
57 ScopedStatsRecorder ssr(&stats, msg)
58
Shinichiro Hamaji0d8e79b2015-06-30 03:29:35 +090059#endif // STATS_H_