check_server_readspeed.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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 <open62541/server_config_default.h>
  6. #include "server/ua_services.h"
  7. #include "ua_server_internal.h"
  8. #include "ua_types_encoding_binary.h"
  9. #include <check.h>
  10. #include <time.h>
  11. #include "testing_networklayers.h"
  12. #include "testing_policy.h"
  13. static UA_SecureChannel testChannel;
  14. static UA_SecurityPolicy dummyPolicy;
  15. static UA_Connection testingConnection;
  16. static funcs_called funcsCalled;
  17. static key_sizes keySizes;
  18. static UA_Server *server;
  19. static void setup(void) {
  20. server = UA_Server_new();
  21. UA_ServerConfig_setDefault(UA_Server_getConfig(server));
  22. TestingPolicy(&dummyPolicy, UA_BYTESTRING_NULL, &funcsCalled, &keySizes);
  23. UA_SecureChannel_init(&testChannel);
  24. UA_SecureChannel_setSecurityPolicy(&testChannel, &dummyPolicy, &UA_BYTESTRING_NULL);
  25. testingConnection = createDummyConnection(65535, NULL);
  26. UA_Connection_attachSecureChannel(&testingConnection, &testChannel);
  27. testChannel.connection = &testingConnection;
  28. }
  29. static void teardown(void) {
  30. UA_SecureChannel_close(&testChannel);
  31. UA_SecureChannel_deleteMembers(&testChannel);
  32. dummyPolicy.deleteMembers(&dummyPolicy);
  33. testingConnection.close(&testingConnection);
  34. UA_Server_delete(server);
  35. }
  36. START_TEST(readSpeed) {
  37. /* add a variable node to the address space */
  38. UA_VariableAttributes attr = UA_VariableAttributes_default;
  39. UA_Int32 myInteger = 42;
  40. UA_Variant_setScalar(&attr.value, &myInteger, &UA_TYPES[UA_TYPES_INT32]);
  41. attr.description = UA_LOCALIZEDTEXT("en-US","the answer");
  42. attr.displayName = UA_LOCALIZEDTEXT("en-US","the answer");
  43. UA_NodeId myIntegerNodeId = UA_NODEID_STRING(1, "the.answer");
  44. UA_QualifiedName myIntegerName = UA_QUALIFIEDNAME(1, "the answer");
  45. UA_NodeId parentNodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER);
  46. UA_NodeId parentReferenceNodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES);
  47. UA_StatusCode retval = UA_Server_addVariableNode(server, myIntegerNodeId, parentNodeId,
  48. parentReferenceNodeId, myIntegerName,
  49. UA_NODEID_NULL, attr, NULL, NULL);
  50. UA_assert(retval == UA_STATUSCODE_GOOD);
  51. UA_ReadRequest request;
  52. UA_ReadRequest_init(&request);
  53. UA_ReadValueId rvi;
  54. rvi.nodeId = myIntegerNodeId;
  55. rvi.attributeId = UA_ATTRIBUTEID_VALUE;
  56. rvi.indexRange = UA_STRING_NULL;
  57. rvi.dataEncoding = UA_QUALIFIEDNAME(0, "Default Binary");
  58. request.timestampsToReturn = UA_TIMESTAMPSTORETURN_NEITHER;
  59. request.nodesToReadSize = 1;
  60. request.nodesToRead = &rvi;
  61. UA_ByteString request_msg;
  62. retval |= UA_ByteString_allocBuffer(&request_msg, 1000);
  63. UA_ByteString response_msg;
  64. retval |= UA_ByteString_allocBuffer(&response_msg, 1000);
  65. UA_Byte *pos = request_msg.data;
  66. const UA_Byte *end = &request_msg.data[request_msg.length];
  67. retval |= UA_encodeBinary(&request, &UA_TYPES[UA_TYPES_READREQUEST], &pos, &end, NULL, NULL);
  68. ck_assert(retval == UA_STATUSCODE_GOOD);
  69. UA_ReadRequest req;
  70. UA_ReadResponse res;
  71. UA_ReadResponse_init(&res);
  72. clock_t begin, finish;
  73. begin = clock();
  74. for(size_t i = 0; i < 1000000; i++) {
  75. size_t offset = 0;
  76. retval |= UA_decodeBinary(&request_msg, &offset, &req, &UA_TYPES[UA_TYPES_READREQUEST], NULL);
  77. Service_Read(server, &server->adminSession, &req, &res);
  78. UA_Byte *rpos = response_msg.data;
  79. const UA_Byte *rend = &response_msg.data[response_msg.length];
  80. retval |= UA_encodeBinary(&res, &UA_TYPES[UA_TYPES_READRESPONSE],
  81. &rpos, &rend, NULL, NULL);
  82. UA_ReadRequest_deleteMembers(&req);
  83. UA_ReadResponse_deleteMembers(&res);
  84. }
  85. finish = clock();
  86. ck_assert(retval == UA_STATUSCODE_GOOD);
  87. double time_spent = (double)(finish - begin) / CLOCKS_PER_SEC;
  88. printf("duration was %f s\n", time_spent);
  89. printf("retval is %s\n", UA_StatusCode_name(retval));
  90. UA_ByteString_deleteMembers(&request_msg);
  91. UA_ByteString_deleteMembers(&response_msg);
  92. }
  93. END_TEST
  94. static Suite * service_speed_suite (void) {
  95. Suite *s = suite_create ("Service Speed");
  96. TCase* tc_read = tcase_create ("Read");
  97. tcase_add_checked_fixture(tc_read, setup, teardown);
  98. tcase_add_test (tc_read, readSpeed);
  99. suite_add_tcase (s, tc_read);
  100. return s;
  101. }
  102. int main (void) {
  103. int number_failed = 0;
  104. Suite *s = service_speed_suite();
  105. SRunner *sr = srunner_create(s);
  106. srunner_set_fork_status(sr,CK_NOFORK);
  107. srunner_run_all(sr, CK_NORMAL);
  108. number_failed += srunner_ntests_failed (sr);
  109. srunner_free(sr);
  110. return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
  111. }