ctkVTKConnectionTestObjectDelete.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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 <QStringList>
  17. #include <QTimer>
  18. // CTK includes
  19. #include <ctkCallback.h>
  20. // CTKVTK includes
  21. #include <ctkVTKConnection.h>
  22. // VTK includes
  23. #include <vtkCallbackCommand.h>
  24. #include <vtkCommand.h>
  25. #include <vtkNew.h>
  26. #include <vtkObject.h>
  27. #include <vtkSmartPointer.h>
  28. #include <vtkTimerLog.h>
  29. // STD includes
  30. #include <cstdlib>
  31. #include <iostream>
  32. #include "moc_ctkVTKConnectionTestObjectDelete.cpp"
  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. template<typename T>
  45. bool check(int line, const char* valueName, T current, T expected)
  46. {
  47. if (current != expected)
  48. {
  49. std::cerr << "Line " << line << "\n"
  50. << "\tcurrent " << valueName << ":" << current << "\n"
  51. << "\texpected " << valueName << ":" << expected
  52. << std::endl;
  53. return false;
  54. }
  55. return true;
  56. }
  57. //-----------------------------------------------------------------------------
  58. void displayDartMeasurement(const char* name, double value)
  59. {
  60. std::cout << "<DartMeasurement name=\""<< name <<"\" "
  61. << "type=\"numeric/double\">"
  62. << value << "</DartMeasurement>" << std::endl;
  63. }
  64. //-----------------------------------------------------------------------------
  65. bool computeTimingAfterObjectDelete(bool observeDeletion,
  66. int connectionCount,
  67. int objectCount,
  68. bool deleteVTK,
  69. bool deleteQt)
  70. {
  71. vtkNew<vtkTimerLog> timerLog;
  72. QObject* topObject = new QObject(0);
  73. ctkCallback* slotObject = new ctkCallback(spy, topObject);
  74. QList< ctkVTKConnection* > connections;
  75. QList< vtkSmartPointer<vtkObject> > objects;
  76. total_event_count = 0;
  77. for (int connectionIdx = 0; connectionIdx < connectionCount; ++connectionIdx)
  78. {
  79. for (int objectIdx = 0; objectIdx < objectCount; ++objectIdx)
  80. {
  81. vtkNew<vtkObject> object;
  82. ctkVTKConnection* connection = new ctkVTKConnection(topObject);
  83. connection->observeDeletion(observeDeletion);
  84. connection->setup(object.GetPointer(), vtkCommand::ModifiedEvent,
  85. slotObject, SLOT(invoke()));
  86. connections.append(connection);
  87. objects.append(object.GetPointer());
  88. }
  89. }
  90. QString attribute(observeDeletion ? "observeDeletion" : "noObserveDeletion");
  91. {
  92. timerLog->StartTimer();
  93. foreach(vtkSmartPointer<vtkObject> object, objects)
  94. {
  95. object->Modified();
  96. }
  97. timerLog->StopTimer();
  98. QString measurementName = QString("time_%1-%2-%3-%4").arg(
  99. "connection-object-modified").arg(attribute).arg(connectionCount).arg(objectCount);
  100. displayDartMeasurement(qPrintable(measurementName), timerLog->GetElapsedTime());
  101. if (!check<int>(__LINE__, "total_event_count",
  102. /* current= */ total_event_count,
  103. /* expected = */ connectionCount * objectCount))
  104. {
  105. return false;
  106. }
  107. }
  108. if (deleteVTK)
  109. {
  110. total_event_count = 0;
  111. timerLog->StartTimer();
  112. objects.clear();
  113. timerLog->StopTimer();
  114. QString measurementName = QString("time_%1-%2-%3-%4").arg(
  115. "connection-vtkobject-deleted").arg(attribute).arg(connectionCount).arg(objectCount);
  116. displayDartMeasurement(qPrintable(measurementName), timerLog->GetElapsedTime());
  117. if (!check<int>(__LINE__, "total_event_count",
  118. /* current= */ total_event_count,
  119. /* expected = */ 0))
  120. {
  121. return false;
  122. }
  123. if (!check<void*>(__LINE__, "connection_0_vtkobject",
  124. /* current= */ connections.at(0)->vtkobject(),
  125. /* expected = */ 0))
  126. {
  127. return false;
  128. }
  129. if (!check<void*>(__LINE__, "connection_last_vtkobject",
  130. /* current= */ connections.last()->vtkobject(),
  131. /* expected = */ 0))
  132. {
  133. return false;
  134. }
  135. }
  136. if (deleteQt)
  137. {
  138. total_event_count = 0;
  139. timerLog->StartTimer();
  140. QTimer::singleShot(0, slotObject, SLOT(deleteLater()));
  141. QCoreApplication::sendPostedEvents();
  142. QCoreApplication::processEvents();
  143. timerLog->StopTimer();
  144. QString measurementName = QString("time_%1-%2-%3-%4").arg(
  145. "connection-qtobject-deleted").arg(attribute).arg(connectionCount).arg(objectCount);
  146. displayDartMeasurement(qPrintable(measurementName), timerLog->GetElapsedTime());
  147. if (!check<int>(__LINE__, "total_event_count",
  148. /* current= */ total_event_count,
  149. /* expected = */ 0))
  150. {
  151. return false;
  152. }
  153. if (!check<void*>(__LINE__, "connection_0_qtobject",
  154. /* current= */ connections.at(0)->object(),
  155. /* expected = */ 0))
  156. {
  157. return false;
  158. }
  159. if (!check<void*>(__LINE__, "connection_last_qtobject",
  160. /* current= */ connections.last()->object(),
  161. /* expected = */ 0))
  162. {
  163. return false;
  164. }
  165. }
  166. delete topObject;
  167. return true;
  168. }
  169. } // end of anonymous namespace
  170. //-----------------------------------------------------------------------------
  171. int ctkVTKConnectionTestObjectDelete( int argc, char * argv [] )
  172. {
  173. QCoreApplication app(argc, argv);
  174. int testCase = -1;
  175. if (argc > 1)
  176. {
  177. testCase = app.arguments().at(1).toInt();
  178. }
  179. int connectionCount = 1000;
  180. int objectCount = 100;
  181. if (testCase == -1 || testCase == 1)
  182. {
  183. if (!computeTimingAfterObjectDelete(/* observeDeletion = */ true,
  184. connectionCount, objectCount,
  185. /* deleteVTK = */ true,
  186. /* deleteQt = */ false))
  187. {
  188. return EXIT_FAILURE;
  189. }
  190. }
  191. if (testCase == -1 || testCase == 2)
  192. {
  193. if (!computeTimingAfterObjectDelete(/* observeDeletion = */ false,
  194. connectionCount, objectCount,
  195. /* deleteVTK = */ true,
  196. /* deleteQt = */ false))
  197. {
  198. return EXIT_FAILURE;
  199. }
  200. }
  201. if (testCase == -1 || testCase == 3)
  202. {
  203. if (!computeTimingAfterObjectDelete(/* observeDeletion = */ true,
  204. connectionCount, objectCount,
  205. /* deleteVTK = */ false,
  206. /* deleteQt = */ true))
  207. {
  208. return EXIT_FAILURE;
  209. }
  210. }
  211. if (testCase == -1 || testCase == 4)
  212. {
  213. if (!computeTimingAfterObjectDelete(/* observeDeletion = */ false,
  214. connectionCount, objectCount,
  215. /* deleteVTK = */ false,
  216. /* deleteQt = */ true))
  217. {
  218. return EXIT_FAILURE;
  219. }
  220. }
  221. return EXIT_SUCCESS;
  222. }