check_server_readspeed.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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_close(&testChannel);
  34. UA_SecureChannel_deleteMembers(&testChannel);
  35. dummyPolicy.deleteMembers(&dummyPolicy);
  36. testingConnection.close(&testingConnection);
  37. UA_Server_delete(server);
  38. UA_ServerConfig_delete(config);
  39. }
  40. START_TEST(readSpeed) {
  41. /* add a variable node to the address space */
  42. UA_VariableAttributes attr = UA_VariableAttributes_default;
  43. UA_Int32 myInteger = 42;
  44. UA_Variant_setScalar(&attr.value, &myInteger, &UA_TYPES[UA_TYPES_INT32]);
  45. attr.description = UA_LOCALIZEDTEXT("en-US","the answer");
  46. attr.displayName = UA_LOCALIZEDTEXT("en-US","the answer");
  47. UA_NodeId myIntegerNodeId = UA_NODEID_STRING(1, "the.answer");
  48. UA_QualifiedName myIntegerName = UA_QUALIFIEDNAME(1, "the answer");
  49. UA_NodeId parentNodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER);
  50. UA_NodeId parentReferenceNodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES);
  51. UA_StatusCode retval = UA_Server_addVariableNode(server, myIntegerNodeId, parentNodeId,
  52. parentReferenceNodeId, myIntegerName,
  53. UA_NODEID_NULL, attr, NULL, NULL);
  54. UA_assert(retval == UA_STATUSCODE_GOOD);
  55. UA_ReadRequest request;
  56. UA_ReadRequest_init(&request);
  57. UA_ReadValueId rvi;
  58. rvi.nodeId = myIntegerNodeId;
  59. rvi.attributeId = UA_ATTRIBUTEID_VALUE;
  60. rvi.indexRange = UA_STRING_NULL;
  61. rvi.dataEncoding = UA_QUALIFIEDNAME(0, "Default Binary");
  62. request.timestampsToReturn = UA_TIMESTAMPSTORETURN_NEITHER;
  63. request.nodesToReadSize = 1;
  64. request.nodesToRead = &rvi;
  65. UA_ByteString request_msg;
  66. retval |= UA_ByteString_allocBuffer(&request_msg, 1000);
  67. UA_ByteString response_msg;
  68. retval |= UA_ByteString_allocBuffer(&response_msg, 1000);
  69. UA_Byte *pos = request_msg.data;
  70. const UA_Byte *end = &request_msg.data[request_msg.length];
  71. retval |= UA_encodeBinary(&request, &UA_TYPES[UA_TYPES_READREQUEST], &pos, &end, NULL, NULL);
  72. UA_assert(retval == UA_STATUSCODE_GOOD);
  73. UA_ReadRequest rq;
  74. UA_MessageContext mc;
  75. UA_ResponseHeader rh;
  76. UA_ResponseHeader_init(&rh);
  77. clock_t begin, finish;
  78. begin = clock();
  79. for(size_t i = 0; i < 1000000; i++) {
  80. size_t offset = 0;
  81. retval |= UA_decodeBinary(&request_msg, &offset, &rq, &UA_TYPES[UA_TYPES_READREQUEST], NULL);
  82. UA_MessageContext_begin(&mc, &testChannel, 0, UA_MESSAGETYPE_MSG);
  83. retval |= Service_Read(server, &server->adminSession, &mc, &rq, &rh);
  84. UA_MessageContext_finish(&mc);
  85. UA_ReadRequest_deleteMembers(&rq);
  86. }
  87. finish = clock();
  88. UA_assert(retval == UA_STATUSCODE_GOOD);
  89. double time_spent = (double)(finish - begin) / CLOCKS_PER_SEC;
  90. printf("duration was %f s\n", time_spent);
  91. printf("retval is %s\n", UA_StatusCode_name(retval));
  92. UA_ByteString_deleteMembers(&request_msg);
  93. UA_ByteString_deleteMembers(&response_msg);
  94. }
  95. END_TEST
  96. static Suite * service_speed_suite (void) {
  97. Suite *s = suite_create ("Service Speed");
  98. TCase* tc_read = tcase_create ("Read");
  99. tcase_add_checked_fixture(tc_read, setup, teardown);
  100. tcase_add_test (tc_read, readSpeed);
  101. suite_add_tcase (s, tc_read);
  102. return s;
  103. }
  104. int main (void) {
  105. int number_failed = 0;
  106. Suite *s = service_speed_suite();
  107. SRunner *sr = srunner_create(s);
  108. srunner_set_fork_status(sr,CK_NOFORK);
  109. srunner_run_all(sr, CK_NORMAL);
  110. number_failed += srunner_ntests_failed (sr);
  111. srunner_free(sr);
  112. return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
  113. }