check_pubsub_publishspeed.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this
  3. * file, You can obtain one at http://mozilla.org/MPL/2.0/.
  4. *
  5. * Copyright (c) 2017 - 2018 Fraunhofer IOSB (Author: Andreas Ebner)
  6. */
  7. #include <time.h>
  8. #include <stdio.h>
  9. #include <check.h>
  10. #include "ua_server_pubsub.h"
  11. #include "src_generated/ua_types_generated_encoding_binary.h"
  12. #include "ua_types.h"
  13. #include "ua_pubsub.h"
  14. #include "ua_config_default.h"
  15. #include "ua_network_pubsub_udp.h"
  16. #include "ua_server_internal.h"
  17. UA_Server *server = NULL;
  18. UA_ServerConfig *config = NULL;
  19. UA_NodeId connection1, connection2, writerGroup1, writerGroup2, writerGroup3,
  20. publishedDataSet1, publishedDataSet2, dataSetWriter1, dataSetWriter2, dataSetWriter3;
  21. static void setup(void) {
  22. config = UA_ServerConfig_new_default();
  23. config->pubsubTransportLayers = (UA_PubSubTransportLayer *) UA_malloc(sizeof(UA_PubSubTransportLayer));
  24. if(!config->pubsubTransportLayers) {
  25. UA_ServerConfig_delete(config);
  26. }
  27. config->pubsubTransportLayers[0] = UA_PubSubTransportLayerUDPMP();
  28. config->pubsubTransportLayersSize++;
  29. server = UA_Server_new(config);
  30. UA_PubSubConnectionConfig connectionConfig;
  31. memset(&connectionConfig, 0, sizeof(UA_PubSubConnectionConfig));
  32. connectionConfig.name = UA_STRING("UADP Connection");
  33. UA_NetworkAddressUrlDataType networkAddressUrl = {UA_STRING_NULL, UA_STRING("opc.udp://224.0.0.22:4840/")};
  34. UA_Variant_setScalar(&connectionConfig.address, &networkAddressUrl,
  35. &UA_TYPES[UA_TYPES_NETWORKADDRESSURLDATATYPE]);
  36. connectionConfig.transportProfileUri = UA_STRING("http://opcfoundation.org/UA-Profile/Transport/pubsub-udp-uadp");
  37. UA_Server_addPubSubConnection(server, &connectionConfig, &connection1);
  38. UA_WriterGroupConfig writerGroupConfig;
  39. memset(&writerGroupConfig, 0, sizeof(writerGroupConfig));
  40. writerGroupConfig.name = UA_STRING("WriterGroup 1");
  41. writerGroupConfig.publishingInterval = 10;
  42. writerGroupConfig.encodingMimeType = UA_PUBSUB_ENCODING_UADP;
  43. UA_Server_addWriterGroup(server, connection1, &writerGroupConfig, &writerGroup1);
  44. UA_PublishedDataSetConfig pdsConfig;
  45. memset(&pdsConfig, 0, sizeof(UA_PublishedDataSetConfig));
  46. pdsConfig.publishedDataSetType = UA_PUBSUB_DATASET_PUBLISHEDITEMS;
  47. pdsConfig.name = UA_STRING("PublishedDataSet 1");
  48. UA_Server_addPublishedDataSet(server, &pdsConfig, &publishedDataSet1);
  49. UA_DataSetWriterConfig dataSetWriterConfig;
  50. memset(&dataSetWriterConfig, 0, sizeof(dataSetWriterConfig));
  51. dataSetWriterConfig.name = UA_STRING("DataSetWriter 1");
  52. UA_Server_addDataSetWriter(server, writerGroup1, publishedDataSet1, &dataSetWriterConfig, &dataSetWriter1);
  53. UA_Server_run_startup(server);
  54. }
  55. static void teardown(void) {
  56. UA_Server_run_shutdown(server);
  57. UA_Server_delete(server);
  58. UA_ServerConfig_delete(config);
  59. }
  60. START_TEST(PublishSpeedTest) {
  61. UA_DataSetFieldConfig dataSetFieldConfig;
  62. memset(&dataSetFieldConfig, 0, sizeof(UA_DataSetFieldConfig));
  63. dataSetFieldConfig.dataSetFieldType = UA_PUBSUB_DATASETFIELD_VARIABLE;
  64. dataSetFieldConfig.field.variable.fieldNameAlias = UA_STRING("Server localtime");
  65. dataSetFieldConfig.field.variable.promotedField = UA_FALSE;
  66. dataSetFieldConfig.field.variable.publishParameters.publishedVariable = UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER_LOCALTIME);
  67. dataSetFieldConfig.field.variable.publishParameters.attributeId = UA_ATTRIBUTEID_VALUE;
  68. UA_Server_addDataSetField(server, publishedDataSet1, &dataSetFieldConfig, NULL);
  69. UA_WriterGroup *wg = UA_WriterGroup_findWGbyId(server, writerGroup1);
  70. printf("start sending 8000 publish messages via UDP\n");
  71. clock_t begin, finish;
  72. begin = clock();
  73. for(int i = 0; i < 8000; i++) {
  74. UA_WriterGroup_publishCallback(server, wg);
  75. }
  76. finish = clock();
  77. double time_spent = (double)(finish - begin) / CLOCKS_PER_SEC;
  78. printf("duration was %f s\n", time_spent);
  79. } END_TEST
  80. int main(void) {
  81. TCase *tc_publishspeed = tcase_create("Speed of the publisher");
  82. tcase_add_checked_fixture(tc_publishspeed, setup, teardown);
  83. tcase_add_test(tc_publishspeed, PublishSpeedTest);
  84. Suite *s = suite_create("PubSub Speed Test");
  85. suite_add_tcase(s, tc_publishspeed);
  86. SRunner *sr = srunner_create(s);
  87. srunner_set_fork_status(sr, CK_NOFORK);
  88. srunner_run_all(sr,CK_NORMAL);
  89. int number_failed = srunner_ntests_failed(sr);
  90. srunner_free(sr);
  91. return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
  92. }