/src/geos/include/geos/profiler.h
Line | Count | Source |
1 | | /********************************************************************** |
2 | | * |
3 | | * GEOS - Geometry Engine Open Source |
4 | | * http://geos.osgeo.org |
5 | | * |
6 | | * Copyright (C) 2001-2002 Vivid Solutions Inc. |
7 | | * |
8 | | * This is free software; you can redistribute and/or modify it under |
9 | | * the terms of the GNU Lesser General Public Licence as published |
10 | | * by the Free Software Foundation. |
11 | | * See the COPYING file for more information. |
12 | | * |
13 | | **********************************************************************/ |
14 | | |
15 | | #pragma once |
16 | | |
17 | | #include <geos/export.h> |
18 | | #include <chrono> |
19 | | |
20 | | #include <map> |
21 | | #include <memory> |
22 | | #include <iostream> |
23 | | #include <string> |
24 | | #include <vector> |
25 | | |
26 | | #ifndef PROFILE |
27 | | #define PROFILE 0 |
28 | | #endif |
29 | | |
30 | | #ifdef _MSC_VER |
31 | | #pragma warning(push) |
32 | | #pragma warning(disable: 4251) // warning C4251: needs to have dll-interface to be used by clients of class |
33 | | #endif |
34 | | |
35 | | namespace geos { |
36 | | namespace util { |
37 | | |
38 | | |
39 | | /* |
40 | | * \class Profile utils.h geos.h |
41 | | * |
42 | | * \brief Profile statistics |
43 | | */ |
44 | | class GEOS_DLL Profile { |
45 | | public: |
46 | | using timeunit = std::chrono::microseconds; |
47 | | |
48 | | /** \brief Create a named profile */ |
49 | | Profile(std::string name); |
50 | | |
51 | | /** \brief Destructor */ |
52 | | ~Profile() = default; |
53 | | |
54 | | /** \brief start a new timer */ |
55 | | void |
56 | | start() |
57 | 0 | { |
58 | 0 | starttime = std::chrono::high_resolution_clock::now(); |
59 | 0 | } |
60 | | |
61 | | /** \brief stop current timer */ |
62 | | void |
63 | | stop() |
64 | 0 | { |
65 | 0 | stoptime = std::chrono::high_resolution_clock::now(); |
66 | 0 | auto elapsed = std::chrono::duration_cast<timeunit>(stoptime - starttime); |
67 | 0 |
|
68 | 0 | timings.push_back(elapsed); |
69 | 0 |
|
70 | 0 | totaltime += elapsed; |
71 | 0 | if(timings.size() == 1) { |
72 | 0 | max = min = elapsed; |
73 | 0 | } |
74 | 0 | else { |
75 | 0 | if(elapsed > max) { |
76 | 0 | max = elapsed; |
77 | 0 | } |
78 | 0 | if(elapsed < min) { |
79 | 0 | min = elapsed; |
80 | 0 | } |
81 | 0 | } |
82 | 0 |
|
83 | 0 | avg = static_cast<double>(totaltime.count()) / static_cast<double>(timings.size()); |
84 | 0 | } |
85 | | |
86 | | /** \brief Return Max stored timing */ |
87 | | double getMax() const; |
88 | | |
89 | | /** \brief Return Min stored timing */ |
90 | | double getMin() const; |
91 | | |
92 | | /** \brief Return total timing */ |
93 | | double getTot() const; |
94 | | |
95 | | /** \brief Return total timing */ |
96 | | std::string getTotFormatted() const; |
97 | | |
98 | | /** \brief Return average timing */ |
99 | | double getAvg() const; |
100 | | |
101 | | /** \brief Return number of timings */ |
102 | | std::size_t getNumTimings() const; |
103 | | |
104 | | /** \brief Profile name */ |
105 | | std::string name; |
106 | | |
107 | | |
108 | | |
109 | | private: |
110 | | /* \brief current start and stop times */ |
111 | | std::chrono::high_resolution_clock::time_point starttime, stoptime; |
112 | | |
113 | | /* \brief actual times */ |
114 | | std::vector<timeunit> timings; |
115 | | |
116 | | /* \brief total time */ |
117 | | timeunit totaltime; |
118 | | |
119 | | /* \brief max time */ |
120 | | timeunit max; |
121 | | |
122 | | /* \brief max time */ |
123 | | timeunit min; |
124 | | |
125 | | /* \brief avg time */ |
126 | | double avg; |
127 | | }; |
128 | | |
129 | | /* |
130 | | * \class Profiler utils.h geos.h |
131 | | * |
132 | | * \brief Profiling class |
133 | | * |
134 | | */ |
135 | | class GEOS_DLL Profiler { |
136 | | |
137 | | public: |
138 | | |
139 | | Profiler() = default; |
140 | | ~Profiler() = default; |
141 | | |
142 | | Profiler(const Profiler&) = delete; |
143 | | Profiler& operator=(const Profiler&) = delete; |
144 | | |
145 | | /** |
146 | | * \brief |
147 | | * Return the singleton instance of the |
148 | | * profiler. |
149 | | */ |
150 | | static Profiler* instance(void); |
151 | | |
152 | | /** |
153 | | * \brief |
154 | | * Start timer for named task. The task is |
155 | | * created if does not exist. |
156 | | */ |
157 | | void start(std::string name); |
158 | | |
159 | | /** |
160 | | * \brief |
161 | | * Stop timer for named task. |
162 | | * Elapsed time is registered in the given task. |
163 | | */ |
164 | | void stop(std::string name); |
165 | | |
166 | | /** \brief get Profile of named task */ |
167 | | Profile* get(std::string name); |
168 | | |
169 | | std::map<std::string, std::unique_ptr<Profile>> profs; |
170 | | }; |
171 | | |
172 | | |
173 | | /** \brief Return a string representing the Profile */ |
174 | | GEOS_DLL std::ostream& operator<< (std::ostream& os, const Profile&); |
175 | | |
176 | | /** \brief Return a string representing the Profiler */ |
177 | | GEOS_DLL std::ostream& operator<< (std::ostream& os, const Profiler&); |
178 | | |
179 | | } // namespace geos::util |
180 | | } // namespace geos |
181 | | |
182 | | #ifdef _MSC_VER |
183 | | #pragma warning(pop) |
184 | | #endif |
185 | | |