check_server_readspeed.c 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /* This work is licensed under a Creative Commons CCZero 1.0 Universal License.
  2. * See http://creativecommons.org/publicdomain/zero/1.0/ for more information. */
  3. /* This example is just to see how fast we can process messages. The server does
  4. not open a TCP port. */
  5. #include <time.h>
  6. #include <stdio.h>
  7. #include "ua_server.h"
  8. #include "ua_config_default.h"
  9. #include "server/ua_services.h"
  10. #include "ua_types_encoding_binary.h"
  11. int main(int argc, char** argv) {
  12. UA_ServerConfig *config = UA_ServerConfig_new_default();
  13. UA_Server *server = UA_Server_new(config);
  14. /* add a variable node to the address space */
  15. UA_VariableAttributes attr = UA_VariableAttributes_default;
  16. UA_Int32 myInteger = 42;
  17. UA_Variant_setScalar(&attr.value, &myInteger, &UA_TYPES[UA_TYPES_INT32]);
  18. attr.description = UA_LOCALIZEDTEXT("en-US","the answer");
  19. attr.displayName = UA_LOCALIZEDTEXT("en-US","the answer");
  20. UA_NodeId myIntegerNodeId = UA_NODEID_STRING(1, "the.answer");
  21. UA_QualifiedName myIntegerName = UA_QUALIFIEDNAME(1, "the answer");
  22. UA_NodeId parentNodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER);
  23. UA_NodeId parentReferenceNodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES);
  24. UA_StatusCode retval = UA_Server_addVariableNode(server, myIntegerNodeId, parentNodeId,
  25. parentReferenceNodeId, myIntegerName,
  26. UA_NODEID_NULL, attr, NULL, NULL);
  27. UA_assert(retval == UA_STATUSCODE_GOOD);
  28. UA_ReadRequest request;
  29. UA_ReadRequest_init(&request);
  30. UA_ReadValueId rvi;
  31. rvi.nodeId = myIntegerNodeId;
  32. rvi.attributeId = UA_ATTRIBUTEID_VALUE;
  33. rvi.indexRange = UA_STRING_NULL;
  34. rvi.dataEncoding = UA_QUALIFIEDNAME(0, "Default Binary");
  35. request.timestampsToReturn = UA_TIMESTAMPSTORETURN_NEITHER;
  36. request.nodesToReadSize = 1;
  37. request.nodesToRead = &rvi;
  38. UA_ByteString request_msg;
  39. retval |= UA_ByteString_allocBuffer(&request_msg, 1000);
  40. UA_ByteString response_msg;
  41. retval |= UA_ByteString_allocBuffer(&response_msg, 1000);
  42. UA_Byte *pos = request_msg.data;
  43. const UA_Byte *end = &request_msg.data[request_msg.length];
  44. retval |= UA_encodeBinary(&request, &UA_TYPES[UA_TYPES_READREQUEST], &pos, &end, NULL, NULL);
  45. UA_assert(retval == UA_STATUSCODE_GOOD);
  46. UA_Byte *resp_pos = response_msg.data;
  47. const UA_Byte *resp_end = &response_msg.data[response_msg.length];
  48. UA_ReadRequest rq;
  49. UA_ReadResponse rr;
  50. clock_t begin, finish;
  51. begin = clock();
  52. for(int i = 0; i < 1000000; i++) {
  53. size_t offset = 0;
  54. retval |= UA_decodeBinary(&request_msg, &offset, &rq, &UA_TYPES[UA_TYPES_READREQUEST], 0, NULL);
  55. UA_assert(retval == UA_STATUSCODE_GOOD);
  56. UA_ReadResponse_init(&rr);
  57. Service_Read(server, &adminSession, &rq, &rr);
  58. resp_pos = response_msg.data;
  59. retval |= UA_encodeBinary(&rr, &UA_TYPES[UA_TYPES_READRESPONSE], &resp_pos, &resp_end, NULL, NULL);
  60. UA_assert(retval == UA_STATUSCODE_GOOD);
  61. UA_ReadRequest_deleteMembers(&rq);
  62. UA_ReadResponse_deleteMembers(&rr);
  63. }
  64. finish = clock();
  65. double time_spent = (double)(finish - begin) / CLOCKS_PER_SEC;
  66. printf("duration was %f s\n", time_spent);
  67. printf("retval is %s\n", UA_StatusCode_name(retval));
  68. UA_ByteString_deleteMembers(&request_msg);
  69. UA_ByteString_deleteMembers(&response_msg);
  70. UA_Server_delete(server);
  71. UA_ServerConfig_delete(config);
  72. return (int)retval;
  73. }