Fraxinus  2023.01.05-dev+develop.0da12
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 
20 namespace cx
21 {
22 
24 {
25  mRenderClock.start();
26  mIntervalClock.start();
27  this->reset();
28 }
29 
31 {
32  mName = name;
33  mRenderClock.start();
34  mIntervalClock.start();
35  this->reset();
36 }
37 
38 void CyclicActionLogger::reset(int interval)
39 {
40  mIntervalClock.restart();
41  mInterval = interval;
42  mTiming.clear();
43 }
44 
46 {
47  this->time("outside");
48 }
49 
50 void CyclicActionLogger::time(QString id)
51 {
52  std::vector<Entry>::iterator iter;
53  for (iter=mTiming.begin(); iter!=mTiming.end(); ++iter)
54  {
55  if (iter->id!=id)
56  continue;
57  iter->time.push_back(mRenderClock.restart());
58  return;
59  }
60 
61  Entry newEntry;
62  newEntry.id = id;
63  newEntry.time.push_back(mRenderClock.restart());
64  mTiming.push_back(newEntry);
65 }
66 
70 {
71  if (!mIntervalClock.elapsed())
72  return -1;
73  double numberOfRenderings = mTiming.empty() ? 0 : mTiming.front().time.size();
74  double fps = 1000.0 * numberOfRenderings / mIntervalClock.elapsed();
75  return std::round(fps);
76 }
77 
79 {
80  return mIntervalClock.elapsed() > mInterval;
81 }
82 
84 {
85  return this->dumpStatisticsSmall();
86 // std::stringstream ss;
87 // ss << "=== " << mName << " statistics ===" << std::endl;
88 // ss << "Interval \t= " << mInterval << " ms" << std::endl;
89 // ss << "Elapsed \t= " << mIntervalClock.elapsed() << " ms" << std::endl;
90 // ss << "FPS \t= " << this->getFPS() << " frames/s" << std::endl;
91 //
92 //
93 // if (mRenderTime.empty() || mOffRenderTime.empty())
94 // return qstring_cast(ss.str());
95 //
96 // double maxRenderTime = *std::max_element(mRenderTime.begin(), mRenderTime.end());
97 // double maxOffRenderTime = *std::max_element(mOffRenderTime.begin(), mOffRenderTime.end());
98 //
99 // double meanRenderTime = std::accumulate(mRenderTime.begin(), mRenderTime.end(), 0)/mRenderTime.size();
100 // double meanOffRenderTime = std::accumulate(mOffRenderTime.begin(), mOffRenderTime.end(), 0)/mOffRenderTime.size();
101 // double meanTotalTime = meanRenderTime + meanOffRenderTime;
102 //
103 // ss << "Mean time:\t= " << meanTotalTime << " ms/frame" << std::endl;
104 // ss << std::endl;
105 // ss << "Mean times: \t"<< "render: " << meanRenderTime << "\tother: " << meanOffRenderTime << std::endl;
106 // ss << "Max times: \t" << "render: " << maxRenderTime << "\tother: " << maxOffRenderTime << std::endl;
107 // ss << std::endl;
108 //
109 // ss << "Render Times: " << mRenderTime.size() << std::endl;
110 // for (unsigned i = 0; i < mRenderTime.size(); ++i)
111 // ss << mRenderTime[i] << " ";
112 // ss << std::endl;
113 //
114 // ss << "Off Render Times: " << mOffRenderTime.size() << std::endl;
115 // for (unsigned i = 0; i < mOffRenderTime.size(); ++i)
116 // ss << mOffRenderTime[i] << " ";
117 // ss << std::endl;
118 //
119 // ss << "================================" << std::endl;
120 // ss << std::endl;
121 // return qstring_cast(ss.str());
122 }
123 
125 {
126  std::stringstream ss;
127  ss << mName << ":\t";
128  ss << "Elapsed=" << mIntervalClock.elapsed() << " ms";
129  ss << "\tFPS=" << this->getFPS() << " fps";
130 
131  if (mTiming.empty())
132  return qstring_cast(ss.str());
133 
134  QStringList meanTimes;
135  QStringList maxTimes;
136  QStringList ids;
137  double totalTime=0;
138 
139  std::vector<Entry>::iterator entry;
140  for (entry=mTiming.begin(); entry!=mTiming.end(); ++entry)
141  {
142  double maxTime = this->getMaxTime(entry->time);
143  double meanTime = this->getMeanTime(entry->time);
144  totalTime += meanTime;
145 
146  meanTimes << qstring_cast(int(meanTime));
147  maxTimes << qstring_cast(int(maxTime));
148  ids << entry->id;
149  }
150 
151  ss << QString("\t(total=%1)").arg(ids.join("+"));
152  ss << QString("\tMean:(%2=%3) ms/frame").arg(totalTime).arg(meanTimes.join("+"));
153  ss << QString("\tMax:(%1)").arg(maxTimes.join("+"));
154 
155  return qstring_cast(ss.str());
156 }
157 
158 double CyclicActionLogger::getMeanTime(std::vector<double> &time)
159 {
160  return std::accumulate(time.begin(), time.end(), 0)/time.size();
161 }
162 
163 double CyclicActionLogger::getMaxTime(std::vector<double> &time)
164 {
165  return *std::max_element(time.begin(), time.end());
166 }
167 
169 {
170  std::vector<Entry>::iterator entry = getTimingVectorIterator(id);
171  if(entry == mTiming.end())
172  {
173  reportWarning("CyclicActionLogger::getTime() unknown id: " + id);
174  return 0;
175  }
176  return getMeanTime(entry->time);
177 }
178 
179 std::vector<CyclicActionLogger::Entry>::iterator CyclicActionLogger::getTimingVectorIterator(QString id)
180 {
181  std::vector<Entry>::iterator entry;
182  for (entry=mTiming.begin(); entry!=mTiming.end(); ++entry)
183  {
184  if(entry->id == id)
185  break;
186  }
187  return entry;
188 }
189 
191 {
192  double totalTime = 0;
193  std::vector<Entry>::iterator entry;
194  for (entry=mTiming.begin(); entry!=mTiming.end(); ++entry)
195  {
196  if(entry->id != "outside")
197  {
198  double meanTime = this->getMeanTime(entry->time);
199  totalTime += meanTime;
200  }
201  }
202  return totalTime;
203 }
204 
205 } // namespace cx
QString qstring_cast(const T &val)
void reset(int interval=1000)
void begin()
start timing for this cycle
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()
Vector3D round(const Vector3D &a)
Definition: cxVector3D.cpp:75
Namespace for all CustusX production code.