Fraxinus  16.5.0-fx-rc9
An IGT application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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) 2008-2014, SINTEF Department of Medical Technology
5 All rights reserved.
6 
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions are met:
9 
10 1. Redistributions of source code must retain the above copyright notice,
11  this list of conditions and the following disclaimer.
12 
13 2. Redistributions in binary form must reproduce the above copyright notice,
14  this list of conditions and the following disclaimer in the documentation
15  and/or other materials provided with the distribution.
16 
17 3. Neither the name of the copyright holder nor the names of its contributors
18  may be used to endorse or promote products derived from this software
19  without specific prior written permission.
20 
21 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
25 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 =========================================================================*/
32 
33 #include "cxCyclicActionLogger.h"
34 #include <numeric>
35 #include <sstream>
36 #include <QStringList>
37 #include "cxTypeConversions.h"
38 #include <cmath>
39 #include "cxLogger.h"
40 #include "cxMathUtils.h"
41 
42 namespace cx
43 {
44 
46 {
47  mRenderClock.start();
48  mIntervalClock.start();
49  this->reset();
50 }
51 
53 {
54  mName = name;
55  mRenderClock.start();
56  mIntervalClock.start();
57  this->reset();
58 }
59 
60 void CyclicActionLogger::reset(int interval)
61 {
62  mIntervalClock.restart();
63  mInterval = interval;
64  mTiming.clear();
65 }
66 
68 {
69  this->time("outside");
70 }
71 
72 void CyclicActionLogger::time(QString id)
73 {
74  std::vector<Entry>::iterator iter;
75  for (iter=mTiming.begin(); iter!=mTiming.end(); ++iter)
76  {
77  if (iter->id!=id)
78  continue;
79  iter->time.push_back(mRenderClock.restart());
80  return;
81  }
82 
83  Entry newEntry;
84  newEntry.id = id;
85  newEntry.time.push_back(mRenderClock.restart());
86  mTiming.push_back(newEntry);
87 }
88 
92 {
93  if (!mIntervalClock.elapsed())
94  return -1;
95  double numberOfRenderings = mTiming.empty() ? 0 : mTiming.front().time.size();
96  double fps = 1000.0 * numberOfRenderings / mIntervalClock.elapsed();
97  return roundAwayFromZero(fps);
98 }
99 
101 {
102  return mIntervalClock.elapsed() > mInterval;
103 }
104 
106 {
107  return this->dumpStatisticsSmall();
108 // std::stringstream ss;
109 // ss << "=== " << mName << " statistics ===" << std::endl;
110 // ss << "Interval \t= " << mInterval << " ms" << std::endl;
111 // ss << "Elapsed \t= " << mIntervalClock.elapsed() << " ms" << std::endl;
112 // ss << "FPS \t= " << this->getFPS() << " frames/s" << std::endl;
113 //
114 //
115 // if (mRenderTime.empty() || mOffRenderTime.empty())
116 // return qstring_cast(ss.str());
117 //
118 // double maxRenderTime = *std::max_element(mRenderTime.begin(), mRenderTime.end());
119 // double maxOffRenderTime = *std::max_element(mOffRenderTime.begin(), mOffRenderTime.end());
120 //
121 // double meanRenderTime = std::accumulate(mRenderTime.begin(), mRenderTime.end(), 0)/mRenderTime.size();
122 // double meanOffRenderTime = std::accumulate(mOffRenderTime.begin(), mOffRenderTime.end(), 0)/mOffRenderTime.size();
123 // double meanTotalTime = meanRenderTime + meanOffRenderTime;
124 //
125 // ss << "Mean time:\t= " << meanTotalTime << " ms/frame" << std::endl;
126 // ss << std::endl;
127 // ss << "Mean times: \t"<< "render: " << meanRenderTime << "\tother: " << meanOffRenderTime << std::endl;
128 // ss << "Max times: \t" << "render: " << maxRenderTime << "\tother: " << maxOffRenderTime << std::endl;
129 // ss << std::endl;
130 //
131 // ss << "Render Times: " << mRenderTime.size() << std::endl;
132 // for (unsigned i = 0; i < mRenderTime.size(); ++i)
133 // ss << mRenderTime[i] << " ";
134 // ss << std::endl;
135 //
136 // ss << "Off Render Times: " << mOffRenderTime.size() << std::endl;
137 // for (unsigned i = 0; i < mOffRenderTime.size(); ++i)
138 // ss << mOffRenderTime[i] << " ";
139 // ss << std::endl;
140 //
141 // ss << "================================" << std::endl;
142 // ss << std::endl;
143 // return qstring_cast(ss.str());
144 }
145 
147 {
148  std::stringstream ss;
149  ss << mName << ":\t";
150  ss << "Elapsed=" << mIntervalClock.elapsed() << " ms";
151  ss << "\tFPS=" << this->getFPS() << " fps";
152 
153  if (mTiming.empty())
154  return qstring_cast(ss.str());
155 
156  QStringList meanTimes;
157  QStringList maxTimes;
158  QStringList ids;
159  double totalTime=0;
160 
161  std::vector<Entry>::iterator entry;
162  for (entry=mTiming.begin(); entry!=mTiming.end(); ++entry)
163  {
164  double maxTime = this->getMaxTime(entry->time);
165  double meanTime = this->getMeanTime(entry->time);
166  totalTime += meanTime;
167 
168  meanTimes << qstring_cast(int(meanTime));
169  maxTimes << qstring_cast(int(maxTime));
170  ids << entry->id;
171  }
172 
173  ss << QString("\t(total=%1)").arg(ids.join("+"));
174  ss << QString("\tMean:(%2=%3) ms/frame").arg(totalTime).arg(meanTimes.join("+"));
175  ss << QString("\tMax:(%1)").arg(maxTimes.join("+"));
176 
177  return qstring_cast(ss.str());
178 }
179 
180 double CyclicActionLogger::getMeanTime(std::vector<double> &time)
181 {
182  return std::accumulate(time.begin(), time.end(), 0)/time.size();
183 }
184 
185 double CyclicActionLogger::getMaxTime(std::vector<double> &time)
186 {
187  return *std::max_element(time.begin(), time.end());
188 }
189 
191 {
192  std::vector<Entry>::iterator entry = getTimingVectorIterator(id);
193  if(entry == mTiming.end())
194  {
195  reportWarning("CyclicActionLogger::getTime() unknown id: " + id);
196  return 0;
197  }
198  return getMeanTime(entry->time);
199 }
200 
201 std::vector<CyclicActionLogger::Entry>::iterator CyclicActionLogger::getTimingVectorIterator(QString id)
202 {
203  std::vector<Entry>::iterator entry;
204  for (entry=mTiming.begin(); entry!=mTiming.end(); ++entry)
205  {
206  if(entry->id == id)
207  break;
208  }
209  return entry;
210 }
211 
213 {
214  double totalTime = 0;
215  std::vector<Entry>::iterator entry;
216  for (entry=mTiming.begin(); entry!=mTiming.end(); ++entry)
217  {
218  if(entry->id != "outside")
219  {
220  double meanTime = this->getMeanTime(entry->time);
221  totalTime += meanTime;
222  }
223  }
224  return totalTime;
225 }
226 
227 } // 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:35
void reportWarning(QString msg)
Definition: cxLogger.cpp:91
int getTotalLoggedTime()
Total time contained in entered id's (id outside is not counted)
void time(QString id)
store time from begin or last time()