rambrain
tester.cpp
Go to the documentation of this file.
1 /* rambrain - a dynamical physical memory extender
2  * Copyright (C) 2015 M. Imgrund, A. Arth
3  * mimgrund (at) mpifr-bonn.mpg.de
4  * arth (at) usm.uni-muenchen.de
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program. If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #include "tester.h"
21 #include "managedMemory.h"
22 tester::tester ( const char *name ) : name ( name )
23 {
25 }
26 
28 {
29 }
30 
31 void tester::addParameter ( char *param )
32 {
33  parameters.push_back ( param );
34 }
35 
37 {
38 #ifdef LOGSTATS
40 #endif
41  std::chrono::high_resolution_clock::time_point t = std::chrono::high_resolution_clock::now();
42  timeMeasures.back().push_back ( t );
43 }
44 
45 void tester::addExternalTime ( chrono::duration< double > mdur )
46 {
47  if ( timeMeasures.back().size() == 0 ) {
48  std::chrono::high_resolution_clock::time_point t = std::chrono::high_resolution_clock::now();
49  timeMeasures.back().push_back ( t );
50  }
51 
52  std::chrono::high_resolution_clock::time_point t = timeMeasures.back().back();
53  t += std::chrono::duration_cast< std::chrono::milliseconds > ( mdur );
54  timeMeasures.back().push_back ( t );
55 
56 }
57 
58 
59 void tester::addComment ( const char *comment )
60 {
61  this->comment = std::string ( comment );
62 }
63 
64 void tester::setSeed ( unsigned int seed )
65 {
66  seeds.back() = seed;
67  seeded.back() = true;
68  srand48 ( seed );
69  srand ( seed );
70 
71  std::cout << "I am running with a seed of " << seed << std::endl;
72 }
73 
74 int tester::random ( int max ) const
75 {
76  return static_cast<uint64_t> ( rand() ) * max / RAND_MAX;
77 }
78 
79 uint64_t tester::random ( uint64_t max ) const
80 {
81  return random ( static_cast<double> ( max ) );
82 }
83 
84 double tester::random ( double max ) const
85 {
86  return drand48() * max;
87 }
88 
90 {
91  timeMeasures.push_back ( std::vector<std::chrono::high_resolution_clock::time_point>() );
92 }
93 
95 {
96  seeds.push_back ( 0u );
97  seeded.push_back ( false );
98 }
99 
101 {
102  if ( std::string ( name ).empty() ) {
103  std::cerr << "Can not write to file without file name" << std::endl;
104  return;
105  }
106 
107  std::stringstream fileName;
108  fileName << name;
109  for ( auto it = parameters.begin(); it != parameters.end(); ++it ) {
110  fileName << "#" << *it;
111  }
112  std::ofstream out ( fileName.str(), std::ofstream::out );
113 
114  out << "# " << name;
115  for ( auto it = parameters.begin(); it != parameters.end(); ++it ) {
116  out << " " << *it;
117  }
118  out << std::endl;
119 
120  const int cyclesCount = timeMeasures.size();
121  out << "# " << cyclesCount << " cycles run for average" << std::endl;
122 
123  bool firstSeed = true;
124  for ( size_t i = 0; i < seeded.size(); ++i ) {
125  if ( seeded[i] ) {
126  if ( firstSeed ) {
127  out << "# Random seeds: ";
128  firstSeed = false;
129  }
130  out << seeds[i] << " ";
131  } else {
132  if ( ! firstSeed ) {
133  out << "* ";
134  }
135  }
136  }
137  if ( !firstSeed ) {
138  out << std::endl;
139  }
140 
141  out << "# Columns: (Time [ms], Start [ms], End [ms], Percentage) per repetition run and afterwards for average (without Start, End)" << std::endl;
142 
143  if ( ! comment.empty() ) {
144  out << "# " << comment << std::endl;
145  }
146 
147  const int timesCount = timeMeasures.front().size() - 1;
148  int64_t durations[timesCount][cyclesCount], starts[timesCount][cyclesCount], ends[timesCount][cyclesCount];
149  double percentages[timesCount][cyclesCount];
150 
151  int cycle = 0, time;
152  for ( auto repIt = timeMeasures.begin(); repIt != timeMeasures.end(); ++repIt, ++cycle ) {
153  int64_t totms = std::chrono::duration_cast<std::chrono::milliseconds> ( repIt->back() - repIt->front() ).count();
154 
155  time = 0;
156  for ( auto it = repIt->begin(), jt = repIt->begin() + 1; it != repIt->end() && jt != repIt->end(); ++it, ++jt, ++time ) {
157  starts[time][cycle] = std::chrono::duration_cast<std::chrono::milliseconds> ( it->time_since_epoch() ).count();
158  ends[time][cycle] = std::chrono::duration_cast<std::chrono::milliseconds> ( jt->time_since_epoch() ).count();
159  durations[time][cycle] = std::chrono::duration_cast<std::chrono::milliseconds> ( ( *jt ) - ( *it ) ).count();
160  percentages[time][cycle] = 100.0 * durations[time][cycle] / totms;
161  }
162  }
163 
164  int64_t avgTime;
165  double avgPercentage;
166  for ( time = 0; time < timesCount; ++time ) {
167 
168  avgTime = 0;
169  avgPercentage = 0.0;
170 
171  for ( cycle = 0; cycle < cyclesCount; ++cycle ) {
172  avgTime += durations[time][cycle];
173  avgPercentage += percentages[time][cycle];
174 
175  out << durations[time][cycle] << "\t" << starts[time][cycle] << "\t" << ends[time][cycle] << "\t" << percentages[time][cycle] << "\t";
176  }
177  avgTime /= cyclesCount;
178  avgPercentage /= cyclesCount;
179 
180  out << avgTime << "\t" << avgPercentage << std::endl;
181  }
182 
183  out << std::flush;
184 }
185 
186 std::vector<int64_t> tester::getDurationsForCurrentCycle() const
187 {
188  std::vector<int64_t> durations;
189 
190  for ( auto it = timeMeasures.back().begin(), jt = timeMeasures.back().begin() + 1; it != timeMeasures.back().end() && jt != timeMeasures.back().end(); ++it, ++jt ) {
191  durations.push_back ( std::chrono::duration_cast<std::chrono::milliseconds> ( ( *jt ) - ( *it ) ).count() );
192  }
193 
194  return durations;
195 }
196 
std::string comment
Definition: tester.h:118
void addTimeMeasurement()
Saves the current timestamp.
Definition: tester.cpp:36
void addComment(const char *comment)
Simple setter.
Definition: tester.cpp:59
static void sigswapstats(int sig)
static binding that will print out some stats. Compile with cmake -DSWAPSTATS=on and send process SIG...
void addExternalTime(std::chrono::duration< double >)
Add a duration to the timing list.
Definition: tester.cpp:45
tester(const char *name="")
tester Create a new tester
Definition: tester.cpp:22
void startNewRNGCycle()
Starts a new cycle of random numbers.
Definition: tester.cpp:94
const char * name
Definition: tester.h:115
void setSeed(unsigned int seed=time(NULL))
Set a new seed for random number generation.
Definition: tester.cpp:64
int random(int max) const
Get a random number (integer)
Definition: tester.cpp:74
void startNewTimeCycle()
Starts a new cycle of time measurements.
Definition: tester.cpp:89
std::vector< int64_t > getDurationsForCurrentCycle() const
Take the current cycle of time measurements and calculate all durations in betwen.
Definition: tester.cpp:186
std::vector< unsigned int > seeds
Definition: tester.h:119
std::vector< std::vector< std::chrono::high_resolution_clock::time_point > > timeMeasures
Definition: tester.h:117
void writeToFile()
Write all collected information to file.
Definition: tester.cpp:100
~tester()
Cleans up and destroys the tester.
Definition: tester.cpp:27
std::vector< bool > seeded
Definition: tester.h:120
void addParameter(char *param)
Add a new parameter to the list of parameters.
Definition: tester.cpp:31
std::vector< char * > parameters
Definition: tester.h:116