ctkVTKConnectionTest1.cpp 3.9 KB

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