check_server_monitoringspeed.c 4.3 KB

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