check_server_monitoringspeed.c 4.3 KB

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