diff Framework/Plugins/IndexBackend.cpp @ 447:5881e4af5799 pg-transactions

measure DB latency
author Alain Mazy <am@osimis.io>
date Mon, 15 Jan 2024 18:27:20 +0100
parents f16faa1fdc46
children 6780dd8b0a66
line wrap: on
line diff
--- 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)