server_readspeed.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*
  2. * This work is licensed under a Creative Commons CCZero 1.0 Universal License.
  3. * See http://creativecommons.org/publicdomain/zero/1.0/ for more information.
  4. */
  5. #include <signal.h>
  6. #ifdef UA_NO_AMALGAMATION
  7. # include "ua_types.h"
  8. # include "ua_types_generated.h"
  9. # include "ua_server.h"
  10. # include "logger_stdout.h"
  11. # include "networklayer_tcp.h"
  12. #else
  13. # include "open62541.h"
  14. #endif
  15. #include <time.h>
  16. #include <stdio.h>
  17. #include "server/ua_services.h"
  18. #include "ua_types_encoding_binary.h"
  19. UA_Boolean running = true;
  20. UA_Logger logger = Logger_Stdout;
  21. static void stopHandler(int sign) {
  22. UA_LOG_INFO(logger, UA_LOGCATEGORY_SERVER, "received ctrl-c");
  23. running = false;
  24. }
  25. int main(int argc, char** argv) {
  26. signal(SIGINT, stopHandler); /* catches ctrl-c */
  27. UA_ServerConfig config = UA_ServerConfig_standard;
  28. UA_ServerNetworkLayer nl = UA_ServerNetworkLayerTCP(UA_ConnectionConfig_standard, 16664, logger);
  29. config.logger = Logger_Stdout;
  30. config.networkLayers = &nl;
  31. config.networkLayersSize = 1;
  32. UA_Server *server = UA_Server_new(config);
  33. /* add a variable node to the address space */
  34. UA_VariableAttributes attr;
  35. UA_VariableAttributes_init(&attr);
  36. UA_Int32 myInteger = 42;
  37. UA_Variant_setScalar(&attr.value, &myInteger, &UA_TYPES[UA_TYPES_INT32]);
  38. attr.description = UA_LOCALIZEDTEXT("en_US","the answer");
  39. attr.displayName = UA_LOCALIZEDTEXT("en_US","the answer");
  40. UA_NodeId myIntegerNodeId = UA_NODEID_STRING(1, "the.answer");
  41. UA_QualifiedName myIntegerName = UA_QUALIFIEDNAME(1, "the answer");
  42. UA_NodeId parentNodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER);
  43. UA_NodeId parentReferenceNodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES);
  44. UA_Server_addVariableNode(server, myIntegerNodeId, parentNodeId,
  45. parentReferenceNodeId, myIntegerName,
  46. UA_NODEID_NULL, attr, NULL, NULL);
  47. UA_ReadRequest request;
  48. UA_ReadRequest_init(&request);
  49. UA_ReadValueId rvi;
  50. rvi.nodeId = myIntegerNodeId;
  51. rvi.attributeId = UA_ATTRIBUTEID_VALUE;
  52. rvi.indexRange = UA_STRING_NULL;
  53. rvi.dataEncoding = UA_QUALIFIEDNAME(0, "DefaultBinary");
  54. request.timestampsToReturn = UA_TIMESTAMPSTORETURN_NEITHER;
  55. request.nodesToReadSize = 1;
  56. request.nodesToRead = &rvi;
  57. UA_StatusCode retval = UA_STATUSCODE_GOOD;
  58. UA_ByteString request_msg;
  59. retval |= UA_ByteString_allocBuffer(&request_msg, 1000);
  60. UA_ByteString response_msg;
  61. retval |= UA_ByteString_allocBuffer(&response_msg, 1000);
  62. size_t offset = 0;
  63. retval |= UA_encodeBinary(&request, &UA_TYPES[UA_TYPES_READREQUEST], &request_msg, &offset);
  64. clock_t begin, end;
  65. begin = clock();
  66. UA_ReadRequest rq;
  67. UA_ReadResponse rr;
  68. for(int i = 0; i < 900000; i++) {
  69. offset = 0;
  70. retval |= UA_decodeBinary(&request_msg, &offset, &rq, &UA_TYPES[UA_TYPES_READREQUEST]);
  71. UA_ReadResponse_init(&rr);
  72. Service_Read(server, &adminSession, &rq, &rr);
  73. offset = 0;
  74. retval |= UA_encodeBinary(&rr, &UA_TYPES[UA_TYPES_READRESPONSE], &response_msg, &offset);
  75. UA_ReadRequest_deleteMembers(&rq);
  76. UA_ReadResponse_deleteMembers(&rr);
  77. }
  78. end = clock();
  79. double time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
  80. printf("duration was %f s\n", time_spent);
  81. printf("retval is %i\n", retval);
  82. UA_ByteString_deleteMembers(&request_msg);
  83. UA_ByteString_deleteMembers(&response_msg);
  84. retval |= UA_Server_run(server, &running);
  85. UA_Server_delete(server);
  86. nl.deleteMembers(&nl);
  87. return (int)retval;
  88. }