check_server_readspeed.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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 <open62541/plugin/nodestore_default.h>
  7. #include "server/ua_services.h"
  8. #include "ua_server_internal.h"
  9. #include "ua_types_encoding_binary.h"
  10. #include <check.h>
  11. #include <time.h>
  12. #include "testing_networklayers.h"
  13. #include "testing_policy.h"
  14. #define READNODES 10000 /* Number of nodes to be created for reading */
  15. #define READS 1000000 /* Number of reads to perform */
  16. static UA_Server *server;
  17. static UA_NodeId readNodeIds[READNODES];
  18. static void setup(void) {
  19. UA_ServerConfig config;
  20. memset(&config, 0, sizeof(UA_ServerConfig));
  21. UA_Nodestore_HashMap(&config.nodestore);
  22. server = UA_Server_newWithConfig(&config);
  23. }
  24. static void teardown(void) {
  25. UA_Server_delete(server);
  26. }
  27. START_TEST(readSpeed) {
  28. UA_StatusCode retval = UA_STATUSCODE_GOOD;
  29. /* Add variable nodes to the address space */
  30. UA_VariableAttributes attr = UA_VariableAttributes_default;
  31. UA_Int32 myInteger = 42;
  32. UA_Variant_setScalar(&attr.value, &myInteger, &UA_TYPES[UA_TYPES_INT32]);
  33. UA_NodeId parentNodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER);
  34. UA_NodeId parentReferenceNodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES);
  35. for(size_t i = 0; i < READNODES; i++) {
  36. char varName[20];
  37. UA_snprintf(varName, 20, "Variable %u", (UA_UInt32)i);
  38. UA_NodeId myNodeId = UA_NODEID_STRING(1, varName);
  39. UA_QualifiedName myName = UA_QUALIFIEDNAME(1, varName);
  40. retval = UA_Server_addVariableNode(server, myNodeId, parentNodeId,
  41. parentReferenceNodeId, myName,
  42. UA_NODEID_NULL, attr, NULL,
  43. &readNodeIds[i]);
  44. UA_assert(retval == UA_STATUSCODE_GOOD);
  45. }
  46. UA_ByteString request_msg;
  47. retval |= UA_ByteString_allocBuffer(&request_msg, 1000);
  48. UA_ByteString response_msg;
  49. retval |= UA_ByteString_allocBuffer(&response_msg, 1000);
  50. UA_assert(retval == UA_STATUSCODE_GOOD);
  51. UA_ReadRequest request;
  52. UA_ReadRequest_init(&request);
  53. UA_ReadValueId rvi;
  54. rvi.attributeId = UA_ATTRIBUTEID_VALUE;
  55. rvi.indexRange = UA_STRING_NULL;
  56. rvi.dataEncoding = UA_QUALIFIEDNAME(0, "Default Binary");
  57. request.timestampsToReturn = UA_TIMESTAMPSTORETURN_NEITHER;
  58. request.nodesToReadSize = 1;
  59. request.nodesToRead = &rvi;
  60. UA_ReadResponse res;
  61. UA_ReadResponse_init(&res);
  62. clock_t begin, finish;
  63. begin = clock();
  64. for(size_t i = 0; i < READS ; i++) {
  65. /* Set the NodeId */
  66. rvi.nodeId = readNodeIds[i % READNODES];
  67. UA_LOCK(server->serviceMutex);
  68. Service_Read(server, &server->adminSession, &request, &res);
  69. UA_UNLOCK(server->serviceMutex);
  70. UA_ReadResponse_deleteMembers(&res);
  71. }
  72. finish = clock();
  73. ck_assert(retval == UA_STATUSCODE_GOOD);
  74. double time_spent = (double)(finish - begin) / CLOCKS_PER_SEC;
  75. printf("duration was %f s\n", time_spent);
  76. printf("retval is %s\n", UA_StatusCode_name(retval));
  77. UA_ByteString_deleteMembers(&request_msg);
  78. UA_ByteString_deleteMembers(&response_msg);
  79. for(size_t i = 0; i < READNODES; i++)
  80. UA_NodeId_clear(&readNodeIds[i]);
  81. }
  82. END_TEST
  83. START_TEST(readSpeedWithEncoding) {
  84. UA_StatusCode retval = UA_STATUSCODE_GOOD;
  85. /* Add variable nodes to the address space */
  86. UA_VariableAttributes attr = UA_VariableAttributes_default;
  87. UA_Int32 myInteger = 42;
  88. UA_Variant_setScalar(&attr.value, &myInteger, &UA_TYPES[UA_TYPES_INT32]);
  89. UA_NodeId parentNodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER);
  90. UA_NodeId parentReferenceNodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES);
  91. for(size_t i = 0; i < READNODES; i++) {
  92. char varName[20];
  93. UA_snprintf(varName, 20, "Variable %u", (UA_UInt32)i);
  94. UA_NodeId myNodeId = UA_NODEID_STRING(1, varName);
  95. UA_QualifiedName myName = UA_QUALIFIEDNAME(1, varName);
  96. retval = UA_Server_addVariableNode(server, myNodeId, parentNodeId,
  97. parentReferenceNodeId, myName,
  98. UA_NODEID_NULL, attr, NULL,
  99. &readNodeIds[i]);
  100. UA_assert(retval == UA_STATUSCODE_GOOD);
  101. }
  102. UA_ByteString request_msg;
  103. retval |= UA_ByteString_allocBuffer(&request_msg, 1000);
  104. UA_ByteString response_msg;
  105. retval |= UA_ByteString_allocBuffer(&response_msg, 1000);
  106. UA_assert(retval == UA_STATUSCODE_GOOD);
  107. UA_ReadRequest request;
  108. UA_ReadRequest_init(&request);
  109. UA_ReadValueId rvi;
  110. rvi.attributeId = UA_ATTRIBUTEID_VALUE;
  111. rvi.indexRange = UA_STRING_NULL;
  112. rvi.dataEncoding = UA_QUALIFIEDNAME(0, "Default Binary");
  113. request.timestampsToReturn = UA_TIMESTAMPSTORETURN_NEITHER;
  114. request.nodesToReadSize = 1;
  115. request.nodesToRead = &rvi;
  116. UA_ReadRequest req;
  117. UA_ReadResponse res;
  118. UA_ReadResponse_init(&res);
  119. clock_t begin, finish;
  120. begin = clock();
  121. for(size_t i = 0; i < READS; i++) {
  122. /* Set the NodeId */
  123. rvi.nodeId = readNodeIds[i % READNODES];
  124. /* Encode the request */
  125. UA_Byte *pos = request_msg.data;
  126. const UA_Byte *end = &request_msg.data[request_msg.length];
  127. retval |= UA_encodeBinary(&request, &UA_TYPES[UA_TYPES_READREQUEST], &pos, &end, NULL, NULL);
  128. ck_assert(retval == UA_STATUSCODE_GOOD);
  129. /* Decode the request */
  130. size_t offset = 0;
  131. retval |= UA_decodeBinary(&request_msg, &offset, &req, &UA_TYPES[UA_TYPES_READREQUEST], NULL);
  132. UA_LOCK(server->serviceMutex);
  133. Service_Read(server, &server->adminSession, &req, &res);
  134. UA_UNLOCK(server->serviceMutex);
  135. UA_Byte *rpos = response_msg.data;
  136. const UA_Byte *rend = &response_msg.data[response_msg.length];
  137. retval |= UA_encodeBinary(&res, &UA_TYPES[UA_TYPES_READRESPONSE],
  138. &rpos, &rend, NULL, NULL);
  139. UA_ReadRequest_deleteMembers(&req);
  140. UA_ReadResponse_deleteMembers(&res);
  141. }
  142. finish = clock();
  143. ck_assert(retval == UA_STATUSCODE_GOOD);
  144. double time_spent = (double)(finish - begin) / CLOCKS_PER_SEC;
  145. printf("duration with encoding was %f s\n", time_spent);
  146. printf("retval is %s\n", UA_StatusCode_name(retval));
  147. UA_ByteString_deleteMembers(&request_msg);
  148. UA_ByteString_deleteMembers(&response_msg);
  149. for(size_t i = 0; i < READNODES; i++)
  150. UA_NodeId_clear(&readNodeIds[i]);
  151. }
  152. END_TEST
  153. static Suite * service_speed_suite (void) {
  154. Suite *s = suite_create ("Service Speed");
  155. TCase* tc_read = tcase_create ("Read");
  156. tcase_add_checked_fixture(tc_read, setup, teardown);
  157. tcase_add_test (tc_read, readSpeed);
  158. tcase_add_test (tc_read, readSpeedWithEncoding);
  159. suite_add_tcase (s, tc_read);
  160. return s;
  161. }
  162. int main (void) {
  163. int number_failed = 0;
  164. Suite *s = service_speed_suite();
  165. SRunner *sr = srunner_create(s);
  166. srunner_set_fork_status(sr,CK_NOFORK);
  167. srunner_run_all(sr, CK_NORMAL);
  168. number_failed += srunner_ntests_failed (sr);
  169. srunner_free(sr);
  170. return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
  171. }