ctkEventAdminPerfTestSuite.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /*=============================================================================
  2. Library: CTK
  3. Copyright (c) German Cancer Research Center,
  4. Division of Medical and Biological Informatics
  5. Licensed under the Apache License, Version 2.0 (the "License");
  6. you may not use this file except in compliance with the License.
  7. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. =============================================================================*/
  15. #include "ctkEventAdminPerfTestSuite_p.h"
  16. #include <ctkPluginContext.h>
  17. #include <ctkServiceEvent.h>
  18. #include <service/event/ctkEventAdmin.h>
  19. #include <service/event/ctkEventConstants.h>
  20. #include <QTest>
  21. #include <QDebug>
  22. //----------------------------------------------------------------------------
  23. TestEventHandler::TestEventHandler(int& counter)
  24. : counter(counter)
  25. {}
  26. //----------------------------------------------------------------------------
  27. void TestEventHandler::handleEvent(const ctkEvent& )
  28. {
  29. counter++;
  30. }
  31. //----------------------------------------------------------------------------
  32. ctkEventAdminPerfTestSuite::ctkEventAdminPerfTestSuite(ctkPluginContext *context, int pluginId)
  33. : pc(context)
  34. , pluginId(pluginId)
  35. , nSendEvents(400)
  36. , nHandlers(40)
  37. , nEvent1Handled(0)
  38. , nEvent2Handled(0)
  39. , eventAdmin(0)
  40. {
  41. }
  42. //----------------------------------------------------------------------------
  43. void ctkEventAdminPerfTestSuite::addHandlers()
  44. {
  45. qDebug() << "Adding" << nHandlers << "event handlers";
  46. for (int i = 0; i < nHandlers; ++i)
  47. {
  48. TestEventHandler* h1 = new TestEventHandler(nEvent1Handled);
  49. handlers.push_back(h1);
  50. ctkDictionary props1;
  51. props1.insert(ctkEventConstants::EVENT_TOPIC, "org/bla/1");
  52. handlerRegistrations.push_back(pc->registerService<ctkEventHandler>(h1, props1));
  53. TestEventHandler* h2 = new TestEventHandler(nEvent2Handled);
  54. handlers.push_back(h2);
  55. ctkDictionary props2;
  56. props2.insert(ctkEventConstants::EVENT_TOPIC, "org/bla/*");
  57. handlerRegistrations.push_back(pc->registerService<ctkEventHandler>(h2, props2));
  58. TestEventHandler* h3 = new TestEventHandler(nEvent2Handled);
  59. handlers.push_back(h3);
  60. ctkDictionary props3;
  61. props3.insert(ctkEventConstants::EVENT_TOPIC, "org/bla/*");
  62. props3.insert(ctkEventConstants::EVENT_FILTER, "(name=bla)");
  63. handlerRegistrations.push_back(pc->registerService<ctkEventHandler>(h3, props3));
  64. }
  65. }
  66. //----------------------------------------------------------------------------
  67. void ctkEventAdminPerfTestSuite::removeHandlers()
  68. {
  69. foreach(ctkServiceRegistration sr, handlerRegistrations)
  70. {
  71. sr.unregister();
  72. }
  73. handlerRegistrations.clear();
  74. qDeleteAll(handlers);
  75. handlers.clear();
  76. }
  77. //----------------------------------------------------------------------------
  78. void ctkEventAdminPerfTestSuite::sendEvents()
  79. {
  80. ctkEvent event1("org/bla/1");
  81. for (int i = 0; i < nSendEvents; ++i)
  82. {
  83. eventAdmin->sendEvent(event1);
  84. }
  85. for (int i = 0; i < nSendEvents; ++i)
  86. {
  87. ctkDictionary props;
  88. props.insert("name", "bla");
  89. props.insert("level", i);
  90. ctkEvent event2("org/bla/2", props);
  91. eventAdmin->sendEvent(event2);
  92. }
  93. }
  94. //----------------------------------------------------------------------------
  95. void ctkEventAdminPerfTestSuite::postEvents()
  96. {
  97. ctkEvent event1("org/bla/1");
  98. for (int i = 0; i < nSendEvents; ++i)
  99. {
  100. eventAdmin->postEvent(event1);
  101. }
  102. for (int i = 0; i < nSendEvents; ++i)
  103. {
  104. ctkDictionary props;
  105. props.insert("name", "bla");
  106. props.insert("level", i);
  107. ctkEvent event2("org/bla/2", props);
  108. eventAdmin->postEvent(event2);
  109. }
  110. }
  111. //----------------------------------------------------------------------------
  112. void ctkEventAdminPerfTestSuite::initTestCase()
  113. {
  114. nEvent1Handled = 0;
  115. nEvent2Handled = 0;
  116. pc->getPlugin(pluginId)->start();
  117. ctkServiceReference reference = pc->getServiceReference<ctkEventAdmin>();
  118. QVERIFY(reference);
  119. eventAdmin = pc->getService<ctkEventAdmin>(reference);
  120. QVERIFY(eventAdmin);
  121. addHandlers();
  122. }
  123. //----------------------------------------------------------------------------
  124. void ctkEventAdminPerfTestSuite::testSendEvents()
  125. {
  126. QTime t;
  127. t.start();
  128. sendEvents();
  129. int ms = t.elapsed();
  130. QCOMPARE(nEvent1Handled, nSendEvents * nHandlers);
  131. QCOMPARE(nEvent2Handled, nSendEvents * nHandlers * 3);
  132. qDebug() << "Sending" << 2*nSendEvents << "synchronous events took" << ms << "ms";
  133. }
  134. //----------------------------------------------------------------------------
  135. void ctkEventAdminPerfTestSuite::testPostEvents()
  136. {
  137. QTime t;
  138. t.start();
  139. postEvents();
  140. int ms = t.elapsed();
  141. qDebug() << "Sending" << 2*nSendEvents << "asynchronous events took" << ms << "ms";
  142. // wait a little for the asynchronous handling of events
  143. QTest::qWait(10000);
  144. }
  145. //----------------------------------------------------------------------------
  146. void ctkEventAdminPerfTestSuite::cleanupTestCase()
  147. {
  148. try
  149. {
  150. removeHandlers();
  151. pc->getPlugin(pluginId)->stop();
  152. }
  153. catch (const ctkInvalidArgumentException&)
  154. {
  155. qDebug() << "NOooooo!!!";
  156. }
  157. }