server_readspeed.c 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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, "Default Binary");
  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. UA_Byte *pos = request_msg.data;
  48. const UA_Byte *end = &request_msg.data[request_msg.length];
  49. retval |= UA_encodeBinary(&request, &UA_TYPES[UA_TYPES_READREQUEST], &pos, &end, NULL, NULL);
  50. clock_t begin, finish;
  51. begin = clock();
  52. UA_ReadRequest rq;
  53. UA_ReadResponse rr;
  54. for(int i = 0; i < 1000000; i++) {
  55. size_t offset = 0;
  56. retval |= UA_decodeBinary(&request_msg, &offset, &rq, &UA_TYPES[UA_TYPES_READREQUEST], 0, NULL);
  57. UA_ReadResponse_init(&rr);
  58. Service_Read(server, &adminSession, &rq, &rr);
  59. pos = request_msg.data;
  60. retval |= UA_encodeBinary(&rr, &UA_TYPES[UA_TYPES_READRESPONSE], &pos, &end, NULL, NULL);
  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 %i\n", retval);
  68. UA_ByteString_deleteMembers(&request_msg);
  69. UA_ByteString_deleteMembers(&response_msg);
  70. UA_Server_delete(server);
  71. return (int)retval;
  72. }