changeset 5325:9c00e832985f

refactoring MetricsRegistry
author Sebastien Jodogne <s.jodogne@gmail.com>
date Sun, 25 Jun 2023 17:49:34 +0200
parents e95caa87fed8
children fbe857e942cd
files OrthancFramework/Sources/FileStorage/StorageAccessor.cpp OrthancFramework/Sources/MetricsRegistry.cpp OrthancFramework/Sources/MetricsRegistry.h
diffstat 3 files changed, 45 insertions(+), 25 deletions(-) [+]
line wrap: on
line diff
--- 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<IMemoryBuffer> buffer(area_.Read(info.GetUuid(), info.GetContentType()));
           buffer->MoveToString(content);
 
@@ -203,7 +203,7 @@
           std::unique_ptr<IMemoryBuffer> 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<IMemoryBuffer> 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<IMemoryBuffer> buffer(area_.ReadRange(fileUuid, contentType, 0, end));
       assert(buffer->GetSize() == end);
       buffer->MoveToString(target);
--- 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> 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);
+      }
+    }
   }
 
 
--- 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);