ctkVTKConnectionTest1.cpp 4.1 KB

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