check_server_readspeed.c 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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_standard.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_Server_addVariableNode(server, myIntegerNodeId, parentNodeId,
  25. parentReferenceNodeId, myIntegerName,
  26. UA_NODEID_NULL, attr, NULL, NULL);
  27. UA_ReadRequest request;
  28. UA_ReadRequest_init(&request);
  29. UA_ReadValueId rvi;
  30. rvi.nodeId = myIntegerNodeId;
  31. rvi.attributeId = UA_ATTRIBUTEID_VALUE;
  32. rvi.indexRange = UA_STRING_NULL;
  33. rvi.dataEncoding = UA_QUALIFIEDNAME(0, "Default Binary");
  34. request.timestampsToReturn = UA_TIMESTAMPSTORETURN_NEITHER;
  35. request.nodesToReadSize = 1;
  36. request.nodesToRead = &rvi;
  37. UA_StatusCode retval = UA_STATUSCODE_GOOD;
  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. clock_t begin, finish;
  46. begin = clock();
  47. UA_ReadRequest rq;
  48. UA_ReadResponse rr;
  49. for(int i = 0; i < 1000000; i++) {
  50. size_t offset = 0;
  51. retval |= UA_decodeBinary(&request_msg, &offset, &rq, &UA_TYPES[UA_TYPES_READREQUEST], 0, NULL);
  52. UA_ReadResponse_init(&rr);
  53. Service_Read(server, &adminSession, &rq, &rr);
  54. pos = request_msg.data;
  55. retval |= UA_encodeBinary(&rr, &UA_TYPES[UA_TYPES_READRESPONSE], &pos, &end, NULL, NULL);
  56. UA_ReadRequest_deleteMembers(&rq);
  57. UA_ReadResponse_deleteMembers(&rr);
  58. }
  59. finish = clock();
  60. double time_spent = (double)(finish - begin) / CLOCKS_PER_SEC;
  61. printf("duration was %f s\n", time_spent);
  62. printf("retval is %i\n", retval);
  63. UA_ByteString_deleteMembers(&request_msg);
  64. UA_ByteString_deleteMembers(&response_msg);
  65. UA_Server_delete(server);
  66. UA_ServerConfig_delete(config);
  67. return (int)retval;
  68. }