check_server_readspeed.c 4.7 KB

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