blob: e77503a5e308705e658e66fd11ada71ab08d70c3 [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
Shinichiro Hamaji8380fb82016-02-26 16:51:50 +090018#include <mutex>
Shinichiro Hamaji0d8e79b2015-06-30 03:29:35 +090019#include <string>
20
21using namespace std;
22
23class Stats {
24 public:
25 explicit Stats(const char* name);
26
27 string String() const;
28
29 private:
30 void Start();
Shinichiro Hamajib123fe52015-06-30 16:32:56 +090031 double End();
Shinichiro Hamaji0d8e79b2015-06-30 03:29:35 +090032
33 friend class ScopedStatsRecorder;
34
35 const char* name_;
Shinichiro Hamaji0d8e79b2015-06-30 03:29:35 +090036 double elapsed_;
Shinichiro Hamaji8c8358d2015-06-30 17:08:00 +090037 int cnt_;
Shinichiro Hamaji8380fb82016-02-26 16:51:50 +090038 mutable mutex mu_;
Shinichiro Hamaji0d8e79b2015-06-30 03:29:35 +090039};
40
41class ScopedStatsRecorder {
42 public:
Shinichiro Hamajib123fe52015-06-30 16:32:56 +090043 explicit ScopedStatsRecorder(Stats* st, const char* msg = 0);
Shinichiro Hamaji0d8e79b2015-06-30 03:29:35 +090044 ~ScopedStatsRecorder();
45
46 private:
47 Stats* st_;
Shinichiro Hamajib123fe52015-06-30 16:32:56 +090048 const char* msg_;
Shinichiro Hamaji0d8e79b2015-06-30 03:29:35 +090049};
50
51void ReportAllStats();
52
Dan Willemsen3ce083f2017-10-11 22:17:48 -070053#define COLLECT_STATS(name) \
54 static Stats stats(name); \
Shinichiro Hamaji0d8e79b2015-06-30 03:29:35 +090055 ScopedStatsRecorder ssr(&stats)
56
Dan Willemsen3ce083f2017-10-11 22:17:48 -070057#define COLLECT_STATS_WITH_SLOW_REPORT(name, msg) \
58 static Stats stats(name); \
Shinichiro Hamajib123fe52015-06-30 16:32:56 +090059 ScopedStatsRecorder ssr(&stats, msg)
60
Shinichiro Hamaji0d8e79b2015-06-30 03:29:35 +090061#endif // STATS_H_