ctkVTKConnectionTest1.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. // Qt includes
  2. #include <QApplication>
  3. #include <QDebug>
  4. #include <QList>
  5. #include <QTimer>
  6. // CTKVTK includes
  7. #include "ctkVTKConnection.h"
  8. // STD includes
  9. #include <cstdlib>
  10. #include <iostream>
  11. // VTK includes
  12. #include <vtkCallbackCommand.h>
  13. #include <vtkCommand.h>
  14. #include <vtkObject.h>
  15. #include <vtkSmartPointer.h>
  16. #include <vtkTimerLog.h>
  17. void doit(vtkObject* obj, unsigned long event, void* client_data, void* param)
  18. {
  19. QTimer* t = reinterpret_cast<QTimer*>(client_data);
  20. t->stop();
  21. }
  22. int ctkVTKConnectionTest1( int argc, char * argv [] )
  23. {
  24. QApplication app(argc, argv);
  25. int objects = 1000;
  26. int events = 100;
  27. vtkObject* obj = vtkObject::New();
  28. vtkObject* obj2 = vtkObject::New();
  29. vtkObject* obj3 = vtkObject::New();
  30. vtkObject* obj4 = vtkObject::New();
  31. vtkObject* obj5 = vtkObject::New();
  32. QObject* topObject = new QObject(0);
  33. // It could be here any kind of Qt object, QTimer has a no op slot so use it
  34. QTimer* slotObject = new QTimer(topObject);
  35. for (int i = 0; i < objects; ++i)
  36. {
  37. ctkVTKConnection* connection = new ctkVTKConnection(topObject);
  38. connection->observeDeletion(false);
  39. connection->setup(obj, vtkCommand::ModifiedEvent,
  40. slotObject, SLOT(stop()));
  41. vtkCallbackCommand* callback = vtkCallbackCommand::New();
  42. callback->SetClientData(slotObject);
  43. callback->SetCallback(doit);
  44. obj2->AddObserver(vtkCommand::ModifiedEvent, callback);
  45. callback->Delete();
  46. ctkVTKConnection* connection2 = new ctkVTKConnection(topObject);
  47. connection2->observeDeletion(true);
  48. connection2->setup(obj3, vtkCommand::ModifiedEvent,
  49. slotObject, SLOT(stop()));
  50. ctkVTKConnection* connection3 = new ctkVTKConnection(topObject);
  51. connection3->observeDeletion(false);
  52. connection3->setup(obj4, vtkCommand::ModifiedEvent,
  53. new QTimer(topObject), SLOT(stop()));
  54. ctkVTKConnection* connection4 = new ctkVTKConnection(topObject);
  55. connection4->observeDeletion(true);
  56. connection4->setup(obj5, vtkCommand::ModifiedEvent,
  57. slotObject, SLOT(stop()));
  58. }
  59. vtkSmartPointer<vtkTimerLog> timerLog =
  60. vtkSmartPointer<vtkTimerLog>::New();
  61. timerLog->StartTimer();
  62. for (int i = 0; i < events; ++i)
  63. {
  64. obj->Modified();
  65. }
  66. timerLog->StopTimer();
  67. double t1 = timerLog->GetElapsedTime();
  68. qDebug() << events << "events listened by" << objects << "objects (ctkVTKConnection): " << t1 << "seconds";
  69. // Callback only
  70. vtkSmartPointer<vtkTimerLog> timerLog2 =
  71. vtkSmartPointer<vtkTimerLog>::New();
  72. timerLog2->StartTimer();
  73. for (int i = 0; i < events; ++i)
  74. {
  75. obj2->Modified();
  76. }
  77. timerLog2->StopTimer();
  78. double t2 = timerLog2->GetElapsedTime();
  79. qDebug() << events << "events listened by" << objects <<"objects (vtkCallbacks): " << t2 << "seconds";
  80. double ratio = t1 / t2;
  81. qDebug() << "ctkVTKConnection / vtkCallbacks: " << ratio;
  82. vtkSmartPointer<vtkTimerLog> timerLog3 =
  83. vtkSmartPointer<vtkTimerLog>::New();
  84. timerLog3->StartTimer();
  85. for (int i = 0; i < events; ++i)
  86. {
  87. obj3->Modified();
  88. }
  89. timerLog3->StopTimer();
  90. double t3 = timerLog3->GetElapsedTime();
  91. qDebug() << events << "events listened by" << objects << "objects (observed ctkVTKConnection): " << t3 << "seconds";
  92. vtkSmartPointer<vtkTimerLog> timerLog4 =
  93. vtkSmartPointer<vtkTimerLog>::New();
  94. timerLog4->StartTimer();
  95. for (int i = 0; i < events; ++i)
  96. {
  97. obj4->Modified();
  98. }
  99. timerLog4->StopTimer();
  100. double t4 = timerLog4->GetElapsedTime();
  101. qDebug() << events << "events listened by" << objects << "objects (ctkVTKConnection, 1-1): " << t4 << "seconds";
  102. obj->Delete();
  103. obj2->Delete();
  104. obj3->Delete();
  105. delete topObject;
  106. obj4->Delete();
  107. obj5->Delete();
  108. // Ideally a ratio ~= 1. but the ratio can be more in Debug mode... up to 2.
  109. if (ratio > 2.)
  110. {
  111. return EXIT_FAILURE;
  112. }
  113. return EXIT_SUCCESS;
  114. }