HoverRace  2.0
Profiler.h
Go to the documentation of this file.
1 
2 // Profiler.h
3 //
4 // Copyright (c) 2015 Michael Imamura.
5 //
6 // Licensed under GrokkSoft HoverRace SourceCode License v1.0(the "License");
7 // you may not use this file except in compliance with the License.
8 //
9 // A copy of the license should have been attached to the package from which
10 // you have taken this file. If you can not find the license you can not use
11 // this file.
12 //
13 //
14 // The author makes no representations about the suitability of
15 // this software for any purpose. It is provided "as is" "AS IS",
16 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
17 // implied.
18 //
19 // See the License for the specific language governing permissions
20 // and limitations under the License.
21 
22 #pragma once
23 
24 #include <chrono>
25 
26 #if defined(_WIN32) && defined(HR_ENGINE_SHARED)
27 # ifdef MR_ENGINE
28 # define MR_DllDeclare __declspec( dllexport )
29 # else
30 # define MR_DllDeclare __declspec( dllimport )
31 # endif
32 #else
33 # define MR_DllDeclare
34 #endif
35 
36 namespace HoverRace {
37 namespace Util {
38 
44 {
45 public:
46  Profiler() = delete;
47  Profiler(const std::string &name);
48 
49 public:
50  using clock_t = std::chrono::high_resolution_clock;
51  using dur_t = std::chrono::nanoseconds;
52 
53  class Sampler
54  {
55  public:
56  Sampler() = delete;
57 
58  Sampler(Profiler &profiler) :
59  profiler(profiler)
60  {
61  if ((++(profiler.sampling)) == 1) {
62  profiler.sampleStart = clock_t::now();
63  }
64  }
65 
67  {
68  if ((--(profiler.sampling)) == 0) {
69  profiler.dur += (clock_t::now() - profiler.sampleStart);
70  }
71  }
72 
73  Sampler(const Sampler&) = delete;
74  Sampler(Sampler&&) = default;
75 
76  Sampler &operator=(const Sampler&) = delete;
77  Sampler &operator=(Sampler&&) = default;
78 
79  private:
81  };
82 
83  struct LapTime
84  {
85  LapTime() :
86  time(dur_t::zero()),
87  pctParent(std::numeric_limits<double>::quiet_NaN()) { }
88 
90  double pctParent;
91  };
92 
93 public:
94  const std::string &GetName() const { return name; }
95  dur_t GetDuration() const { return dur; }
96  const LapTime &GetLastLap() const { return lap; }
97 
102  const LapTime &GetOtherTime() const { return otherTime; }
103 
104  void Reset();
105 
106  const LapTime &Lap(const Profiler *parent = nullptr);
107 
108 public:
109  std::shared_ptr<Profiler> AddSub(const std::string &name);
110 
111 private:
113  std::string name;
114  std::vector<std::shared_ptr<Profiler>> subs;
115  int sampling;
116  clock_t::time_point sampleStart;
119 };
120 
121 MR_DllDeclare inline std::ostream &operator<<(std::ostream &os,
122  const Profiler &p)
123 {
124  os << p.GetName() << ' ' << p.GetDuration().count();
125  return os;
126 }
127 
128 MR_DllDeclare inline std::ostream &operator<<(std::ostream &os,
129  const Profiler::LapTime &lp)
130 {
131  static boost::format fmt("%0.2f%% (%d)");
132  os << fmt % lp.pctParent % lp.time.count();
133  return os;
134 }
135 
136 } // namespace Util
137 } // namespace HoverRace
138 
139 #undef MR_DllDeclare
LapTime lap
Definition: Profiler.h:117
double pctParent
Definition: Profiler.h:90
dur_t dur
Definition: Profiler.h:112
const LapTime & GetOtherTime() const
Retrieve the unaccounted-for time from the last call to Lap().
Definition: Profiler.h:102
STL namespace.
std::chrono::high_resolution_clock clock_t
Definition: Profiler.h:50
Profiler & profiler
Definition: Profiler.h:80
clock_t::time_point sampleStart
Definition: Profiler.h:116
std::string name
Definition: Profiler.h:113
Definition: Profiler.h:53
~Sampler()
Definition: Profiler.h:66
const std::string & GetName() const
Definition: Profiler.h:94
dur_t time
Definition: Profiler.h:89
LapTime otherTime
Definition: Profiler.h:118
std::ostream & operator<<(std::ostream &os, const Clock &clock)
Definition: Clock.h:115
int sampling
Definition: Profiler.h:115
Simple profiler.
Definition: Profiler.h:43
std::chrono::nanoseconds dur_t
Definition: Profiler.h:51
std::vector< std::shared_ptr< Profiler > > subs
Definition: Profiler.h:114
dur_t GetDuration() const
Definition: Profiler.h:95
const LapTime & GetLastLap() const
Definition: Profiler.h:96
Definition: Announcement.h:24
Sampler(Profiler &profiler)
Definition: Profiler.h:58
#define MR_DllDeclare
Definition: Profiler.h:33
Definition: Profiler.h:83
LapTime()
Definition: Profiler.h:85