# HG changeset patch # User Sebastien Jodogne # Date 1687708174 -7200 # Node ID 9c00e832985f5e7fa980f5ba8e5d0f0524b68472 # Parent e95caa87fed80c4724b5e43ea68fd4780fb35898 refactoring MetricsRegistry diff -r e95caa87fed8 -r 9c00e832985f OrthancFramework/Sources/FileStorage/StorageAccessor.cpp --- a/OrthancFramework/Sources/FileStorage/StorageAccessor.cpp Sun Jun 25 15:19:25 2023 +0200 +++ b/OrthancFramework/Sources/FileStorage/StorageAccessor.cpp Sun Jun 25 17:49:34 2023 +0200 @@ -38,9 +38,9 @@ #endif -static const std::string METRICS_CREATE = "orthanc_storage_create_duration_ms"; -static const std::string METRICS_READ = "orthanc_storage_read_duration_ms"; -static const std::string METRICS_REMOVE = "orthanc_storage_remove_duration_ms"; +static const std::string METRICS_CREATE_DURATION = "orthanc_storage_create_duration_ms"; +static const std::string METRICS_READ_DURATION = "orthanc_storage_read_duration_ms"; +static const std::string METRICS_REMOVE_DURATION = "orthanc_storage_remove_duration_ms"; namespace Orthanc @@ -116,7 +116,7 @@ { case CompressionType_None: { - MetricsTimer timer(*this, METRICS_CREATE); + MetricsTimer timer(*this, METRICS_CREATE_DURATION); area_.Create(uuid, data, size, type); @@ -143,7 +143,7 @@ } { - MetricsTimer timer(*this, METRICS_CREATE); + MetricsTimer timer(*this, METRICS_CREATE_DURATION); if (compressed.size() > 0) { @@ -189,7 +189,7 @@ { case CompressionType_None: { - MetricsTimer timer(*this, METRICS_READ); + MetricsTimer timer(*this, METRICS_READ_DURATION); std::unique_ptr buffer(area_.Read(info.GetUuid(), info.GetContentType())); buffer->MoveToString(content); @@ -203,7 +203,7 @@ std::unique_ptr compressed; { - MetricsTimer timer(*this, METRICS_READ); + MetricsTimer timer(*this, METRICS_READ_DURATION); compressed.reset(area_.Read(info.GetUuid(), info.GetContentType())); } @@ -234,7 +234,7 @@ { if (cache_ == NULL || !cache_->Fetch(content, info.GetUuid(), info.GetContentType())) { - MetricsTimer timer(*this, METRICS_READ); + MetricsTimer timer(*this, METRICS_READ_DURATION); std::unique_ptr buffer(area_.Read(info.GetUuid(), info.GetContentType())); buffer->MoveToString(content); } @@ -250,7 +250,7 @@ } { - MetricsTimer timer(*this, METRICS_REMOVE); + MetricsTimer timer(*this, METRICS_REMOVE_DURATION); area_.Remove(fileUuid, type); } } @@ -269,7 +269,7 @@ { if (cache_ == NULL || !cache_->FetchStartRange(target, fileUuid, contentType, end)) { - MetricsTimer timer(*this, METRICS_READ); + MetricsTimer timer(*this, METRICS_READ_DURATION); std::unique_ptr buffer(area_.ReadRange(fileUuid, contentType, 0, end)); assert(buffer->GetSize() == end); buffer->MoveToString(target); diff -r e95caa87fed8 -r 9c00e832985f OrthancFramework/Sources/MetricsRegistry.cpp --- a/OrthancFramework/Sources/MetricsRegistry.cpp Sun Jun 25 15:19:25 2023 +0200 +++ b/OrthancFramework/Sources/MetricsRegistry.cpp Sun Jun 25 17:49:34 2023 +0200 @@ -214,24 +214,22 @@ } } - void MetricsRegistry::SetValueInternal(const std::string& name, - float value, - MetricsType type) + + MetricsRegistry::Item& MetricsRegistry::GetItemInternal(const std::string& name, + MetricsType type) { - boost::mutex::scoped_lock lock(mutex_); - Content::iterator found = content_.find(name); if (found == content_.end()) { - std::unique_ptr item(new Item(type)); - item->Update(value); - content_[name] = item.release(); + Item* item = new Item(type); + content_[name] = item; + return *item; } else { assert(found->second != NULL); - found->second->Update(value); + return *found->second; } } @@ -248,14 +246,30 @@ // Inlining to avoid loosing time if metrics are disabled if (enabled_) { - SetValueInternal(name, value, type); + boost::mutex::scoped_lock lock(mutex_); + GetItemInternal(name, type).Update(value); } } - void MetricsRegistry::SetValue(const std::string &name, float value) + void MetricsRegistry::IncrementValue(const std::string &name, + float delta) { - SetValue(name, value, MetricsType_Default); + // Inlining to avoid loosing time if metrics are disabled + if (enabled_) + { + boost::mutex::scoped_lock lock(mutex_); + Item& item = GetItemInternal(name, MetricsType_Default); + + if (item.HasValue()) + { + item.Update(item.GetValue() + delta); + } + else + { + item.Update(delta); + } + } } diff -r e95caa87fed8 -r 9c00e832985f OrthancFramework/Sources/MetricsRegistry.h --- a/OrthancFramework/Sources/MetricsRegistry.h Sun Jun 25 15:19:25 2023 +0200 +++ b/OrthancFramework/Sources/MetricsRegistry.h Sun Jun 25 17:49:34 2023 +0200 @@ -58,8 +58,8 @@ boost::mutex mutex_; Content content_; - void SetValueInternal(const std::string& name, - float value, + // The mutex must be locked + Item& GetItemInternal(const std::string& name, MetricsType type); public: @@ -79,7 +79,13 @@ MetricsType type); void SetValue(const std::string& name, - float value); + float value) + { + SetValue(name, value, MetricsType_Default); + } + + void IncrementValue(const std::string& name, + float delta); MetricsType GetMetricsType(const std::string& name);