ctkVTKConnectionTest1.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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 <vtkObject.h>
  25. #include <vtkSmartPointer.h>
  26. #include <vtkTimerLog.h>
  27. // STD includes
  28. #include <cstdlib>
  29. #include <iostream>
  30. //-----------------------------------------------------------------------------
  31. void doit(vtkObject* vtkNotUsed(obj), unsigned long vtkNotUsed(event),
  32. void* client_data, void* vtkNotUsed(param))
  33. {
  34. QTimer* t = reinterpret_cast<QTimer*>(client_data);
  35. t->stop();
  36. }
  37. //-----------------------------------------------------------------------------
  38. int ctkVTKConnectionTest1( int argc, char * argv [] )
  39. {
  40. QCoreApplication app(argc, argv);
  41. int objects = 1000;
  42. int events = 100;
  43. vtkObject* obj = vtkObject::New();
  44. vtkObject* obj2 = vtkObject::New();
  45. vtkObject* obj3 = vtkObject::New();
  46. vtkObject* obj4 = vtkObject::New();
  47. vtkObject* obj5 = vtkObject::New();
  48. QObject* topObject = new QObject(0);
  49. // It could be here any kind of Qt object, QTimer has a no op slot so use it
  50. QTimer* slotObject = new QTimer(topObject);
  51. for (int i = 0; i < objects; ++i)
  52. {
  53. ctkVTKConnection* connection = new ctkVTKConnection(topObject);
  54. connection->observeDeletion(false);
  55. connection->setup(obj, vtkCommand::ModifiedEvent,
  56. slotObject, SLOT(stop()));
  57. vtkCallbackCommand* callback = vtkCallbackCommand::New();
  58. callback->SetClientData(slotObject);
  59. callback->SetCallback(doit);
  60. obj2->AddObserver(vtkCommand::ModifiedEvent, callback);
  61. callback->Delete();
  62. ctkVTKConnection* connection2 = new ctkVTKConnection(topObject);
  63. connection2->observeDeletion(true);
  64. connection2->setup(obj3, vtkCommand::ModifiedEvent,
  65. slotObject, SLOT(stop()));
  66. ctkVTKConnection* connection3 = new ctkVTKConnection(topObject);
  67. connection3->observeDeletion(false);
  68. connection3->setup(obj4, vtkCommand::ModifiedEvent,
  69. new QTimer(topObject), SLOT(stop()));
  70. ctkVTKConnection* connection4 = new ctkVTKConnection(topObject);
  71. connection4->observeDeletion(true);
  72. connection4->setup(obj5, vtkCommand::ModifiedEvent,
  73. slotObject, SLOT(stop()));
  74. }
  75. vtkSmartPointer<vtkTimerLog> timerLog =
  76. vtkSmartPointer<vtkTimerLog>::New();
  77. timerLog->StartTimer();
  78. for (int i = 0; i < events; ++i)
  79. {
  80. obj->Modified();
  81. }
  82. timerLog->StopTimer();
  83. double t1 = timerLog->GetElapsedTime();
  84. qDebug() << events << "events listened by" << objects << "objects (ctkVTKConnection): " << t1 << "seconds";
  85. // Callback only
  86. vtkSmartPointer<vtkTimerLog> timerLog2 =
  87. vtkSmartPointer<vtkTimerLog>::New();
  88. timerLog2->StartTimer();
  89. for (int i = 0; i < events; ++i)
  90. {
  91. obj2->Modified();
  92. }
  93. timerLog2->StopTimer();
  94. double t2 = timerLog2->GetElapsedTime();
  95. qDebug() << events << "events listened by" << objects <<"objects (vtkCallbacks): " << t2 << "seconds";
  96. double ratio = t1 / t2;
  97. qDebug() << "ctkVTKConnection / vtkCallbacks: " << ratio;
  98. vtkSmartPointer<vtkTimerLog> timerLog3 =
  99. vtkSmartPointer<vtkTimerLog>::New();
  100. timerLog3->StartTimer();
  101. for (int i = 0; i < events; ++i)
  102. {
  103. obj3->Modified();
  104. }
  105. timerLog3->StopTimer();
  106. double t3 = timerLog3->GetElapsedTime();
  107. qDebug() << events << "events listened by" << objects << "objects (observed ctkVTKConnection): " << t3 << "seconds";
  108. vtkSmartPointer<vtkTimerLog> timerLog4 =
  109. vtkSmartPointer<vtkTimerLog>::New();
  110. timerLog4->StartTimer();
  111. for (int i = 0; i < events; ++i)
  112. {
  113. obj4->Modified();
  114. }
  115. timerLog4->StopTimer();
  116. double t4 = timerLog4->GetElapsedTime();
  117. qDebug() << events << "events listened by" << objects << "objects (ctkVTKConnection, 1-1): " << t4 << "seconds";
  118. obj->Delete();
  119. obj2->Delete();
  120. obj3->Delete();
  121. delete topObject;
  122. obj4->Delete();
  123. obj5->Delete();
  124. #ifdef QT_NO_DEBUG // In Debug mode, the ratio can be over 2 !
  125. // Ideally a ratio ~= 1.
  126. if (ratio > 1.2)
  127. {
  128. return EXIT_FAILURE;
  129. }
  130. #endif
  131. return EXIT_SUCCESS;
  132. }