changeset 447:5881e4af5799 pg-transactions

measure DB latency
author Alain Mazy <am@osimis.io>
date Mon, 15 Jan 2024 18:27:20 +0100
parents 9e039e65d68e
children f2427f94d879
files Framework/Plugins/DatabaseBackendAdapterV4.cpp Framework/Plugins/IDatabaseBackend.h Framework/Plugins/IndexBackend.cpp Framework/Plugins/IndexBackend.h
diffstat 4 files changed, 55 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/Framework/Plugins/DatabaseBackendAdapterV4.cpp	Mon Jan 15 15:21:43 2024 +0100
+++ b/Framework/Plugins/DatabaseBackendAdapterV4.cpp	Mon Jan 15 18:27:20 2024 +0100
@@ -433,6 +433,7 @@
         response.mutable_get_system_information()->set_supports_labels(accessor.GetBackend().HasLabelsSupport());
         response.mutable_get_system_information()->set_supports_increment_global_property(accessor.GetBackend().HasAtomicIncrementGlobalProperty());
         response.mutable_get_system_information()->set_has_update_and_get_statistics(accessor.GetBackend().HasUpdateAndGetStatistics());
+        response.mutable_get_system_information()->set_has_measure_latency(accessor.GetBackend().HasMeasureLatency());
         break;
       }
 
@@ -516,7 +517,14 @@
         
         break;
       }
-              
+
+      case Orthanc::DatabasePluginMessages::OPERATION_MEASURE_LATENCY:
+      {
+        IndexConnectionsPool::Accessor accessor(pool);
+        response.mutable_measure_latency()->set_latency_us(accessor.GetBackend().MeasureLatency(accessor.GetManager()));
+        break;
+      }
+
       default:
         LOG(ERROR) << "Not implemented database operation from protobuf: " << request.operation();
         throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange);
--- a/Framework/Plugins/IDatabaseBackend.h	Mon Jan 15 15:21:43 2024 +0100
+++ b/Framework/Plugins/IDatabaseBackend.h	Mon Jan 15 18:27:20 2024 +0100
@@ -349,15 +349,19 @@
     virtual void ListAllLabels(std::list<std::string>& target,
                                DatabaseManager& manager) = 0;
 
+    // New in Orthanc 1.12.X
     virtual bool HasAtomicIncrementGlobalProperty() = 0;
 
+    // New in Orthanc 1.12.X
     virtual int64_t IncrementGlobalProperty(DatabaseManager& manager,
                                             const char* serverIdentifier,
                                             int32_t property,
                                             int64_t increment) = 0;
 
+    // New in Orthanc 1.12.X
     virtual bool HasUpdateAndGetStatistics() = 0;
 
+    // New in Orthanc 1.12.X
     virtual void UpdateAndGetStatistics(DatabaseManager& manager,
                                         int64_t& patientsCount,
                                         int64_t& studiesCount,
@@ -366,5 +370,12 @@
                                         int64_t& compressedSize,
                                         int64_t& uncompressedSize) = 0;
 
+    // New in Orthanc 1.12.X
+    virtual bool HasMeasureLatency() = 0;
+
+    // New in Orthanc 1.12.X
+    virtual uint64_t MeasureLatency(DatabaseManager& manager) = 0;
+
+
   };
 }
--- a/Framework/Plugins/IndexBackend.cpp	Mon Jan 15 15:21:43 2024 +0100
+++ b/Framework/Plugins/IndexBackend.cpp	Mon Jan 15 18:27:20 2024 +0100
@@ -33,6 +33,7 @@
 #include <Compatibility.h>  // For std::unique_ptr<>
 #include <Logging.h>
 #include <OrthancException.h>
+#include <Toolbox.h>
 
 
 namespace OrthancDatabases
@@ -1236,6 +1237,12 @@
     throw Orthanc::OrthancException(Orthanc::ErrorCode_NotImplemented);
   }
 
+  bool IndexBackend::HasMeasureLatency()
+  {
+    return true;
+  }
+
+
   void IndexBackend::LookupIdentifier(std::list<int64_t>& target /*out*/,
                                       DatabaseManager& manager,
                                       OrthancPluginResourceType resourceType,
@@ -2870,6 +2877,30 @@
   }
 
 
+  uint64_t IndexBackend::MeasureLatency(DatabaseManager& manager)
+  {
+    // execute 11x the simplest statement and return the median value
+    std::vector<uint64_t> measures;
+
+    for (int i = 0; i < 11; i++)
+    {
+      DatabaseManager::CachedStatement statement(
+        STATEMENT_FROM_HERE, manager,
+        "SELECT 1");
+
+      Orthanc::Toolbox::ElapsedTimer timer;
+
+      statement.Execute();
+
+      measures.push_back(timer.GetElapsedMicroseconds());
+    }
+    
+    std::sort(measures.begin(), measures.end());
+
+    return measures[measures.size() / 2];
+  }
+
+
   DatabaseManager* IndexBackend::CreateSingleDatabaseManager(IDatabaseBackend& backend,
                                                              bool hasIdentifierTags,
                                                              const std::list<IdentifierTag>& identifierTags)
--- a/Framework/Plugins/IndexBackend.h	Mon Jan 15 15:21:43 2024 +0100
+++ b/Framework/Plugins/IndexBackend.h	Mon Jan 15 18:27:20 2024 +0100
@@ -415,6 +415,10 @@
                                         int64_t& compressedSize,
                                         int64_t& uncompressedSize) ORTHANC_OVERRIDE;
 
+    virtual bool HasMeasureLatency() ORTHANC_OVERRIDE;
+
+    virtual uint64_t MeasureLatency(DatabaseManager& manager) ORTHANC_OVERRIDE;
+
     /**
      * "maxDatabaseRetries" is to handle
      * "OrthancPluginErrorCode_DatabaseCannotSerialize" if there is a