MxEngine
Profiler.h
1 // Copyright(c) 2019 - 2020, #Momo
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are met :
6 //
7 // 1. Redistributions of source code must retain the above copyright notice, this
8 // list of conditions and the following disclaimer.
9 //
10 // 2. Redistributions in binary form must reproduce the above copyright notice,
11 // this list of conditions and the following disclaimer in the documentation
12 // and /or other materials provided with the distribution.
13 //
14 // 3. Neither the name of the copyright holder nor the names of its
15 // contributors may be used to endorse or promote products derived from
16 // this software without specific prior written permission.
17 //
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 // DISCLAIMED.IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
22 // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 // DAMAGES(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24 // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
25 // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26 // OR TORT(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 
29 #pragma once
30 
31 #include "Utilities/Time/Time.h"
32 #include "Utilities/Logger/Logger.h"
33 #include "Utilities/SingletonHolder/SingletonHolder.h"
34 
35 #include <fstream>
36 #include <string>
37 
38 namespace MxEngine
39 {
45  {
49  std::ofstream stream;
53  size_t entriesCount = 0;
54 
58  void WriteJsonHeader();
62  void WriteJsonFooter();
63  public:
68  bool IsValid() const;
73  size_t GetEntryCount() const;
78  void StartSession(const MxString& filename);
85  void WriteJsonEntry(const char* function, TimeStep begin, TimeStep delta);
89  void EndSession();
90  };
91 
93 
99  {
103  TimeStep start;
107  const char* function;
111  ProfileSession& profiler;
112  public:
118  inline ScopeProfiler(ProfileSession& profiler, const char* function)
119  : start(Time::Current()), function(function), profiler(profiler) { }
120 
124  inline ~ScopeProfiler()
125  {
126  TimeStep end = Time::Current();
127  this->profiler.WriteJsonEntry(this->function, this->start, end - start);
128  }
129  };
130 
136  {
140  TimeStep start;
144  MxString function;
148  std::string_view invoker;
149  public:
155  inline ScopeTimer(std::string_view invoker, std::string_view function)
156  : start(Time::Current()), invoker(invoker), function(function.data())
157  {
158  Logger::Instance().Debug(this->invoker.data(), "calling " + this->function);
159  }
160 
164  inline ~ScopeTimer()
165  {
166  TimeStep end = Time::Current();
167  MxString delta = BeautifyTime(end - start);
168  Logger::Instance().Debug(this->invoker.data(), this->function + " finished in " + delta);
169  }
170  };
171 
172 // wrapper aroung ScopeProfiler
173 #define MAKE_SCOPE_PROFILER(function) ScopeProfiler MXENGINE_CONCAT(_profiler, __LINE__)(Profiler::Instance(), function)
174 // wrapper around ScopeTimer
175 #define MAKE_SCOPE_TIMER(invoker, function) ScopeTimer MXENGINE_CONCAT(_timer, __LINE__)(invoker, function)
176 }
Definition: Profiler.h:135
~ScopeProfiler()
Definition: Profiler.h:124
ScopeTimer(std::string_view invoker, std::string_view function)
Definition: Profiler.h:155
void StartSession(const MxString &filename)
Definition: Profiler.cpp:57
MxString BeautifyTime(TimeStep time)
Definition: Time.cpp:51
bool IsValid() const
Definition: Profiler.cpp:47
Definition: SingletonHolder.h:48
static TimeStep Current()
static T & Instance()
Definition: SingletonHolder.h:80
ScopeProfiler(ProfileSession &profiler, const char *function)
Definition: Profiler.h:118
Definition: Profiler.h:98
Definition: Profiler.h:44
void EndSession()
Definition: Profiler.cpp:84
void WriteJsonEntry(const char *function, TimeStep begin, TimeStep delta)
Definition: Profiler.cpp:64
size_t GetEntryCount() const
Definition: Profiler.cpp:52
Definition: Time.h:48
~ScopeTimer()
Definition: Profiler.h:164
Definition: Application.cpp:49