ctkVTKConnectionTest1.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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 <QTimer>
  19. // CTKVTK includes
  20. #include "ctkVTKConnection.h"
  21. // VTK includes
  22. #include <vtkCallbackCommand.h>
  23. #include <vtkCommand.h>
  24. #include <vtkNew.h>
  25. #include <vtkObject.h>
  26. #include <vtkSmartPointer.h>
  27. #include <vtkTimerLog.h>
  28. // STD includes
  29. #include <cstdlib>
  30. #include <iostream>
  31. namespace
  32. {
  33. //-----------------------------------------------------------------------------
  34. void doit(vtkObject* vtkNotUsed(obj), unsigned long vtkNotUsed(event),
  35. void* client_data, void* vtkNotUsed(param))
  36. {
  37. QTimer* t = reinterpret_cast<QTimer*>(client_data);
  38. t->stop();
  39. }
  40. //-----------------------------------------------------------------------------
  41. double computeConnectionTiming(const QString& connectionDescription,
  42. int eventCount,
  43. int objectCount, vtkObject* obj)
  44. {
  45. vtkNew<vtkTimerLog> timerLog;
  46. timerLog->StartTimer();
  47. for (int i = 0; i < eventCount; ++i)
  48. {
  49. obj->Modified();
  50. }
  51. timerLog->StopTimer();
  52. double elapsedTime = timerLog->GetElapsedTime();
  53. qDebug()
  54. << qPrintable(connectionDescription.leftJustified(30, ' ')) << ":"
  55. << "1 event invoked" << eventCount << "times and listened by" << objectCount
  56. << "objects: " << elapsedTime << "seconds";
  57. return elapsedTime;
  58. }
  59. } // end of anonymous namespace
  60. //-----------------------------------------------------------------------------
  61. int ctkVTKConnectionTest1( int argc, char * argv [] )
  62. {
  63. QCoreApplication app(argc, argv);
  64. int objects = 1000;
  65. int events = 100;
  66. vtkObject* obj = vtkObject::New();
  67. vtkObject* obj2 = vtkObject::New();
  68. vtkObject* obj3 = vtkObject::New();
  69. vtkObject* obj4 = vtkObject::New();
  70. vtkObject* obj5 = vtkObject::New();
  71. int connection1_observeDeletion = 0;
  72. // NA for connection2
  73. int connection3_observeDeletion = 1;
  74. int connection4_observeDeletion = 0;
  75. int connection5_observeDeletion = 1;
  76. QObject* topObject = new QObject(0);
  77. // It could be here any kind of Qt object, QTimer has a no op slot so use it
  78. QTimer* slotObject = new QTimer(topObject);
  79. for (int i = 0; i < objects; ++i)
  80. {
  81. // connection1
  82. ctkVTKConnection* connection1 = new ctkVTKConnection(topObject);
  83. connection1->observeDeletion(connection1_observeDeletion);
  84. connection1->setup(obj, vtkCommand::ModifiedEvent,
  85. slotObject, SLOT(stop()));
  86. // connection2: regular callback
  87. vtkCallbackCommand* callback = vtkCallbackCommand::New();
  88. callback->SetClientData(slotObject);
  89. callback->SetCallback(doit);
  90. obj2->AddObserver(vtkCommand::ModifiedEvent, callback);
  91. callback->Delete();
  92. // connection3
  93. ctkVTKConnection* connection3 = new ctkVTKConnection(topObject);
  94. connection3->observeDeletion(connection3_observeDeletion);
  95. connection3->setup(obj3, vtkCommand::ModifiedEvent,
  96. slotObject, SLOT(stop()));
  97. // connection4
  98. ctkVTKConnection* connection4 = new ctkVTKConnection(topObject);
  99. connection4->observeDeletion(connection4_observeDeletion);
  100. connection4->setup(obj4, vtkCommand::ModifiedEvent,
  101. new QTimer(topObject), SLOT(stop()));
  102. // connection5
  103. ctkVTKConnection* connection5 = new ctkVTKConnection(topObject);
  104. connection5->observeDeletion(connection5_observeDeletion);
  105. connection5->setup(obj5, vtkCommand::ModifiedEvent,
  106. slotObject, SLOT(stop()));
  107. }
  108. // Compute timing for connection1
  109. QString connection1_description =
  110. QString("connection1 / %1").arg(connection1_observeDeletion);
  111. double time_connection1 = computeConnectionTiming(connection1_description, events , objects, obj);
  112. // Compute timing for connection2: Callback only
  113. QString connection2_description =
  114. QString("connection2 / vtkCallback");
  115. double time_connection2 = computeConnectionTiming(connection2_description, events , objects, obj2);
  116. // Compute timing for connection3
  117. QString connection3_description =
  118. QString("connection3 / %1").arg(connection3_observeDeletion);
  119. computeConnectionTiming(connection3_description, events , objects, obj3);
  120. // Compute timing for connection4
  121. QString connection4_description =
  122. QString("connection4 / %1 / 1-1").arg(connection4_observeDeletion);
  123. computeConnectionTiming(connection4_description, events , objects, obj4);
  124. double ratio = time_connection1 / time_connection2;
  125. qDebug().nospace() << "Ratio [ time_connection1 / time_connection2 ]: " << ratio;
  126. obj->Delete();
  127. obj2->Delete();
  128. obj3->Delete();
  129. delete topObject;
  130. obj4->Delete();
  131. obj5->Delete();
  132. #ifdef QT_NO_DEBUG // In Debug mode, the ratio can be over 2 !
  133. // Ideally a ratio ~= 1.
  134. if (ratio > 1.2)
  135. {
  136. return EXIT_FAILURE;
  137. }
  138. #endif
  139. return EXIT_SUCCESS;
  140. }