main.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. // This is a minimal example that starts a number of workerThreads and terminates them afer a key press
  2. // C includes
  3. #include <stdio.h>
  4. #include <libconfig.h>
  5. #include <signal.h>
  6. #include <open62541/client_config_default.h>
  7. #include <open62541/client_highlevel.h>
  8. // C++ includes
  9. #include <iostream>
  10. #include <thread>
  11. // custom includes
  12. #include "helper_functions.h"
  13. // OPC UA general includes
  14. #include <open62541/server.h>
  15. #include "helper_functions.h"
  16. #include <list>
  17. int cfg_NumberOfConnections = 10;
  18. int cfg_OpcUaReadsPerConnectionPerS = 1;
  19. bool cfg_EnableConsoleOutput;
  20. char *cfg_OpcUa_ServerAddress;
  21. using namespace std;
  22. bool stop = false; // when set to true, the client threads terminate
  23. void workerThread()
  24. {
  25. static int totalThreads = 0;
  26. int threadId = totalThreads++;
  27. cout << "Thread " << threadId << " starting" << endl;
  28. // open OPC UA connection to server
  29. UA_Client *client = UA_Client_new();
  30. UA_ClientConfig_setDefault(UA_Client_getConfig(client));
  31. UA_StatusCode retval = UA_Client_connect(client, cfg_OpcUa_ServerAddress); // Should be something like "opc.tcp://localhost:4840"
  32. if(retval != UA_STATUSCODE_GOOD) {
  33. UA_Client_delete(client);
  34. cerr << "Could not open OPC UA connection to " << cfg_OpcUa_ServerAddress << endl;
  35. return;
  36. }
  37. // TODO: maybe wait for a specific point in time to allow all threads to start before sending requests
  38. const UA_NodeId nodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER_SERVERSTATUS_CURRENTTIME);
  39. chrono::system_clock::time_point startTime = chrono::system_clock::now();
  40. unsigned long intervalUs = 1.0 / ((double)cfg_OpcUaReadsPerConnectionPerS) * 1000000.0;
  41. UA_Variant value;
  42. UA_DateTime raw_date;
  43. UA_DateTimeStruct dts;
  44. for(int i = 0; !stop; i++) // loop repeats every intervalUs microseconds
  45. {
  46. std::this_thread::sleep_until(startTime + chrono::microseconds(i * intervalUs));
  47. retval = UA_Client_readValueAttribute(client, nodeId, &value);
  48. if (cfg_EnableConsoleOutput) {
  49. raw_date = *(UA_DateTime *) value.data;
  50. dts = UA_DateTime_toStruct(raw_date);
  51. cout << "Thread " << threadId << ": " << dts.day << "-" << dts.month << "-" << dts.year << "-" << dts.hour << "-" << dts.min << "-" << dts.sec << endl;
  52. }
  53. if (retval != UA_STATUSCODE_GOOD) {
  54. UA_Client_delete(client);
  55. cerr << "Could not read node NS=" << nodeId.namespaceIndex << ",i=" << nodeId.identifier.numeric << " from OPC UA server" << endl;
  56. return;
  57. }
  58. }
  59. cout << "Thread " << threadId << " terminating" << endl;
  60. }
  61. int main(int argc, char **argv) {
  62. printf("This is the ua-stress-test\n");
  63. // Check if a config file is provided via input arguments or use config.cfg
  64. string configFilename = "config.cfg";
  65. if (argc < 2) {
  66. cout << "No configuration file specified, using " << configFilename << endl;
  67. }
  68. else {
  69. configFilename = argv[1]; // use the input argument as confiFilename
  70. }
  71. // Check if the configuration file exists
  72. if(-1 == access(configFilename.c_str(), 0)) {
  73. cerr << "Could not find configuration file %s" << endl;
  74. return EXIT_FAILURE;
  75. }
  76. // Load the configuration file
  77. config_t cfg;
  78. config_setting_t *setting;
  79. config_init(&cfg);
  80. if(! config_read_file(&cfg, configFilename.c_str())){
  81. cerr << "Error in the config file: " << config_error_file(&cfg) << ":" << config_error_line(&cfg) << " - " << config_error_text(&cfg) << endl;
  82. config_destroy(&cfg);
  83. return EXIT_FAILURE;
  84. }
  85. // Read relevant values from the configuration file
  86. bool configOk = true;
  87. configOk &= getIntFromConfig(&cfg, "NumberOfConnections", &cfg_NumberOfConnections);
  88. configOk &= getIntFromConfig(&cfg, "OpcUaReadsPerConnectionPerS", &cfg_OpcUaReadsPerConnectionPerS);
  89. configOk &= getBoolFromConfig(&cfg, "EnableConsoleOutput", &cfg_EnableConsoleOutput);
  90. configOk &= getStringFromConfig(&cfg, "OpcUa.ServerAddress", &cfg_OpcUa_ServerAddress);
  91. if(!configOk){
  92. config_destroy(&cfg);
  93. return EXIT_FAILURE;
  94. }
  95. // start a thread for each connection
  96. list<thread> threadList;
  97. for (int i = 0; i < cfg_NumberOfConnections; i++) { // loop repeats every intervalUs microseconds
  98. threadList.push_back(thread(workerThread));
  99. }
  100. cout << "Press ENTER to stop the stress test" << endl;
  101. getchar();
  102. cout << "Stopping..." << endl;
  103. // wait for threads to terminate
  104. stop = true;
  105. list<thread>::iterator it;
  106. for (it = threadList.begin(); it != threadList.end(); ++it){
  107. it->join();
  108. }
  109. return EXIT_SUCCESS;
  110. }