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 <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. UA_assert(retval == UA_STATUSCODE_GOOD);
  69. UA_ReadRequest rq;
  70. UA_MessageContext mc;
  71. UA_ResponseHeader rh;
  72. UA_ResponseHeader_init(&rh);
  73. clock_t begin, finish;
  74. begin = clock();
  75. for(size_t i = 0; i < 1000000; i++) {
  76. size_t offset = 0;
  77. retval |= UA_decodeBinary(&request_msg, &offset, &rq, &UA_TYPES[UA_TYPES_READREQUEST], NULL);
  78. UA_MessageContext_begin(&mc, &testChannel, 0, UA_MESSAGETYPE_MSG);
  79. retval |= Service_Read(server, &server->adminSession, &mc, &rq, &rh);
  80. UA_MessageContext_finish(&mc);
  81. UA_ReadRequest_deleteMembers(&rq);
  82. }
  83. finish = clock();
  84. UA_assert(retval == UA_STATUSCODE_GOOD);
  85. double time_spent = (double)(finish - begin) / CLOCKS_PER_SEC;
  86. printf("duration was %f s\n", time_spent);
  87. printf("retval is %s\n", UA_StatusCode_name(retval));
  88. UA_ByteString_deleteMembers(&request_msg);
  89. UA_ByteString_deleteMembers(&response_msg);
  90. }
  91. END_TEST
  92. static Suite * service_speed_suite (void) {
  93. Suite *s = suite_create ("Service Speed");
  94. TCase* tc_read = tcase_create ("Read");
  95. tcase_add_checked_fixture(tc_read, setup, teardown);
  96. tcase_add_test (tc_read, readSpeed);
  97. suite_add_tcase (s, tc_read);
  98. return s;
  99. }
  100. int main (void) {
  101. int number_failed = 0;
  102. Suite *s = service_speed_suite();
  103. SRunner *sr = srunner_create(s);
  104. srunner_set_fork_status(sr,CK_NOFORK);
  105. srunner_run_all(sr, CK_NORMAL);
  106. number_failed += srunner_ntests_failed (sr);
  107. srunner_free(sr);
  108. return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
  109. }