check_server_monitoringspeed.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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 monitor value changes. 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_subscription.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);
  26. UA_SecureChannel_setSecurityPolicy(&testChannel, &dummyPolicy, &UA_BYTESTRING_NULL);
  27. testingConnection = createDummyConnection(65535, NULL);
  28. UA_Connection_attachSecureChannel(&testingConnection, &testChannel);
  29. testChannel.connection = &testingConnection;
  30. }
  31. static void teardown(void) {
  32. UA_SecureChannel_close(&testChannel);
  33. UA_SecureChannel_deleteMembers(&testChannel);
  34. dummyPolicy.deleteMembers(&dummyPolicy);
  35. testingConnection.close(&testingConnection);
  36. UA_Server_delete(server);
  37. UA_ServerConfig_delete(config);
  38. }
  39. static size_t callbackCount = 0;
  40. static void
  41. dataChangeNotificationCallback(UA_Server *s, UA_UInt32 monitoredItemId,
  42. void *monitoredItemContext, const UA_NodeId *nodeId,
  43. void *nodeContext, UA_UInt32 attributeId,
  44. const UA_DataValue *value) {
  45. callbackCount++;
  46. }
  47. START_TEST(monitorIntegerNoChanges) {
  48. /* add a variable node to the address space */
  49. UA_VariableAttributes attr = UA_VariableAttributes_default;
  50. UA_Int32 myInteger = 42;
  51. UA_Variant_setScalar(&attr.value, &myInteger, &UA_TYPES[UA_TYPES_INT32]);
  52. attr.description = UA_LOCALIZEDTEXT("en-US","the answer");
  53. attr.displayName = UA_LOCALIZEDTEXT("en-US","the answer");
  54. UA_NodeId myIntegerNodeId = UA_NODEID_STRING(1, "the.answer");
  55. UA_QualifiedName myIntegerName = UA_QUALIFIEDNAME(1, "the answer");
  56. UA_NodeId parentNodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER);
  57. UA_NodeId parentReferenceNodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES);
  58. UA_StatusCode retval = UA_Server_addVariableNode(server, myIntegerNodeId, parentNodeId,
  59. parentReferenceNodeId, myIntegerName,
  60. UA_NODEID_NULL, attr, NULL, NULL);
  61. UA_assert(retval == UA_STATUSCODE_GOOD);
  62. UA_MonitoredItemCreateRequest item;
  63. UA_MonitoredItemCreateRequest_init(&item);
  64. item.itemToMonitor.nodeId = myIntegerNodeId;
  65. item.itemToMonitor.attributeId = UA_ATTRIBUTEID_VALUE;
  66. item.monitoringMode = UA_MONITORINGMODE_REPORTING;
  67. UA_Server_createDataChangeMonitoredItem(server, UA_TIMESTAMPSTORETURN_NEITHER,
  68. item, NULL, dataChangeNotificationCallback);
  69. callbackCount = 0;
  70. UA_MonitoredItem *mon = LIST_FIRST(&server->localMonitoredItems);
  71. clock_t begin, finish;
  72. begin = clock();
  73. for(int i = 0; i < 1000000; i++) {
  74. UA_MonitoredItem_sampleCallback(server, mon);
  75. }
  76. finish = clock();
  77. double time_spent = (double)(finish - begin) / CLOCKS_PER_SEC;
  78. printf("duration was %f s\n", time_spent);
  79. printf("retval is %s\n", UA_StatusCode_name(retval));
  80. UA_assert(callbackCount == 0);
  81. }
  82. END_TEST
  83. static Suite * monitoring_speed_suite (void) {
  84. Suite *s = suite_create ("Monitoring Speed");
  85. TCase* tc_datachange = tcase_create ("DataChange");
  86. tcase_add_checked_fixture(tc_datachange, setup, teardown);
  87. tcase_add_test (tc_datachange, monitorIntegerNoChanges);
  88. suite_add_tcase (s, tc_datachange);
  89. return s;
  90. }
  91. int main (void) {
  92. int number_failed = 0;
  93. Suite *s = monitoring_speed_suite();
  94. SRunner *sr = srunner_create(s);
  95. srunner_set_fork_status(sr,CK_NOFORK);
  96. srunner_run_all(sr, CK_NORMAL);
  97. number_failed += srunner_ntests_failed (sr);
  98. srunner_free(sr);
  99. return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
  100. }