alaCarte Maps
Renderer for OpenStreetMap tiles
statistic.cpp
Go to the documentation of this file.
1 
22 #include <fstream>
23 
24 #include "utils/statistic.hpp"
26 
27 
28 shared_ptr<Statistic> Statistic::instance;
29 
31 {
32  return stopTime[i] - startTime[i];
33 }
34 
35 Statistic::Statistic(const shared_ptr<Configuration>& config)
36  : config(config)
37 {
38 
39  for (int i = 0; i < Component::Size; i++)
40  {
41  std::fill_n(componentAvgs[i].count, 19, 0);
42  std::fill_n(componentAvgs[i].average, 19, 0);
43  }
44 }
45 
47 {
49  writeToFile(config->get<string>(opt::server::performance_log).c_str());
50 }
51 
52 shared_ptr<Statistic::JobMeasurement> Statistic::startNewMeasurement(const string& stylesheet, int zoom)
53 {
54 #ifdef Statistic_Activated
55  shared_ptr<Statistic::JobMeasurement> jm = boost::make_shared<JobMeasurement>(stylesheet, zoom);
56  jm->jobStartTime = boost::posix_time::microsec_clock::universal_time();
57  return jm;
58 #endif
59  return boost::make_shared<JobMeasurement>();
60 }
61 void Statistic::setStats(shared_ptr<Statistic::JobMeasurement>& job,
62  unsigned int nodes,
63  unsigned int ways,
64  unsigned int relations)
65 {
66 #ifdef Statistic_Activated
67  job->nodes = nodes;
68  job->ways = ways;
69  job->relations = relations;
70 #endif
71 }
72 void Statistic::start(shared_ptr<Statistic::JobMeasurement>& job, Component component) const
73 {
74 #ifdef Statistic_Activated
75  job->startTime[component] = boost::posix_time::microsec_clock::universal_time();
76 #endif
77 }
78 
79 void Statistic::stop(shared_ptr<Statistic::JobMeasurement>& job, Component component) const
80 {
81 #ifdef Statistic_Activated
82  job->stopTime[component] = boost::posix_time::microsec_clock::universal_time();
83  job->stopped[component] = true;
84 #endif
85 }
87 {
88 #ifdef Statistic_Activated
89  std::stringstream ss;
90  for(int c = 0; c < Component::Size; c++) {
91  ss << "\n" << componentToName((Component)c) << ": \n";
92  for(int z = 0; z < 19; z++) {
93  uint32_t n = componentAvgs[c].count[z];
94  if (n == 0)
95  continue;
96  float avg = componentAvgs[c].average[z];
97  ss << n << " Measurements on Zoom: " << z << ", average: ";
98  if(avg >= 1000) {
99  ss << avg/1000.0 << " ms\n";
100  } else {
101  ss << avg << " µs\n";
102  }
103  }
104  }
105 
106  LOG_SEV(stat_log, info) << ss.str();
107 #endif
108 }
109 
110 void Statistic::finished(shared_ptr<Statistic::JobMeasurement>& job)
111 {
112 #ifdef Statistic_Activated
113  avgLock.lock();
114  uint32_t n;
115  for(int c = 0; c < Component::Size; c++) {
116  if (!job->stopped[c])
117  continue;
118 
120 
121  n = m.count[job->zoom];
122  m.average[job->zoom] *= n/(float)(n + 1.0f);
123  m.average[job->zoom] += job->getDuration(c).total_microseconds() / (float)(n + 1.0f);
124  m.count[job->zoom]++;
125  }
126  avgLock.unlock();
127 
129  return;
130 
131  boost::mutex::scoped_lock scoped(bufferLock);
132 
133  measurementsBuffer.push_back(job);
134  if(measurementsBuffer.size() > 100)
135  {
136  writeToFile(config->get<string>(opt::server::performance_log).c_str());
137  measurementsBuffer.clear();
138  }
139 #endif
140 }
141 
142 void Statistic::writeToFile(const char* filename)
143 {
144  std::ofstream file;
145  file.open(filename, std::fstream::app);
146 
147  for (auto& job : measurementsBuffer) {
148  std::stringstream ss;
149  ss << "JobStart " << job->jobStartTime.time_of_day().total_microseconds() << " ";
150  for(int i = 0; i < Component::Size; i++) {
151  if(job->stopped[i])
152  ss << componentToName((Component)i) << " " << job->getDuration(i).total_microseconds() << " ";
153  }
154  ss << "Stylesheet " << job->stylesheet << " Zoom " << job->zoom << " Nodes " << job->nodes << " Ways " << job->ways << " Relations " << job->relations << "\n";
155  file << ss.str();
156  }
157 
158  file.close();
159 }
160 
161 string Statistic::componentToName(Component component) const
162 {
163  switch(component)
164  {
165  case Cache: return "Cache";
166  case ComputeRect: return "ComputeRect";
167  case GeoNodes: return "GeoNodes";
168  case GeoWays: return "GeoWays";
169  case GeoRelation: return "GeoRelation";
170  case StylesheetMatch: return "StylesheetMatch";
171  case Renderer: return "Renderer";
172  case GeoContainsData: return "GeoContainsData";
173  case Slicing: return "Slicing";
174  default:
175  assert(false);
176  }
177 }
shared_ptr< Configuration > config
Definition: statistic.hpp:106
string componentToName(Component component) const
Definition: statistic.cpp:161
void stop(shared_ptr< Statistic::JobMeasurement > &job, Component component) const
Definition: statistic.cpp:79
shared_ptr< JobMeasurement > startNewMeasurement(const string &stylesheet, int zoom)
Definition: statistic.cpp:52
void setStats(shared_ptr< Statistic::JobMeasurement > &job, unsigned int nodes, unsigned int ways, unsigned int relations)
Definition: statistic.cpp:61
boost::posix_time::time_duration duration
Definition: statistic.hpp:33
void start(shared_ptr< Statistic::JobMeasurement > &job, Component component) const
Definition: statistic.cpp:72
void finished(shared_ptr< Statistic::JobMeasurement > &job)
Definition: statistic.cpp:110
std::vector< shared_ptr< JobMeasurement > > measurementsBuffer
Definition: statistic.hpp:108
Statistic(const shared_ptr< Configuration > &config)
Definition: statistic.cpp:35
ptime stopTime[Component::Size]
Definition: statistic.hpp:73
ptime startTime[Component::Size]
Definition: statistic.hpp:72
AvgMeasurement componentAvgs[Component::Size]
Definition: statistic.hpp:117
boost::mutex bufferLock
Definition: statistic.hpp:107
static shared_ptr< Statistic > instance
This file is part of alaCarte.
Definition: statistic.hpp:101
#define LOG_SEV(log, lvl)
Definition: settings.hpp:78
static const char * performance_log
Filepath to the performance log (type: string)
void writeToFile(const char *filename)
Definition: statistic.cpp:142
boost::mutex avgLock
Definition: statistic.hpp:116
duration getDuration(int i)
Definition: statistic.cpp:30
void printStatistic() const
Definition: statistic.cpp:86
Definition: cache.hpp:38