Browse Source

changed read requests to async (non-blocking) and added some statistics

thomas 4 years ago
parent
commit
e0bc0a0c10
1 changed files with 48 additions and 20 deletions
  1. 48 20
      src/ua-stress-test/main.cpp

+ 48 - 20
src/ua-stress-test/main.cpp

@@ -4,20 +4,21 @@
 #include <stdio.h>
 #include <libconfig.h>
 #include <signal.h>
-#include <open62541/client_config_default.h>
-#include <open62541/client_highlevel.h>
 
 // C++ includes
 #include <iostream>
 #include <thread>
+#include <list>
 
 // custom includes
 #include "helper_functions.h"
 
-// OPC UA general includes
+// OPC UA includes
 #include <open62541/server.h>
 #include "helper_functions.h"
-#include <list>
+#include <open62541/client_highlevel_async.h>
+#include <open62541/client_config_default.h>
+#include <open62541/client_highlevel.h>
 
 int cfg_NumberOfConnections = 10;
 int cfg_OpcUaReadsPerConnectionPerS = 1;
@@ -28,10 +29,37 @@ using namespace std;
 
 bool stop = false; // when set to true, the client threads terminate
 
+struct myUserdata {
+    int threadId=0;
+    int numberOfResponesReceived=0;
+};
+
+static void readValueAttributeCallback(UA_Client *client, void *userdata,
+                           UA_UInt32 requestId, UA_Variant *var) {
+    struct myUserdata* d = (struct myUserdata*) userdata;
+    d->numberOfResponesReceived++;
+    if (cfg_EnableConsoleOutput) {
+        struct timespec now;
+        float nowS;
+        UA_DateTime raw_date; 
+        UA_DateTimeStruct dts;
+
+        clock_gettime(CLOCK_MONOTONIC, &now);
+        nowS = now.tv_sec + (((double)now.tv_nsec) / 1000000000.0);
+
+        raw_date = *(UA_DateTime *) var->data;
+        dts = UA_DateTime_toStruct(raw_date);
+        printf("Thread %d, RequestId %d, Timestamp %09lf, Result %d-%d-%d-%d-%d-%d\n", d->threadId, requestId, nowS, dts.day, dts.month, dts.year, dts.hour, dts.min, dts.sec);
+        fflush(stdout);
+    }
+}
+
 void workerThread()
 {
     static int totalThreads = 0;
     int threadId = totalThreads++;
+    struct myUserdata d;
+    d.threadId = threadId;
     cout << "Thread " << threadId << " starting" << endl;
 
     // open OPC UA connection to server
@@ -49,27 +77,27 @@ void workerThread()
     const UA_NodeId nodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER_SERVERSTATUS_CURRENTTIME);
     chrono::system_clock::time_point startTime = chrono::system_clock::now();
     unsigned long intervalUs = 1.0 / ((double)cfg_OpcUaReadsPerConnectionPerS) * 1000000.0;
-    UA_Variant value;
-    UA_DateTime raw_date; 
-    UA_DateTimeStruct dts; 
+    UA_UInt32 reqId = 0;
     for(int i = 0; !stop; i++) // loop repeats every intervalUs microseconds
     {
         std::this_thread::sleep_until(startTime + chrono::microseconds(i * intervalUs));
-        retval = UA_Client_readValueAttribute(client, nodeId, &value);
-        if (cfg_EnableConsoleOutput) {
-            raw_date = *(UA_DateTime *) value.data;
-            dts = UA_DateTime_toStruct(raw_date);
-            cout << "Thread " << threadId << ": " << dts.day << "-" << dts.month << "-" << dts.year << "-" << dts.hour << "-" << dts.min << "-" << dts.sec << endl;
-        }
-        if (retval != UA_STATUSCODE_GOOD) {
-            UA_Client_delete(client);
-            cerr << "Could not read node NS=" << nodeId.namespaceIndex << ",i=" << nodeId.identifier.numeric << " from OPC UA server" << endl;
-            return;
-        }
+        UA_Client_readValueAttribute_async(client,
+                                            nodeId,
+                                            readValueAttributeCallback, 
+                                            &d,
+                                            &reqId);
+        UA_Client_run_iterate(client, 0);
     }
+    chrono::system_clock::time_point endTime = chrono::system_clock::now();
     UA_Client_disconnect(client);
-
-    cout << "Thread " << threadId << " terminating" << endl;
+    UA_Client_delete(client);
+
+    cout << "Thread " << threadId << " terminating. ";
+    cout << "Statistics: " << endl;
+    cout << "\tRequests sent: " << reqId << endl;
+    cout << "\tResponses received: " << d.numberOfResponesReceived << endl;
+    cout << "\tRuntime(ms): " << (std::chrono::duration_cast<std::chrono::milliseconds>(endTime-startTime)).count() << endl;
+    cout << "\tRequests sent per s: " << ((double)reqId) / (std::chrono::duration_cast<std::chrono::seconds>(endTime-startTime)).count() << endl;
 }
 
 int main(int argc, char **argv) {