-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprofiler.h
75 lines (57 loc) · 1.64 KB
/
profiler.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#pragma once
#include "config.h"
#include <chrono>
#include <QMap>
#ifdef PROFILING_TIME_ENABLE
# define PROFILING_TIME2(functionName) [[maybe_unused]] const auto& ___p = Profiler::instance().createTimeUnit(functionName);
# define PROFILING_TIME PROFILING_TIME2(__FUNCTION__)
#else
# define PROFILING_TIME2(functionName)
# define PROFILING_TIME
#endif
class QFile;
class Profiler;
class TimeUnit {
public:
TimeUnit(const QString &functionName);
~TimeUnit();
private:
void start();
void stop();
void writeElapsed();
private:
QString m_functionName;
std::chrono::steady_clock::time_point m_begin;
std::chrono::steady_clock::time_point m_end;
};
class Profiler {
friend class TimeUnit;
public:
static Profiler& instance();
static void setLogFile(const QString &fileName);
[[nodiscard]]
TimeUnit createTimeUnit(const QString &functionName);
void dumpLog();
private:
Profiler() = default;
Profiler(const Profiler& root) = delete;
Profiler& operator=(const Profiler&) = delete;
void addElapsedTime(const QString &functionName, double time);
class FunctionTimeInfo {
public:
FunctionTimeInfo() = default;
FunctionTimeInfo(const QString &name);
void addElapsedTime(double time);
const QString &name() const;
double totalElapsed() const;
double averageElapsed() const;
long runCount() const;
private:
QString m_name = "undef_function_name";
long m_runCount = 0;
double m_totalElapsed = 0;
};
private:
static QString m_fileName;
QMap<QString, FunctionTimeInfo> m_funcTimeInfoMap;
};