File: TestObserversPerformance.cxx

package info (click to toggle)
vtk7 7.1.1%2Bdfsg2-8
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 127,396 kB
  • sloc: cpp: 1,539,584; ansic: 124,382; python: 78,038; tcl: 47,013; xml: 8,142; yacc: 5,040; java: 4,439; perl: 3,132; lex: 1,926; sh: 1,500; makefile: 126; objc: 83
file content (177 lines) | stat: -rw-r--r-- 5,649 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/*=========================================================================

  Program:   Visualization Toolkit
  Module:    TestSmartPointer.cxx

  Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
  All rights reserved.
  See Copyright.txt or http://www.kitware.com/Copyright.htm for details.

     This software is distributed WITHOUT ANY WARRANTY; without even
     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
     PURPOSE.  See the above copyright notice for more information.

=========================================================================*/
// .NAME Test speed of Observers.
// .SECTION Description
// Probe the speed of vtkObject::AddObserver, vtkObject::InvokeEvent and
// vtkObject::RemoveObserver

#include "vtkCollection.h"
#include "vtkCommand.h"
#include "vtkNew.h"
#include "vtkObjectFactory.h"
#include "vtkSmartPointer.h"
#include "vtkTimerLog.h"

#include <map>
#include <vector>

// How many times the tests are run to average the elapsed time.
static const int STRESS_COUNT = 5;

// Description:
// Type of console outputs.
// CDash writes perfs as <DartMeasurement ...> for unit test regression
// Csv writes perfs as series of 2D table for easy plotting in spreadsheet apps
// Details writes more timing information
enum VerboseType
{
  None = 0x0,
  CDash = 0x1,
  Csv = 0x2,
  Details = 0x4
};

static const int VERBOSE_MODE = CDash;

//------------------------------------------------------------------------------
class vtkSimpleCommand : public vtkCommand
{
public:
  static vtkSimpleCommand* New() { return new vtkSimpleCommand();}
  vtkTypeMacro(vtkSimpleCommand, vtkCommand);

  void Execute(vtkObject*, unsigned long, void*) VTK_OVERRIDE
  {
      this->MTime.Modified();
  }
protected:
  static vtkTimeStamp MTime;
};

vtkTimeStamp vtkSimpleCommand::MTime;

//------------------------------------------------------------------------------
double TestStressInvoke(int observerCount, int eventCount, int invokeCount);

//------------------------------------------------------------------------------
int TestObserversPerformance(int, char*[])
{
  bool res = true;
  int maxInvokeCount = 1000;
  int maxEventCount = 100;
  int maxObserverCount = 1000;
  for (int eventCount = 1; eventCount <= maxEventCount; eventCount *= 10)
  {
    if (VERBOSE_MODE & Csv)
    {
      std::cout << eventCount << " events:" << "\n" << ",";
      for (int observerCount = 1; observerCount <= maxObserverCount; observerCount *= 10)
      {
        std::cout << observerCount << ",";
      }
      std::cout << std::endl;
    }
    for (int invokeCount = 1; invokeCount <= maxInvokeCount; invokeCount *= 10)
    {
      if (VERBOSE_MODE & Csv)
      {
        std::cout << invokeCount << ",";
      }
      for (int observerCount = 1; observerCount <= maxObserverCount; observerCount *= 10)
      {
        double time = TestStressInvoke(observerCount, eventCount, invokeCount);
        if (VERBOSE_MODE & Csv)
        {
          std::cout << time << ",";
        }
      }
      if (VERBOSE_MODE & Csv)
      {
        std::cout << std::endl;
      }
    }
  }
  return res ? EXIT_SUCCESS : EXIT_FAILURE;
}

//------------------------------------------------------------------------------
double StressInvoke(const int observerCount, const int eventCount, const int invokeCount)
{
  if (VERBOSE_MODE & Details)
  {
    std::cout << "StressInvoke " << invokeCount << " invokes "
              << "on " << eventCount << " events observed by "
              << observerCount / eventCount << " observers each." << std::endl;
  }
  vtkObject* volcano = vtkObject::New();
  std::vector<vtkSmartPointer<vtkSimpleCommand> > observers;
  vtkNew<vtkTimerLog> totalTimer;
  vtkNew<vtkTimerLog> addTimer;
  vtkNew<vtkTimerLog> invokeTimer;
  vtkNew<vtkTimerLog> removeTimer;
  totalTimer->StartTimer();
  addTimer->StartTimer();
  for (int observerIDX = 0; observerIDX < observerCount; observerIDX += eventCount)
  {
    for (int event = 0; event < eventCount; ++event)
    {
      vtkNew<vtkSimpleCommand> observer;
      volcano->AddObserver(event + 1000, observer.GetPointer());
      observers.push_back(observer.GetPointer());
    }
  }
  addTimer->StopTimer();
  invokeTimer->StartTimer();
  for (int invoke = 0; invoke < invokeCount; invoke += eventCount)
  {
    for (int event = 0; event < eventCount; ++event)
    {
      volcano->InvokeEvent(event + 1000);
    }
  }
  invokeTimer->StopTimer();
  removeTimer->StartTimer();
  observers.clear();
  volcano->Delete();
  removeTimer->StopTimer();
  totalTimer->StopTimer();
  if (VERBOSE_MODE & Details)
  {
    std::cout << "     Add: " << addTimer->GetElapsedTime() << " seconds" << "\n"
              << "     Invoke: " << invokeTimer->GetElapsedTime() << " seconds" << "\n"
              << "     Remove: " << removeTimer->GetElapsedTime() << " seconds" << "\n"
              << ">>>> Total: " << totalTimer->GetElapsedTime() << " seconds" << "\n";
  }
  return totalTimer->GetElapsedTime();
}

//------------------------------------------------------------------------------
double TestStressInvoke(int observerCount, int eventCount, int invokeCount)
{
  double meanDuration = 0.0;
  for (int i = 0; i < STRESS_COUNT; ++i)
  {
    meanDuration += StressInvoke(observerCount, eventCount, invokeCount);
  }
  meanDuration /= STRESS_COUNT;
  if (VERBOSE_MODE == CDash)
  {
    std::cout << "<DartMeasurement name=\"StressInvoke-"
              << observerCount << "-" << eventCount << "-" << invokeCount
              << "\" type=\"numeric/double\">"
              << meanDuration << "</DartMeasurement>" << std::endl;
  }
  return meanDuration;
}