FairRoot
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
test.cxx
Go to the documentation of this file.
1 /********************************************************************************
2  * Copyright (C) 2014 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH *
3  * *
4  * This software is distributed under the terms of the *
5  * GNU Lesser General Public Licence (LGPL) version 3, *
6  * copied verbatim in the file "LICENSE" *
7  ********************************************************************************/
8 #include "FairLogger.h"
9 
10 #include <TStopwatch.h>
11 #include <algorithm>
12 #include <cstdio>
13 #include <cstdlib>
14 #include <iostream>
15 #include <numeric>
16 #include <unistd.h> //for sleep
17 #include <vector>
18 
19 // std::cout<<"Value: "<<val<<std::endl;
20 
21 // gLogger->Info(MESSAGE_ORIGIN, "Value: %i", val);
22 
23 /*
24 std::cout<<"Das sollte schwarz sein."<<std::endl;
25 */
26 
27 // LOG(info) << "Value: "<< val <<" ";
28 
29 /*
30 std::cout<<"Das sollte wieder schwarz sein."<<std::endl;
31 LOG_IF(WarningLog, true) << "Das sollte man sehen.";
32 LOG_IF(WarningLog, false) << "Das sollte man nicht sehen.";
33 LOG_IF(WarningLog, i%2) << "Jedes zweite mal.";
34 std::cout<<"Das sollte wieder schwarz sein."<<std::endl;
35 */
36 // }
37 
38 void logToPrintf(int iterations)
39 {
40  for (int i = 0; i < iterations; i++) {
41  Int_t val = i * i - (i - 1);
42  printf("Value: %i\n", val);
43  }
44 }
45 
46 void logToCout(int iterations)
47 {
48  for (int i = 0; i < iterations; i++) {
49  Int_t val = i * i - (i - 1);
50  std::cout << "Value: " << val << std::endl;
51  }
52 }
53 
54 void logToCerr(int iterations)
55 {
56  for (int i = 0; i < iterations; i++) {
57  Int_t val = i * i - (i - 1);
58  std::cerr << "Value: " << val << std::endl;
59  }
60 }
61 
62 void logToLoggerPrintf(int iterations)
63 {
64 
65  for (int i = 0; i < iterations; i++) {
66  Int_t val = i * i - (i - 1);
67  gLogger->Info(MESSAGE_ORIGIN, "Value: %i", val);
68  }
69 }
70 
71 void logToLoggerCout(int iterations)
72 {
73  for (int i = 0; i < iterations; i++) {
74  Int_t val = i * i - (i - 1);
75  LOG(info) << "Value: " << val << " ";
76  }
77 }
78 
79 void logToLoggerCoutTest(int iterations)
80 {
81  for (int i = 0; i < iterations; i++) {
82  Int_t val = i * i - (i - 1);
83  LOG(info) << "Value: " << val << " ";
84  LOG(debug) << "Value: " << val << " ";
85  }
86 }
87 
88 void logTo(int)
89 {
90 
91  /*
92  for (int i=0; i < iterations; i++) {
93  Int_t val = i * i - (i-1);
94  }
95 */
96 }
97 
98 void setSyncOutput(bool decision) { std::ios_base::sync_with_stdio(decision); }
99 
101 {
102  std::cerr << "Please call the program with the number of the test to run." << std::endl;
103  std::cerr << "The following tests are implemented:" << std::endl;
104  std::cerr << " 1. printf" << std::endl;
105  std::cerr << " 2. cout syncronised" << std::endl;
106  std::cerr << " 3. cout not synchronised" << std::endl;
107  std::cerr << " 4. cerr syncronised" << std::endl;
108  std::cerr << " 5. cerr not synchronised" << std::endl;
109  std::cerr << " 6. FairLogger printf implemetation" << std::endl;
110  std::cerr << " 7. FairLogger streamer implemetation" << std::endl;
111  exit(-1);
112 }
113 
114 void doCalculation(std::vector<Double_t> rtime, std::vector<Double_t> ctime)
115 {
116  // Remove smallest and largest value
117  sort(rtime.begin(), rtime.end());
118  rtime.erase(rtime.begin());
119  rtime.pop_back();
120  sort(ctime.begin(), ctime.end());
121  ctime.erase(ctime.begin());
122  ctime.pop_back();
123 
124  Double_t ctime_sum = 0.0;
125  Double_t ctime_avg = 0.0;
126  Double_t rtime_sum = 0.0;
127  Double_t rtime_avg = 0.0;
128  for (unsigned int i = 0; i < rtime.size(); i++) {
129  rtime_sum += rtime.at(i);
130  ctime_sum += ctime.at(i);
131  }
132 
133  rtime_avg = rtime_sum / rtime.size();
134  ctime_avg = ctime_sum / ctime.size();
135 
136  Double_t ctime_min = *min_element(ctime.begin(), ctime.end());
137  Double_t ctime_max = *max_element(ctime.begin(), ctime.end());
138 
139  // Double_t rtime_sum = accumulate(rtime.begin(), rtime.end(), 0);
140  // Double_t rtime_avg = rtime_sum/rtime.size();
141  Double_t rtime_min = *min_element(rtime.begin(), rtime.end());
142  Double_t rtime_max = *max_element(rtime.begin(), rtime.end());
143  std::cout << "Discarding the lowest and highest value the" << std::endl;
144  std::cout << "average of " << rtime.size() << " measurements is calculated." << std::endl;
145  std::cout << "Real time (min, average, max): " << rtime_min << ", " << rtime_avg << ", " << rtime_max << std::endl;
146  std::cout << "CPU time (min, average, max): " << ctime_min << ", " << ctime_avg << ", " << ctime_max << std::endl;
147 }
148 
149 int main(int argc, char* argv[])
150 {
151 
152  // gLogger->SetLogScreenLevel("BLA");
153  // gLogger->SetLogToFile(false);
154  // gLogger->SetLogToScreen(false);
155  // gLogger->Fatal(MESSAGE_ORIGIN, "This is a fatal message.");
156  // LOG(info) << "This message should never be printed.";
157  // LOG(fatal) << "This is a fatal message";
158  // exit(-42);
159 
160  int iterations = 1000000;
161  int time_measurements = 12;
162 
163  /*
164  for (int i=0; i < iterations; i++) {
165  Int_t val = i * i - (i-1);
166  printf("Value: %i\n",val);
167  }
168  exit(0);
169  */
170 
171  if (1 == argc) {
172  logToPrintf(iterations);
173  std::cout << "Using printf for output" << std::endl;
174  exit(0);
175  }
176 
177  if (2 < argc) {
179  }
180 
181  int x = atoi(argv[1]);
182 
183  TStopwatch timer;
184  std::vector<Double_t> rtime;
185  std::vector<Double_t> ctime;
186 
187  switch (x) {
188  case 1:
189  for (int meas = 0; meas < time_measurements; meas++) {
190  timer.Start();
191  logToPrintf(iterations);
192  timer.Stop();
193  rtime.push_back(timer.RealTime());
194  ctime.push_back(timer.CpuTime());
195  sleep(4);
196  }
197  doCalculation(rtime, ctime);
198  std::cout << "Using printf for output" << std::endl;
199  break;
200  case 2:
201  for (int meas = 0; meas < time_measurements; meas++) {
202  timer.Start();
203  logToCout(iterations);
204  timer.Stop();
205  rtime.push_back(timer.RealTime());
206  ctime.push_back(timer.CpuTime());
207  sleep(4);
208  }
209  doCalculation(rtime, ctime);
210  std::cout << "Using syncronised cout for output" << std::endl;
211  break;
212  case 3:
213  setSyncOutput(false);
214  for (int meas = 0; meas < time_measurements; meas++) {
215  timer.Start();
216  logToCout(iterations);
217  timer.Stop();
218  rtime.push_back(timer.RealTime());
219  ctime.push_back(timer.CpuTime());
220  sleep(4);
221  }
222  doCalculation(rtime, ctime);
223  std::cout << "Using non syncronised cout for output" << std::endl;
224  break;
225  case 4:
226  for (int meas = 0; meas < time_measurements; meas++) {
227  timer.Start();
228  logToCerr(iterations);
229  timer.Stop();
230  rtime.push_back(timer.RealTime());
231  ctime.push_back(timer.CpuTime());
232  sleep(4);
233  }
234  doCalculation(rtime, ctime);
235  std::cout << "Using syncronised cerr for output" << std::endl;
236  break;
237  case 5:
238  setSyncOutput(false);
239  for (int meas = 0; meas < time_measurements; meas++) {
240  timer.Start();
241  logToCerr(iterations);
242  timer.Stop();
243  rtime.push_back(timer.RealTime());
244  ctime.push_back(timer.CpuTime());
245  sleep(4);
246  }
247  doCalculation(rtime, ctime);
248  std::cout << "Using non syncronised cerr for output" << std::endl;
249  break;
250  case 6:
251  for (int meas = 0; meas < time_measurements; meas++) {
252  timer.Start();
253  logToLoggerPrintf(iterations);
254  timer.Stop();
255  rtime.push_back(timer.RealTime());
256  ctime.push_back(timer.CpuTime());
257  sleep(4);
258  }
259  doCalculation(rtime, ctime);
260  std::cout << "Using logger printf output" << std::endl;
261  break;
262  case 7:
263  for (int meas = 0; meas < time_measurements; meas++) {
264  timer.Start();
265  logToLoggerCout(iterations);
266  timer.Stop();
267  rtime.push_back(timer.RealTime());
268  ctime.push_back(timer.CpuTime());
269  sleep(4);
270  }
271  doCalculation(rtime, ctime);
272  std::cout << "Using logger cout output" << std::endl;
273  break;
274  case 8:
275  setSyncOutput(false);
276  for (int meas = 0; meas < time_measurements; meas++) {
277  timer.Start();
278  logToLoggerPrintf(iterations);
279  timer.Stop();
280  rtime.push_back(timer.RealTime());
281  ctime.push_back(timer.CpuTime());
282  sleep(4);
283  }
284  doCalculation(rtime, ctime);
285  std::cout << "Using logger printf output" << std::endl;
286  break;
287  case 9:
288  setSyncOutput(false);
289  for (int meas = 0; meas < time_measurements; meas++) {
290  timer.Start();
291  logToLoggerCout(iterations);
292  timer.Stop();
293  rtime.push_back(timer.RealTime());
294  ctime.push_back(timer.CpuTime());
295  sleep(4);
296  }
297  doCalculation(rtime, ctime);
298  std::cout << "Using logger cout output" << std::endl;
299  break;
300  case 10:
301  setSyncOutput(false);
302  for (int meas = 0; meas < time_measurements; meas++) {
303  timer.Start();
304  logToLoggerCoutTest(iterations / 2);
305  timer.Stop();
306  rtime.push_back(timer.RealTime());
307  ctime.push_back(timer.CpuTime());
308  sleep(4);
309  }
310  doCalculation(rtime, ctime);
311  std::cout << "Using logger cout output" << std::endl;
312  break;
313  default:
314  std::cout << "Option Unknown" << std::endl;
316  }
317 
318  // std::ios_base::sync_with_stdio(false);
319  // gLogger->SetColoredLog(false);
320  // gLogger->SetLogToFile(true);
321  // gLogger->SetLogToScreen(false);
322 }
void logTo(int)
Definition: test.cxx:88
void setSyncOutput(bool decision)
Definition: test.cxx:98
void logToLoggerCoutTest(int iterations)
Definition: test.cxx:79
void PrintHelpAndExit()
Definition: test.cxx:100
void logToCout(int iterations)
Definition: test.cxx:46
void logToLoggerCout(int iterations)
Definition: test.cxx:71
#define MESSAGE_ORIGIN
Definition: FairLogger.h:24
int main(void)
#define gLogger
Definition: FairLogger.h:156
void doCalculation(std::vector< Double_t > rtime, std::vector< Double_t > ctime)
Definition: test.cxx:114
void logToPrintf(int iterations)
Definition: test.cxx:38
void logToCerr(int iterations)
Definition: test.cxx:54
void logToLoggerPrintf(int iterations)
Definition: test.cxx:62