ctkVTKConnectionTest1.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. /*=========================================================================
  2. Library: CTK
  3. Copyright (c) Kitware Inc.
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. http://www.apache.org/licenses/LICENSE-2.0.txt
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. =========================================================================*/
  14. // Qt includes
  15. #include <QCoreApplication>
  16. #include <QDebug>
  17. #include <QList>
  18. #include <QStringList>
  19. // CTK includes
  20. #include <ctkCallback.h>
  21. // CTKVTK includes
  22. #include "ctkVTKConnection.h"
  23. // VTK includes
  24. #include <vtkCallbackCommand.h>
  25. #include <vtkCommand.h>
  26. #include <vtkNew.h>
  27. #include <vtkObject.h>
  28. #include <vtkSmartPointer.h>
  29. #include <vtkTimerLog.h>
  30. // STD includes
  31. #include <cstdlib>
  32. #include <iostream>
  33. namespace
  34. {
  35. //-----------------------------------------------------------------------------
  36. int total_event_count;
  37. //-----------------------------------------------------------------------------
  38. void spy(void* data)
  39. {
  40. Q_UNUSED(data);
  41. ++total_event_count;
  42. }
  43. //-----------------------------------------------------------------------------
  44. void doit(vtkObject* vtkNotUsed(obj), unsigned long vtkNotUsed(event),
  45. void* client_data, void* vtkNotUsed(param))
  46. {
  47. ctkCallback* t = reinterpret_cast<ctkCallback*>(client_data);
  48. t->invoke();
  49. }
  50. //-----------------------------------------------------------------------------
  51. void displayDartMeasurement(const char* name, double value)
  52. {
  53. std::cout << "<DartMeasurement name=\""<< name <<"\" "
  54. << "type=\"numeric/double\">"
  55. << value << "</DartMeasurement>" << std::endl;
  56. }
  57. //-----------------------------------------------------------------------------
  58. // This function will trigger a ModifiedEvent <eventCount> times
  59. // where <objectCount> objects listen that same event.
  60. bool computeConnectionTiming(const char* name,
  61. int eventCount,
  62. int objectCount, vtkObject* obj,
  63. double & elapsedTime)
  64. {
  65. elapsedTime = 0;
  66. total_event_count = 0;
  67. vtkNew<vtkTimerLog> timerLog;
  68. timerLog->StartTimer();
  69. for (int i = 0; i < eventCount; ++i)
  70. {
  71. obj->Modified();
  72. }
  73. timerLog->StopTimer();
  74. int expected_total_event_count = eventCount * objectCount;
  75. if (total_event_count != expected_total_event_count)
  76. {
  77. std::cerr << "Problem with " << name << "\n"
  78. << "\tcurrent total_event_count:" << total_event_count << "\n"
  79. << "\texpected total_event_count:" << expected_total_event_count
  80. << std::endl;
  81. return false;
  82. }
  83. elapsedTime = timerLog->GetElapsedTime();
  84. QString measurementName = QString("time-%1-%2-%3").arg(name).arg(eventCount).arg(objectCount);
  85. displayDartMeasurement(qPrintable(measurementName), elapsedTime);
  86. return true;
  87. }
  88. //-----------------------------------------------------------------------------
  89. bool computeConnectionTiming(const char* name,
  90. int eventCount,
  91. int objectCount, vtkObject* obj)
  92. {
  93. double elapsedTime = 0;
  94. return computeConnectionTiming(name, eventCount, objectCount, obj, elapsedTime);
  95. }
  96. } // end of anonymous namespace
  97. //-----------------------------------------------------------------------------
  98. int ctkVTKConnectionTest1( int argc, char * argv [] )
  99. {
  100. QCoreApplication app(argc, argv);
  101. int testCase = -1;
  102. if (argc > 1)
  103. {
  104. testCase = app.arguments().at(1).toInt();
  105. }
  106. total_event_count = 0;
  107. int objects = 1000;
  108. int events = 100;
  109. vtkObject* obj1 = vtkObject::New();
  110. vtkObject* obj2 = vtkObject::New();
  111. vtkObject* obj3 = vtkObject::New();
  112. vtkObject* obj4 = vtkObject::New();
  113. vtkObject* obj5 = vtkObject::New();
  114. // NA for connection1
  115. int connection2_observeDeletion = 0;
  116. int connection3_observeDeletion = 1;
  117. int connection4_observeDeletion = 0;
  118. int connection5_observeDeletion = 1;
  119. QObject* topObject = new QObject(0);
  120. ctkCallback* slotObject = new ctkCallback(spy, topObject);
  121. for (int i = 0; i < objects; ++i)
  122. {
  123. // connection1: regular callback
  124. vtkCallbackCommand* callback = vtkCallbackCommand::New();
  125. callback->SetClientData(slotObject);
  126. callback->SetCallback(doit);
  127. obj1->AddObserver(vtkCommand::ModifiedEvent, callback);
  128. callback->Delete();
  129. // connection2
  130. ctkVTKConnection* connection2 = new ctkVTKConnection(topObject);
  131. connection2->observeDeletion(connection2_observeDeletion);
  132. connection2->setup(obj2, vtkCommand::ModifiedEvent,
  133. slotObject, SLOT(invoke()));
  134. // connection3
  135. ctkVTKConnection* connection3 = new ctkVTKConnection(topObject);
  136. connection3->observeDeletion(connection3_observeDeletion);
  137. connection3->setup(obj3, vtkCommand::ModifiedEvent,
  138. slotObject, SLOT(invoke()));
  139. // connection4
  140. ctkVTKConnection* connection4 = new ctkVTKConnection(topObject);
  141. connection4->observeDeletion(connection4_observeDeletion);
  142. connection4->setup(obj4, vtkCommand::ModifiedEvent,
  143. new ctkCallback(spy, topObject), SLOT(invoke()));
  144. // connection5
  145. ctkVTKConnection* connection5 = new ctkVTKConnection(topObject);
  146. connection5->observeDeletion(connection5_observeDeletion);
  147. connection5->setup(obj5, vtkCommand::ModifiedEvent,
  148. new ctkCallback(spy, topObject), SLOT(invoke()));
  149. }
  150. // Compute timing for connection1: Callback only
  151. double time_connection1 = 0;
  152. if (testCase == -1 || testCase == 1)
  153. {
  154. if (!computeConnectionTiming("connection1", events , objects, obj1, time_connection1))
  155. {
  156. return EXIT_FAILURE;
  157. }
  158. }
  159. // Compute timing for connection2
  160. // observeDeletion = 0
  161. double time_connection2 = 0;
  162. if (testCase == -1 || testCase == 2)
  163. {
  164. if (!computeConnectionTiming("connection2", events , objects, obj2, time_connection2))
  165. {
  166. return EXIT_FAILURE;
  167. }
  168. }
  169. // Compute timing for connection3
  170. // observeDeletion = 1
  171. if (testCase == -1 || testCase == 3)
  172. {
  173. if (!computeConnectionTiming("connection3", events , objects, obj3))
  174. {
  175. return EXIT_FAILURE;
  176. }
  177. }
  178. // Compute timing for connection4 - 1-1
  179. // observeDeletion = 0
  180. if (testCase == -1 || testCase == 4)
  181. {
  182. if (!computeConnectionTiming("connection4", events , objects, obj4))
  183. {
  184. return EXIT_FAILURE;
  185. }
  186. }
  187. // Compute timing for connection5 - 1-1
  188. // observeDeletion = 1
  189. if (testCase == -1 || testCase == 5)
  190. {
  191. if (!computeConnectionTiming("connection5", events , objects, obj4))
  192. {
  193. return EXIT_FAILURE;
  194. }
  195. }
  196. obj1->Delete();
  197. obj2->Delete();
  198. obj3->Delete();
  199. delete topObject;
  200. obj4->Delete();
  201. obj5->Delete();
  202. return EXIT_SUCCESS;
  203. }