server_readspeed.c 3.3 KB

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