From 365d5b6cbe07c3f65f8a82849f8248b083ea8ec2 Mon Sep 17 00:00:00 2001
From: Sergey Yakubov <sergey.yakubov@desy.de>
Date: Sun, 4 Feb 2018 11:42:02 +0100
Subject: [PATCH] Fix getnext_broker example

---
 3d_party/rapidjson/include/rapidjson/reader.h |  6 +--
 .../worker/getnext_broker/getnext_broker.cpp  | 38 +++++++++++--------
 2 files changed, 24 insertions(+), 20 deletions(-)

diff --git a/3d_party/rapidjson/include/rapidjson/reader.h b/3d_party/rapidjson/include/rapidjson/reader.h
index 206b26141..9b28b2685 100644
--- a/3d_party/rapidjson/include/rapidjson/reader.h
+++ b/3d_party/rapidjson/include/rapidjson/reader.h
@@ -1223,8 +1223,7 @@ class GenericReader {
                     }
                     i = i * 10 + static_cast<unsigned>(s.TakePush() - '0');
                     significandDigit++;
-                }
-            else
+                } else
                 while (RAPIDJSON_LIKELY(s.Peek() >= '0' && s.Peek() <= '9')) {
                     if (RAPIDJSON_UNLIKELY(i >= 429496729)) { // 2^32 - 1 = 4294967295
                         if (RAPIDJSON_LIKELY(i != 429496729 || s.Peek() > '5')) {
@@ -1265,8 +1264,7 @@ class GenericReader {
                         }
                     i64 = i64 * 10 + static_cast<unsigned>(s.TakePush() - '0');
                     significandDigit++;
-                }
-            else
+                } else
                 while (RAPIDJSON_LIKELY(s.Peek() >= '0' && s.Peek() <= '9')) {
                     if (RAPIDJSON_UNLIKELY(i64 >= RAPIDJSON_UINT64_C2(0x19999999, 0x99999999))) // 2^64 - 1 = 18446744073709551615
                         if (RAPIDJSON_LIKELY(i64 != RAPIDJSON_UINT64_C2(0x19999999, 0x99999999) || s.Peek() > '5')) {
diff --git a/examples/worker/getnext_broker/getnext_broker.cpp b/examples/worker/getnext_broker/getnext_broker.cpp
index 2ee32d782..5013c4669 100644
--- a/examples/worker/getnext_broker/getnext_broker.cpp
+++ b/examples/worker/getnext_broker/getnext_broker.cpp
@@ -5,42 +5,47 @@
 #include <thread>
 #include <chrono>
 #include <iomanip>
-
+#include <numeric>
 
 #include "hidra2_worker.h"
 
 using hidra2::WorkerErrorCode;
 using std::chrono::high_resolution_clock;
 
-int ReadAllData(const std::string& server, const std::string& run_name, int nthreads, uint64_t* duration_ms) {
-    hidra2::FileInfo fi;
-    high_resolution_clock::time_point t1 = high_resolution_clock::now();
+void WaitThreads(std::vector<std::thread>* threads) {
+    for (auto& thread : *threads) {
+        thread.join();
+    }
+}
 
-    std::vector<int>nfiles(nthreads, 0);
-    auto exec_next = [server, run_name, &nfiles](int i) {
+std::vector<std::thread> StartThreads(const std::string& server, const std::string& run_name, int nthreads,
+                                      std::vector<int>* nfiles) {
+    auto exec_next = [server, run_name, nfiles](int i) {
         hidra2::FileInfo fi;
         hidra2::WorkerErrorCode err;
         auto broker = hidra2::DataBrokerFactory::CreateServerBroker(server, run_name, &err);
         while (broker->GetNext(&fi, nullptr) == WorkerErrorCode::kOK) {
-            nfiles[i] ++;
-            if (nfiles[i] > 150000) break;
+            (*nfiles)[i] ++;
         }
     };
 
     std::vector<std::thread> threads;
     for (int i = 0; i < nthreads; i++) {
-        exec_next(i);
         threads.emplace_back(std::thread(exec_next, i));
     }
+    return threads;
+}
 
-    for (auto& thread : threads) {
-        thread.join();
-    }
+int ReadAllData(const std::string& server, const std::string& run_name, int nthreads, uint64_t* duration_ms) {
+    hidra2::FileInfo fi;
+    high_resolution_clock::time_point t1 = high_resolution_clock::now();
 
-    int n_total = 0;
-    for (auto n : nfiles) {
-        n_total += n;
-    }
+    std::vector<int>nfiles(nthreads, 0);
+
+    auto threads = StartThreads(server, run_name, nthreads, &nfiles);
+    WaitThreads(&threads);
+
+    int n_total = std::accumulate(nfiles.begin(), nfiles.end(), 0);
 
     high_resolution_clock::time_point t2 = high_resolution_clock::now();
     auto duration_read = std::chrono::duration_cast<std::chrono::milliseconds>( t2 - t1 );
@@ -63,5 +68,6 @@ int main(int argc, char* argv[]) {
 
     std::cout << "Processed " << nfiles << " file(s)" << std::endl;
     std::cout << "Elapsed : " << duration_ms << "ms" << std::endl;
+    std::cout << "Rate : " << 1000.0f * nfiles / duration_ms << std::endl;
     return 0;
 }
-- 
GitLab