server_readspeed.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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 "open62541.h"
  8. /* include guards to prevent double definitions with open62541.h */
  9. #define UA_TYPES_H_
  10. #define UA_SERVER_H_
  11. #define UA_CONNECTION_H_
  12. #define UA_TYPES_GENERATED_H_
  13. #include "server/ua_services.h"
  14. #include "ua_types_encoding_binary.h"
  15. int main(int argc, char** argv) {
  16. UA_ServerConfig config = UA_ServerConfig_standard;
  17. UA_Server *server = UA_Server_new(config);
  18. /* add a variable node to the address space */
  19. UA_VariableAttributes attr;
  20. UA_VariableAttributes_init(&attr);
  21. UA_Int32 myInteger = 42;
  22. UA_Variant_setScalar(&attr.value, &myInteger, &UA_TYPES[UA_TYPES_INT32]);
  23. attr.description = UA_LOCALIZEDTEXT("en_US","the answer");
  24. attr.displayName = UA_LOCALIZEDTEXT("en_US","the answer");
  25. UA_NodeId myIntegerNodeId = UA_NODEID_STRING(1, "the.answer");
  26. UA_QualifiedName myIntegerName = UA_QUALIFIEDNAME(1, "the answer");
  27. UA_NodeId parentNodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER);
  28. UA_NodeId parentReferenceNodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES);
  29. UA_Server_addVariableNode(server, myIntegerNodeId, parentNodeId,
  30. parentReferenceNodeId, myIntegerName,
  31. UA_NODEID_NULL, attr, NULL, NULL);
  32. UA_ReadRequest request;
  33. UA_ReadRequest_init(&request);
  34. UA_ReadValueId rvi;
  35. rvi.nodeId = myIntegerNodeId;
  36. rvi.attributeId = UA_ATTRIBUTEID_VALUE;
  37. rvi.indexRange = UA_STRING_NULL;
  38. rvi.dataEncoding = UA_QUALIFIEDNAME(0, "DefaultBinary");
  39. request.timestampsToReturn = UA_TIMESTAMPSTORETURN_NEITHER;
  40. request.nodesToReadSize = 1;
  41. request.nodesToRead = &rvi;
  42. UA_StatusCode retval = UA_STATUSCODE_GOOD;
  43. UA_ByteString request_msg;
  44. retval |= UA_ByteString_allocBuffer(&request_msg, 1000);
  45. UA_ByteString response_msg;
  46. retval |= UA_ByteString_allocBuffer(&response_msg, 1000);
  47. size_t offset = 0;
  48. retval |= UA_encodeBinary(&request, &UA_TYPES[UA_TYPES_READREQUEST], NULL, NULL, &request_msg, &offset);
  49. clock_t begin, end;
  50. begin = clock();
  51. UA_ReadRequest rq;
  52. UA_ReadResponse rr;
  53. for(int i = 0; i < 1000000; i++) {
  54. offset = 0;
  55. retval |= UA_decodeBinary(&request_msg, &offset, &rq, &UA_TYPES[UA_TYPES_READREQUEST], 0, NULL);
  56. UA_ReadResponse_init(&rr);
  57. Service_Read(server, &adminSession, &rq, &rr);
  58. offset = 0;
  59. retval |= UA_encodeBinary(&rr, &UA_TYPES[UA_TYPES_READRESPONSE], NULL, NULL, &response_msg, &offset);
  60. UA_ReadRequest_deleteMembers(&rq);
  61. UA_ReadResponse_deleteMembers(&rr);
  62. }
  63. end = clock();
  64. double time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
  65. printf("duration was %f s\n", time_spent);
  66. printf("retval is %i\n", retval);
  67. UA_ByteString_deleteMembers(&request_msg);
  68. UA_ByteString_deleteMembers(&response_msg);
  69. UA_Server_delete(server);
  70. return (int)retval;
  71. }