main.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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_ServerEndpoint;
  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_ServerEndpoint); // 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_ServerEndpoint << 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. UA_Client_disconnect(client);
  60. cout << "Thread " << threadId << " terminating" << endl;
  61. }
  62. int main(int argc, char **argv) {
  63. printf("This is the ua-stress-test\n");
  64. // Check if a config file is provided via input arguments or use config.cfg
  65. string configFilename = "config.cfg";
  66. if (argc < 2) {
  67. cout << "No configuration file specified, using " << configFilename << endl;
  68. }
  69. else {
  70. configFilename = argv[1]; // use the input argument as confiFilename
  71. }
  72. // Check if the configuration file exists
  73. if(-1 == access(configFilename.c_str(), 0)) {
  74. cerr << "Could not find configuration file %s" << endl;
  75. return EXIT_FAILURE;
  76. }
  77. // Load the configuration file
  78. config_t cfg;
  79. config_setting_t *setting;
  80. config_init(&cfg);
  81. if(! config_read_file(&cfg, configFilename.c_str())){
  82. cerr << "Error in the config file: " << config_error_file(&cfg) << ":" << config_error_line(&cfg) << " - " << config_error_text(&cfg) << endl;
  83. config_destroy(&cfg);
  84. return EXIT_FAILURE;
  85. }
  86. // Read relevant values from the configuration file
  87. bool configOk = true;
  88. configOk &= getIntFromConfig(&cfg, "NumberOfConnections", &cfg_NumberOfConnections);
  89. configOk &= getIntFromConfig(&cfg, "OpcUaReadsPerConnectionPerS", &cfg_OpcUaReadsPerConnectionPerS);
  90. configOk &= getBoolFromConfig(&cfg, "EnableConsoleOutput", &cfg_EnableConsoleOutput);
  91. configOk &= getStringFromConfig(&cfg, "OpcUa.ServerEndpoint", &cfg_OpcUa_ServerEndpoint);
  92. if(!configOk){
  93. config_destroy(&cfg);
  94. return EXIT_FAILURE;
  95. }
  96. // start a thread for each connection
  97. list<thread> threadList;
  98. for (int i = 0; i < cfg_NumberOfConnections; i++) { // loop repeats every intervalUs microseconds
  99. threadList.push_back(thread(workerThread));
  100. }
  101. cout << "Press ENTER to stop the stress test" << endl;
  102. getchar();
  103. cout << "Stopping..." << endl;
  104. // wait for threads to terminate
  105. stop = true;
  106. list<thread>::iterator it;
  107. for (it = threadList.begin(); it != threadList.end(); ++it){
  108. it->join();
  109. }
  110. return EXIT_SUCCESS;
  111. }