comparison OrthancFramework/Sources/MetricsRegistry.cpp @ 4300:b30a8de92ad9

abi continued
author Sebastien Jodogne <s.jodogne@gmail.com>
date Thu, 05 Nov 2020 19:33:18 +0100
parents 4d42408da117
children d9473bd5ed43
comparison
equal deleted inserted replaced
4299:3f85db78c441 4300:b30a8de92ad9
173 assert(it->second != NULL); 173 assert(it->second != NULL);
174 delete it->second; 174 delete it->second;
175 } 175 }
176 } 176 }
177 177
178 bool MetricsRegistry::IsEnabled() const
179 {
180 return enabled_;
181 }
182
178 183
179 void MetricsRegistry::SetEnabled(bool enabled) 184 void MetricsRegistry::SetEnabled(bool enabled)
180 { 185 {
181 boost::mutex::scoped_lock lock(mutex_); 186 boost::mutex::scoped_lock lock(mutex_);
182 enabled_ = enabled; 187 enabled_ = enabled;
206 found->second = new Item(type); 211 found->second = new Item(type);
207 } 212 }
208 } 213 }
209 } 214 }
210 215
211
212 void MetricsRegistry::SetValueInternal(const std::string& name, 216 void MetricsRegistry::SetValueInternal(const std::string& name,
213 float value, 217 float value,
214 MetricsType type) 218 MetricsType type)
215 { 219 {
216 boost::mutex::scoped_lock lock(mutex_); 220 boost::mutex::scoped_lock lock(mutex_);
228 assert(found->second != NULL); 232 assert(found->second != NULL);
229 found->second->Update(value); 233 found->second->Update(value);
230 } 234 }
231 } 235 }
232 236
237 MetricsRegistry::MetricsRegistry() :
238 enabled_(true)
239 {
240 }
241
242
243 void MetricsRegistry::SetValue(const std::string &name,
244 float value,
245 MetricsType type)
246 {
247 // Inlining to avoid loosing time if metrics are disabled
248 if (enabled_)
249 {
250 SetValueInternal(name, value, type);
251 }
252 }
253
254
255 void MetricsRegistry::SetValue(const std::string &name, float value)
256 {
257 SetValue(name, value, MetricsType_Default);
258 }
259
233 260
234 MetricsType MetricsRegistry::GetMetricsType(const std::string& name) 261 MetricsType MetricsRegistry::GetMetricsType(const std::string& name)
235 { 262 {
236 boost::mutex::scoped_lock lock(mutex_); 263 boost::mutex::scoped_lock lock(mutex_);
237 264
284 311
285 buffer.Flatten(s); 312 buffer.Flatten(s);
286 } 313 }
287 314
288 315
316 MetricsRegistry::SharedMetrics::SharedMetrics(MetricsRegistry &registry,
317 const std::string &name,
318 MetricsType type) :
319 registry_(registry),
320 name_(name),
321 value_(0)
322 {
323 }
324
289 void MetricsRegistry::SharedMetrics::Add(float delta) 325 void MetricsRegistry::SharedMetrics::Add(float delta)
290 { 326 {
291 boost::mutex::scoped_lock lock(mutex_); 327 boost::mutex::scoped_lock lock(mutex_);
292 value_ += delta; 328 value_ += delta;
293 registry_.SetValue(name_, value_); 329 registry_.SetValue(name_, value_);
294 } 330 }
295 331
296 332
333 MetricsRegistry::ActiveCounter::ActiveCounter(MetricsRegistry::SharedMetrics &metrics) :
334 metrics_(metrics)
335 {
336 metrics_.Add(1);
337 }
338
339 MetricsRegistry::ActiveCounter::~ActiveCounter()
340 {
341 metrics_.Add(-1);
342 }
343
344
297 void MetricsRegistry::Timer::Start() 345 void MetricsRegistry::Timer::Start()
298 { 346 {
299 if (registry_.IsEnabled()) 347 if (registry_.IsEnabled())
300 { 348 {
301 active_ = true; 349 active_ = true;
306 active_ = false; 354 active_ = false;
307 } 355 }
308 } 356 }
309 357
310 358
359 MetricsRegistry::Timer::Timer(MetricsRegistry &registry,
360 const std::string &name) :
361 registry_(registry),
362 name_(name),
363 type_(MetricsType_MaxOver10Seconds)
364 {
365 Start();
366 }
367
368
369 MetricsRegistry::Timer::Timer(MetricsRegistry &registry,
370 const std::string &name,
371 MetricsType type) :
372 registry_(registry),
373 name_(name),
374 type_(type)
375 {
376 Start();
377 }
378
379
311 MetricsRegistry::Timer::~Timer() 380 MetricsRegistry::Timer::~Timer()
312 { 381 {
313 if (active_) 382 if (active_)
314 { 383 {
315 boost::posix_time::time_duration diff = GetNow() - start_; 384 boost::posix_time::time_duration diff = GetNow() - start_;
316 registry_.SetValue( 385 registry_.SetValue(
317 name_, static_cast<float>(diff.total_milliseconds()), type_); 386 name_, static_cast<float>(diff.total_milliseconds()), type_);
318 } 387 }
319 } 388 }
320 } 389 }