comparison OrthancFramework/Sources/MetricsRegistry.cpp @ 5326:fbe857e942cd

store metrics as integers instead of floats to avoid precision loss in increments
author Sebastien Jodogne <s.jodogne@gmail.com>
date Sun, 25 Jun 2023 18:28:49 +0200
parents 9c00e832985f
children c103b0e70d75
comparison
equal deleted inserted replaced
5325:9c00e832985f 5326:fbe857e942cd
39 { 39 {
40 private: 40 private:
41 MetricsType type_; 41 MetricsType type_;
42 boost::posix_time::ptime time_; 42 boost::posix_time::ptime time_;
43 bool hasValue_; 43 bool hasValue_;
44 float value_; 44 int64_t value_;
45 45
46 void Touch(float value, 46 void Touch(int64_t value,
47 const boost::posix_time::ptime& now) 47 const boost::posix_time::ptime& now)
48 { 48 {
49 hasValue_ = true; 49 hasValue_ = true;
50 value_ = value; 50 value_ = value;
51 time_ = now; 51 time_ = now;
52 } 52 }
53 53
54 void Touch(float value) 54 void Touch(int64_t value)
55 { 55 {
56 Touch(value, GetNow()); 56 Touch(value, GetNow());
57 } 57 }
58 58
59 void UpdateMax(float value, 59 void UpdateMax(int64_t value,
60 int duration) 60 int duration)
61 { 61 {
62 if (hasValue_) 62 if (hasValue_)
63 { 63 {
64 const boost::posix_time::ptime now = GetNow(); 64 const boost::posix_time::ptime now = GetNow();
73 { 73 {
74 Touch(value); 74 Touch(value);
75 } 75 }
76 } 76 }
77 77
78 void UpdateMin(float value, 78 void UpdateMin(int64_t value,
79 int duration) 79 int duration)
80 { 80 {
81 if (hasValue_) 81 if (hasValue_)
82 { 82 {
83 const boost::posix_time::ptime now = GetNow(); 83 const boost::posix_time::ptime now = GetNow();
105 MetricsType GetType() const 105 MetricsType GetType() const
106 { 106 {
107 return type_; 107 return type_;
108 } 108 }
109 109
110 void Update(float value) 110 void Update(int64_t value)
111 { 111 {
112 switch (type_) 112 switch (type_)
113 { 113 {
114 case MetricsType_Default: 114 case MetricsType_Default:
115 Touch(value); 115 Touch(value);
151 { 151 {
152 throw OrthancException(ErrorCode_BadSequenceOfCalls); 152 throw OrthancException(ErrorCode_BadSequenceOfCalls);
153 } 153 }
154 } 154 }
155 155
156 float GetValue() const 156 int64_t GetValue() const
157 { 157 {
158 if (hasValue_) 158 if (hasValue_)
159 { 159 {
160 return value_; 160 return value_;
161 } 161 }
238 { 238 {
239 } 239 }
240 240
241 241
242 void MetricsRegistry::SetValue(const std::string &name, 242 void MetricsRegistry::SetValue(const std::string &name,
243 float value, 243 int64_t value,
244 MetricsType type) 244 MetricsType type)
245 { 245 {
246 // Inlining to avoid loosing time if metrics are disabled 246 // Inlining to avoid loosing time if metrics are disabled
247 if (enabled_) 247 if (enabled_)
248 { 248 {
251 } 251 }
252 } 252 }
253 253
254 254
255 void MetricsRegistry::IncrementValue(const std::string &name, 255 void MetricsRegistry::IncrementValue(const std::string &name,
256 float delta) 256 int64_t delta)
257 { 257 {
258 // Inlining to avoid loosing time if metrics are disabled 258 // Inlining to avoid loosing time if metrics are disabled
259 if (enabled_) 259 if (enabled_)
260 { 260 {
261 boost::mutex::scoped_lock lock(mutex_); 261 boost::mutex::scoped_lock lock(mutex_);
335 name_(name), 335 name_(name),
336 value_(0) 336 value_(0)
337 { 337 {
338 } 338 }
339 339
340 void MetricsRegistry::SharedMetrics::Add(float delta) 340 void MetricsRegistry::SharedMetrics::Add(int64_t delta)
341 { 341 {
342 boost::mutex::scoped_lock lock(mutex_); 342 boost::mutex::scoped_lock lock(mutex_);
343 value_ += delta; 343 value_ += delta;
344 registry_.SetValue(name_, value_); 344 registry_.SetValue(name_, value_);
345 } 345 }
396 { 396 {
397 if (active_) 397 if (active_)
398 { 398 {
399 boost::posix_time::time_duration diff = GetNow() - start_; 399 boost::posix_time::time_duration diff = GetNow() - start_;
400 registry_.SetValue( 400 registry_.SetValue(
401 name_, static_cast<float>(diff.total_milliseconds()), type_); 401 name_, static_cast<int64_t>(diff.total_milliseconds()), type_);
402 } 402 }
403 } 403 }
404 } 404 }