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_standard;
  13. UA_Server *server = UA_Server_new(config);
  14. /* add a variable node to the address space */
  15. UA_VariableAttributes attr;
  16. UA_VariableAttributes_init(&attr);
  17. UA_Int32 myInteger = 42;
  18. UA_Variant_setScalar(&attr.value, &myInteger, &UA_TYPES[UA_TYPES_INT32]);
  19. attr.description = UA_LOCALIZEDTEXT("en_US","the answer");
  20. attr.displayName = UA_LOCALIZEDTEXT("en_US","the answer");
  21. UA_NodeId myIntegerNodeId = UA_NODEID_STRING(1, "the.answer");
  22. UA_QualifiedName myIntegerName = UA_QUALIFIEDNAME(1, "the answer");
  23. UA_NodeId parentNodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER);
  24. UA_NodeId parentReferenceNodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES);
  25. UA_Server_addVariableNode(server, myIntegerNodeId, parentNodeId,
  26. parentReferenceNodeId, myIntegerName,
  27. UA_NODEID_NULL, attr, NULL, NULL);
  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_StatusCode retval = UA_STATUSCODE_GOOD;
  39. UA_ByteString request_msg;
  40. retval |= UA_ByteString_allocBuffer(&request_msg, 1000);
  41. UA_ByteString response_msg;
  42. retval |= UA_ByteString_allocBuffer(&response_msg, 1000);
  43. UA_Byte *pos = request_msg.data;
  44. const UA_Byte *end = &request_msg.data[request_msg.length];
  45. retval |= UA_encodeBinary(&request, &UA_TYPES[UA_TYPES_READREQUEST], &pos, &end, NULL, NULL);
  46. clock_t begin, finish;
  47. begin = clock();
  48. UA_ReadRequest rq;
  49. UA_ReadResponse rr;
  50. for(int i = 0; i < 1000000; i++) {
  51. size_t offset = 0;
  52. retval |= UA_decodeBinary(&request_msg, &offset, &rq, &UA_TYPES[UA_TYPES_READREQUEST], 0, NULL);
  53. UA_ReadResponse_init(&rr);
  54. Service_Read(server, &adminSession, &rq, &rr);
  55. pos = request_msg.data;
  56. retval |= UA_encodeBinary(&rr, &UA_TYPES[UA_TYPES_READRESPONSE], &pos, &end, NULL, NULL);
  57. UA_ReadRequest_deleteMembers(&rq);
  58. UA_ReadResponse_deleteMembers(&rr);
  59. }
  60. finish = clock();
  61. double time_spent = (double)(finish - begin) / CLOCKS_PER_SEC;
  62. printf("duration was %f s\n", time_spent);
  63. printf("retval is %i\n", retval);
  64. UA_ByteString_deleteMembers(&request_msg);
  65. UA_ByteString_deleteMembers(&response_msg);
  66. UA_Server_delete(server);
  67. return (int)retval;
  68. }