Fraxinus  18.10
An IGT application
cxCyclicActionLogger.cpp
Go to the documentation of this file.
1 /*=========================================================================
2 This file is part of CustusX, an Image Guided Therapy Application.
3 
4 Copyright (c) SINTEF Department of Medical Technology.
5 All rights reserved.
6 
7 CustusX is released under a BSD 3-Clause license.
8 
9 See Lisence.txt (https://github.com/SINTEFMedtek/CustusX/blob/master/License.txt) for details.
10 =========================================================================*/
11 
12 #include "cxCyclicActionLogger.h"
13 #include <numeric>
14 #include <sstream>
15 #include <QStringList>
16 #include "cxTypeConversions.h"
17 #include <cmath>
18 #include "cxLogger.h"
19 #include "cxMathUtils.h"
20 
21 namespace cx
22 {
23 
25 {
26  mRenderClock.start();
27  mIntervalClock.start();
28  this->reset();
29 }
30 
32 {
33  mName = name;
34  mRenderClock.start();
35  mIntervalClock.start();
36  this->reset();
37 }
38 
39 void CyclicActionLogger::reset(int interval)
40 {
41  mIntervalClock.restart();
42  mInterval = interval;
43  mTiming.clear();
44 }
45 
47 {
48  this->time("outside");
49 }
50 
51 void CyclicActionLogger::time(QString id)
52 {
53  std::vector<Entry>::iterator iter;
54  for (iter=mTiming.begin(); iter!=mTiming.end(); ++iter)
55  {
56  if (iter->id!=id)
57  continue;
58  iter->time.push_back(mRenderClock.restart());
59  return;
60  }
61 
62  Entry newEntry;
63  newEntry.id = id;
64  newEntry.time.push_back(mRenderClock.restart());
65  mTiming.push_back(newEntry);
66 }
67 
71 {
72  if (!mIntervalClock.elapsed())
73  return -1;
74  double numberOfRenderings = mTiming.empty() ? 0 : mTiming.front().time.size();
75  double fps = 1000.0 * numberOfRenderings / mIntervalClock.elapsed();
76  return roundAwayFromZero(fps);
77 }
78 
80 {
81  return mIntervalClock.elapsed() > mInterval;
82 }
83 
85 {
86  return this->dumpStatisticsSmall();
87 // std::stringstream ss;
88 // ss << "=== " << mName << " statistics ===" << std::endl;
89 // ss << "Interval \t= " << mInterval << " ms" << std::endl;
90 // ss << "Elapsed \t= " << mIntervalClock.elapsed() << " ms" << std::endl;
91 // ss << "FPS \t= " << this->getFPS() << " frames/s" << std::endl;
92 //
93 //
94 // if (mRenderTime.empty() || mOffRenderTime.empty())
95 // return qstring_cast(ss.str());
96 //
97 // double maxRenderTime = *std::max_element(mRenderTime.begin(), mRenderTime.end());
98 // double maxOffRenderTime = *std::max_element(mOffRenderTime.begin(), mOffRenderTime.end());
99 //
100 // double meanRenderTime = std::accumulate(mRenderTime.begin(), mRenderTime.end(), 0)/mRenderTime.size();
101 // double meanOffRenderTime = std::accumulate(mOffRenderTime.begin(), mOffRenderTime.end(), 0)/mOffRenderTime.size();
102 // double meanTotalTime = meanRenderTime + meanOffRenderTime;
103 //
104 // ss << "Mean time:\t= " << meanTotalTime << " ms/frame" << std::endl;
105 // ss << std::endl;
106 // ss << "Mean times: \t"<< "render: " << meanRenderTime << "\tother: " << meanOffRenderTime << std::endl;
107 // ss << "Max times: \t" << "render: " << maxRenderTime << "\tother: " << maxOffRenderTime << std::endl;
108 // ss << std::endl;
109 //
110 // ss << "Render Times: " << mRenderTime.size() << std::endl;
111 // for (unsigned i = 0; i < mRenderTime.size(); ++i)
112 // ss << mRenderTime[i] << " ";
113 // ss << std::endl;
114 //
115 // ss << "Off Render Times: " << mOffRenderTime.size() << std::endl;
116 // for (unsigned i = 0; i < mOffRenderTime.size(); ++i)
117 // ss << mOffRenderTime[i] << " ";
118 // ss << std::endl;
119 //
120 // ss << "================================" << std::endl;
121 // ss << std::endl;
122 // return qstring_cast(ss.str());
123 }
124 
126 {
127  std::stringstream ss;
128  ss << mName << ":\t";
129  ss << "Elapsed=" << mIntervalClock.elapsed() << " ms";
130  ss << "\tFPS=" << this->getFPS() << " fps";
131 
132  if (mTiming.empty())
133  return qstring_cast(ss.str());
134 
135  QStringList meanTimes;
136  QStringList maxTimes;
137  QStringList ids;
138  double totalTime=0;
139 
140  std::vector<Entry>::iterator entry;
141  for (entry=mTiming.begin(); entry!=mTiming.end(); ++entry)
142  {
143  double maxTime = this->getMaxTime(entry->time);
144  double meanTime = this->getMeanTime(entry->time);
145  totalTime += meanTime;
146 
147  meanTimes << qstring_cast(int(meanTime));
148  maxTimes << qstring_cast(int(maxTime));
149  ids << entry->id;
150  }
151 
152  ss << QString("\t(total=%1)").arg(ids.join("+"));
153  ss << QString("\tMean:(%2=%3) ms/frame").arg(totalTime).arg(meanTimes.join("+"));
154  ss << QString("\tMax:(%1)").arg(maxTimes.join("+"));
155 
156  return qstring_cast(ss.str());
157 }
158 
159 double CyclicActionLogger::getMeanTime(std::vector<double> &time)
160 {
161  return std::accumulate(time.begin(), time.end(), 0)/time.size();
162 }
163 
164 double CyclicActionLogger::getMaxTime(std::vector<double> &time)
165 {
166  return *std::max_element(time.begin(), time.end());
167 }
168 
170 {
171  std::vector<Entry>::iterator entry = getTimingVectorIterator(id);
172  if(entry == mTiming.end())
173  {
174  reportWarning("CyclicActionLogger::getTime() unknown id: " + id);
175  return 0;
176  }
177  return getMeanTime(entry->time);
178 }
179 
180 std::vector<CyclicActionLogger::Entry>::iterator CyclicActionLogger::getTimingVectorIterator(QString id)
181 {
182  std::vector<Entry>::iterator entry;
183  for (entry=mTiming.begin(); entry!=mTiming.end(); ++entry)
184  {
185  if(entry->id == id)
186  break;
187  }
188  return entry;
189 }
190 
192 {
193  double totalTime = 0;
194  std::vector<Entry>::iterator entry;
195  for (entry=mTiming.begin(); entry!=mTiming.end(); ++entry)
196  {
197  if(entry->id != "outside")
198  {
199  double meanTime = this->getMeanTime(entry->time);
200  totalTime += meanTime;
201  }
202  }
203  return totalTime;
204 }
205 
206 } // namespace cx
QString qstring_cast(const T &val)
void reset(int interval=1000)
void begin()
start timing for this cycle
double roundAwayFromZero(double val)
Definition: cxMathUtils.cpp:21
void reportWarning(QString msg)
Definition: cxLogger.cpp:70
int getTotalLoggedTime()
Total time contained in entered id&#39;s (id outside is not counted)
void time(QString id)
store time from begin or last time()
Namespace for all CustusX production code.