server_readspeed.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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_server.h"
  9. # include "logger_stdout.h"
  10. # include "networklayer_tcp.h"
  11. #else
  12. # include "open62541.h"
  13. #endif
  14. #include <time.h>
  15. #include "ua_types_generated.h"
  16. #include "server/ua_services.h"
  17. #include "ua_types_encoding_binary.h"
  18. UA_Boolean running = 1;
  19. UA_Logger logger;
  20. static void stopHandler(int sign) {
  21. UA_LOG_INFO(logger, UA_LOGCATEGORY_SERVER, "received ctrl-c");
  22. running = 0;
  23. }
  24. int main(int argc, char** argv) {
  25. signal(SIGINT, stopHandler); /* catches ctrl-c */
  26. UA_Server *server = UA_Server_new(UA_ServerConfig_standard);
  27. logger = Logger_Stdout_new();
  28. UA_Server_setLogger(server, logger);
  29. UA_Server_addNetworkLayer(server, ServerNetworkLayerTCP_new(UA_ConnectionConfig_standard, 16664));
  30. /* add a variable node to the address space */
  31. UA_Variant *myIntegerVariant = UA_Variant_new();
  32. UA_Int32 myInteger = 42;
  33. UA_Variant_setScalarCopy(myIntegerVariant, &myInteger, &UA_TYPES[UA_TYPES_INT32]);
  34. //NOTE: the link between myInteger and the value of the node is lost here, you can safely reuse myInteger
  35. UA_QualifiedName myIntegerName = UA_QUALIFIEDNAME(1, "the answer");
  36. UA_NodeId myIntegerNodeId = UA_NODEID_STRING(1, "the.answer"); /* UA_NODEID_NULL would assign a random free nodeid */
  37. UA_LocalizedText myIntegerBrowseName = UA_LOCALIZEDTEXT("en_US","the answer");
  38. UA_NodeId parentNodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER);
  39. UA_NodeId parentReferenceNodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES);
  40. UA_Server_addVariableNode(server, myIntegerNodeId, myIntegerName, myIntegerBrowseName, myIntegerBrowseName, 0, 0,
  41. parentNodeId, parentReferenceNodeId, myIntegerVariant, NULL);
  42. UA_ReadRequest request;
  43. UA_ReadRequest_init(&request);
  44. UA_ReadValueId rvi;
  45. rvi.nodeId = myIntegerNodeId;
  46. rvi.attributeId = UA_ATTRIBUTEID_VALUE;
  47. rvi.indexRange = UA_STRING_NULL;
  48. rvi.dataEncoding = UA_QUALIFIEDNAME(0, "DefaultBinary");
  49. request.timestampsToReturn = UA_TIMESTAMPSTORETURN_NEITHER;
  50. request.nodesToReadSize = 1;
  51. request.nodesToRead = &rvi;
  52. UA_ByteString request_msg;
  53. UA_ByteString response_msg;
  54. UA_ByteString_newMembers(&request_msg, 1000);
  55. UA_ByteString_newMembers(&response_msg, 1000);
  56. size_t offset = 0;
  57. UA_encodeBinary(&request, &UA_TYPES[UA_TYPES_READREQUEST], &request_msg, &offset);
  58. clock_t begin, end;
  59. begin = clock();
  60. UA_ReadRequest rq;
  61. UA_ReadResponse rr;
  62. for(int i = 0; i < 600000; i++) {
  63. UA_ReadRequest_init(&rq);
  64. UA_ReadResponse_init(&rr);
  65. offset = 0;
  66. UA_decodeBinary(&request_msg, &offset, &rq, &UA_TYPES[UA_TYPES_READREQUEST]);
  67. Service_Read(server, &adminSession, &rq, &rr);
  68. offset = 0;
  69. UA_encodeBinary(&rr, &UA_TYPES[UA_TYPES_READRESPONSE], &response_msg, &offset);
  70. UA_ReadRequest_deleteMembers(&rq);
  71. UA_ReadResponse_deleteMembers(&rr);
  72. }
  73. end = clock();
  74. double time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
  75. printf("duration was %f s\n", time_spent);
  76. UA_ByteString_deleteMembers(&request_msg);
  77. UA_ByteString_deleteMembers(&response_msg);
  78. UA_StatusCode retval = UA_Server_run(server, 1, &running);
  79. UA_Server_delete(server);
  80. return retval;
  81. }