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