Selaa lähdekoodia

ENH: Add testing for ctkVTKConnection

Julien Finet 15 vuotta sitten
vanhempi
commit
c6ba76d784

+ 2 - 0
Libs/Visualization/VTK/Core/Testing/Cpp/CMakeLists.txt

@@ -39,6 +39,7 @@ CONFIGURE_FILE(
 
 CREATE_TEST_SOURCELIST(Tests ${KIT}CppTests.cpp
   ctkVTKCommandOptionsTest1.cpp
+  ctkVTKConnectionTest1.cpp
   ctkVTKObjectTest1.cpp
   #EXTRA_INCLUDE TestingMacros.h
   )
@@ -66,6 +67,7 @@ ENDMACRO( SIMPLE_TEST  )
 #
 
 SIMPLE_TEST( ctkVTKObjectTest1 )
+SIMPLE_TEST( ctkVTKConnectionTest1 )
 
 ADD_TEST( ctkVTKCommandOptionsTest1 ${KIT_TESTS}
           ctkVTKCommandOptionsTest1 --help )

+ 89 - 0
Libs/Visualization/VTK/Core/Testing/Cpp/ctkVTKConnectionTest1.cpp

@@ -0,0 +1,89 @@
+
+// Qt includes
+#include <QApplication>
+#include <QDebug>
+#include <QWidget>
+
+// CTKVTK includes
+#include "ctkVTKConnection.h"
+
+// STD includes
+#include <cstdlib>
+#include <iostream>
+
+// VTK includes
+#include <vtkCallbackCommand.h>
+#include <vtkCommand.h>
+#include <vtkObject.h>
+#include <vtkSmartPointer.h>
+#include <vtkTimerLog.h>
+
+void doit(vtkObject* obj, unsigned long event, void* client_data, void* param)
+{
+  QWidget* w = reinterpret_cast<QWidget*>(client_data);
+  w->setFocus();
+}
+
+int ctkVTKConnectionTest1( int argc, char * argv [] )
+{
+  QApplication app(argc, argv);
+  vtkObject* obj = vtkObject::New();
+  QWidget topLevelWidget;
+
+  int objects = 1000;
+  int events = 100;
+  
+  for (int i = 0; i < objects; ++i)
+    {
+    ctkVTKConnection* objectTest = new ctkVTKConnection(&topLevelWidget);
+    objectTest->SetParameters(obj, vtkCommand::ModifiedEvent,
+                              &topLevelWidget, SLOT(setFocus()));
+    objectTest->setEnabled(true);
+    }
+  vtkSmartPointer<vtkTimerLog> timerLog = 
+    vtkSmartPointer<vtkTimerLog>::New();
+  
+  timerLog->StartTimer();
+  for (int i = 0; i < events; ++i)
+    {
+    obj->Modified();
+    }
+  timerLog->StopTimer();
+  
+  double t1 = timerLog->GetElapsedTime();
+  qDebug() << events << "events listened by" << objects << "objects (ctkVTKConnection): " << t1 << "seconds";
+
+  obj->Delete();
+
+  vtkObject* obj2 = vtkObject::New();
+  for (int i = 0; i < objects; ++i)
+    {
+    vtkCallbackCommand* callback = vtkCallbackCommand::New();
+    callback->SetClientData(&topLevelWidget);
+    callback->SetCallback(doit);
+    
+    obj2->AddObserver(vtkCommand::ModifiedEvent, callback);
+    callback->Delete();
+    }
+  vtkSmartPointer<vtkTimerLog> timerLog2 = 
+    vtkSmartPointer<vtkTimerLog>::New();
+  timerLog2->StartTimer();
+  for (int i = 0; i < events; ++i)
+    {
+    obj2->Modified();
+    }
+  timerLog2->StopTimer();
+
+  double t2 = timerLog2->GetElapsedTime();
+  qDebug() << events << "events listened by" << objects <<"objects (vtkCallbacks): " << t2 << "seconds";
+  double ratio = t1 / t2;
+  qDebug() << "ctkVTKConnection / vtkCallbacks: " << ratio;
+  // Ideally a ratio of 2 (a callback and a signal/slot connection is used 
+  // is used in ctkVTKConnection
+  if (ratio > 2.5)
+    {
+    return EXIT_FAILURE;
+    }
+  obj2->Delete();
+  return EXIT_SUCCESS;
+}

+ 1 - 1
Libs/Visualization/VTK/Core/ctkVTKConnection.h

@@ -49,7 +49,7 @@ public:
 
   /// 
   void SetParameters(vtkObject* vtk_obj, unsigned long vtk_event,
-    const QObject* qt_obj, QString qt_slot, float priority);
+    const QObject* qt_obj, QString qt_slot, float priority = 0.f);
 
   /// 
   /// Check the validity of the parameters. Parameters must be valid to add