TimingProfile.h
1 /**************************************************************************************
2 Copyright 2015 Applied Research Associates, Inc.
3 Licensed under the Apache License, Version 2.0 (the "License"); you may not use
4 this file except in compliance with the License. You may obtain a copy of the License
5 at:
6 http://www.apache.org/licenses/LICENSE-2.0
7 Unless required by applicable law or agreed to in writing, software distributed under
8 the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
9 CONDITIONS OF ANY KIND, either express or implied. See the License for the
10 specific language governing permissions and limitations under the License.
11 **************************************************************************************/
12 
13 #pragma once
14 
15 #include <biogears/cdm/CommonDataModel.h>
16 #include <biogears/cdm/utils/Logger.h>
17 
18 #include <chrono>
19 
20 namespace biogears{
21 
22 enum class State {
23  Ready, // Timer has no data and is not running
24  Running, // Timer is currently running
25  Ran // Timer has been stopped and has data
26 };
27 
28 struct Timer {
29  std::chrono::high_resolution_clock::time_point start;
30  std::chrono::high_resolution_clock::time_point end;
32 };
33 
34 
35 class BIOGEARS_API TimingProfile : public Loggable {
36 public:
39  : Loggable(logger)
40  {
41  }
42  virtual ~TimingProfile() {}
43 
44  void Clear();
45 
52  void Reset(const std::string& label);
53 
59  void Start(const std::string& label);
60 
64  void Stop(const std::string& label);
65 
71  double GetElapsedTime_s(const std::string& label);
72 
77  void Print(const std::string& label);
78 
82  template <typename Duration>
83  typename Duration::rep GetElapsedTime(const std::string& label)
84  {
85  biogears::State state = m_timers[label].state;
86 
87  if (state == biogears::State::Running) {
88  return std::chrono::duration_cast<Duration>(std::chrono::high_resolution_clock::now() - m_timers[label].start).count();
89  } else if (state == biogears::State::Ran) {
90  return std::chrono::duration_cast<Duration>(m_timers[label].end - m_timers[label].start).count();
91  } else {
92  return typename Duration::rep(0);
93  }
94  }
95 
96 private:
97  std::map<std::string, biogears::Timer> m_timers;
98 
99  std::stringstream m_ss;
100 };
101 }
virtual ~TimingProfile()
Definition: TimingProfile.h:42
std::chrono::high_resolution_clock::time_point start
Definition: TimingProfile.h:29
Definition: Logger.h:27
TimingProfile(Logger *logger)
Definition: TimingProfile.h:38
Definition: Logger.h:75
std::chrono::high_resolution_clock::time_point end
Definition: TimingProfile.h:30
Duration::rep GetElapsedTime(const std::string &label)
Definition: TimingProfile.h:83
Definition: TimingProfile.h:28
std::map< std::string, biogears::Timer > m_timers
Definition: TimingProfile.h:97
State
Definition: TimingProfile.h:22
std::stringstream m_ss
Definition: TimingProfile.h:99
Definition: TimingProfile.h:35
State state
Definition: TimingProfile.h:31
TimingProfile()
Definition: TimingProfile.h:37
Definition: SEElectricalCircuit.h:18