The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
metrics.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2003 - 2016 by David White <[email protected]>
3  Part of the Battle for Wesnoth Project http://www.wesnoth.org/
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details.
13 */
14 
15 /**
16  * @file
17  * Various server-statistics.
18  */
19 
20 #include "global.hpp"
21 
22 #include "metrics.hpp"
23 
24 #include <algorithm>
25 #include <iostream>
26 
29  {
30  return a < b;
31  }
32 };
33 
35  bool operator()(const metrics::sample& a, const metrics::sample& b) const {
36  return a.processing_time < b.processing_time;
37  }
38 };
39 
40 
42  samples_(),
43  most_consecutive_requests_(0),
44  current_requests_(0),
45  nrequests_(0),
46  nrequests_waited_(0),
47  started_at_(time(nullptr)),
48  terminations_()
49 {}
50 
52 {
53 
55  itor != samples_.end(); ++itor)
56  {
57  delete[] itor->name.begin();
58  }
59  samples_.clear();
60 }
61 
63 {
64  if(current_requests_ > 0) {
66  }
67 
68  ++nrequests_;
72  }
73 }
74 
76 {
78 }
79 
81  clock_t parsing_time, clock_t processing_time)
82 {
84  if(isample == samples_.end()
85  || isample->name != name) {
86  //protect against DoS with memory exhaustion
87  if(samples_.size() > 30) {
88  return;
89  }
90  int index = isample - samples_.begin();
91  simple_wml::string_span dup_name(name.duplicate());
92  sample new_sample;
93  new_sample.name = dup_name;
94  samples_.insert(isample, new_sample);
95 
96  isample = samples_.begin() + index;
97  }
98 
99  isample->nsamples++;
100  isample->parsing_time += parsing_time;
101  isample->processing_time += processing_time;
102  isample->max_parsing_time = std::max(parsing_time,isample->max_parsing_time);
103  isample->max_processing_time = std::max(processing_time,isample->max_processing_time);
104 }
105 
107 {
108  terminations_[reason]++;
109 }
110 
111 std::ostream& metrics::games(std::ostream& out) const
112 {
113  if (terminations_.empty()) return out << "No game ended so far.";
114 
115  size_t n = 0;
116  out << "Games have been terminated in the following ways:\n";
117  for(std::map<std::string,int>::const_iterator i = terminations_.begin(); i != terminations_.end(); ++i) {
118  out << i->first << ": " << i->second << "\n";
119  n += i->second;
120  }
121  out << "Total number of games = " << n;
122 
123  return out;
124 }
125 
126 std::ostream& metrics::requests(std::ostream& out) const
127 {
128  if (samples_.empty()) return out;
129 
130  std::vector<metrics::sample> ordered_samples = samples_;
131  std::sort(ordered_samples.begin(), ordered_samples.end(), compare_samples_by_time());
132 
133  out << "\nSampled request types:\n";
134 
135  size_t n = 0;
136  size_t pa = 0;
137  size_t pr = 0;
138  for(std::vector<metrics::sample>::const_iterator s = ordered_samples.begin(); s != ordered_samples.end(); ++s) {
139  out << "'" << s->name << "' called " << s->nsamples << " times "
140  << s->parsing_time << "("<< s->max_parsing_time <<") parsing time, "
141  << s->processing_time << "("<<s->max_processing_time<<") processing time\n";
142  n += s->nsamples;
143  pa += s->parsing_time;
144  pr += s->processing_time;
145  }
146  out << "Total number of request samples = " << n << "\n"
147  << "Total parsing time = " << pa << "\n"
148  << "Total processing time = " << pr;
149 
150  return out;
151 }
152 
153 std::ostream& operator<<(std::ostream& out, metrics& met)
154 {
155  const time_t time_up = time(nullptr) - met.started_at_;
156  const int seconds = time_up%60;
157  const int minutes = (time_up/60)%60;
158  const int hours = (time_up/(60*60))%24;
159  const int days = time_up/(60*60*24);
160  const int requests_immediate = met.nrequests_ - met.nrequests_waited_;
161  const int percent_immediate = (requests_immediate*100)/(met.nrequests_ > 0 ? met.nrequests_ : 1);
162  out << "METRICS\nUp " << days << " days, " << hours << " hours, "
163  << minutes << " minutes, " << seconds << " seconds\n"
164  << met.nrequests_ << " requests serviced. " << requests_immediate
165  << " (" << percent_immediate << "%) "
166  << "requests were serviced immediately.\n"
167  << "longest burst of requests was: " << met.most_consecutive_requests_;
168 
169  return out;
170 }
metrics()
Definition: metrics.cpp:41
std::ostream & games(std::ostream &out) const
Definition: metrics.cpp:111
static paths::dest_vect::iterator lower_bound(paths::dest_vect &v, const map_location &loc)
Definition: pathfind.cpp:461
int current_requests_
Definition: metrics.hpp:73
char * duplicate() const
Definition: simple_wml.cpp:186
void service_request()
Definition: metrics.cpp:62
GLdouble GLdouble GLdouble b
Definition: glew.h:6966
std::ostream & requests(std::ostream &out) const
Definition: metrics.cpp:126
std::vector< sample > samples_
Definition: metrics.hpp:70
~metrics()
Definition: metrics.cpp:51
bool operator()(const metrics::sample &a, const metrics::sample &b) const
Definition: metrics.cpp:35
const time_t started_at_
Definition: metrics.hpp:76
GLboolean GLboolean GLboolean GLboolean a
Definition: glew.h:7319
void no_requests()
Definition: metrics.cpp:75
clock_t processing_time
Definition: metrics.hpp:60
int nrequests_
Definition: metrics.hpp:74
simple_wml::string_span name
Definition: metrics.hpp:58
void game_terminated(const std::string &reason)
Definition: metrics.cpp:106
std::map< std::string, tfilter >::iterator itor
Definition: filter.cpp:199
GLuint index
Definition: glew.h:1782
std::ostream & operator<<(std::ostream &out, metrics &met)
Definition: metrics.cpp:153
size_t i
Definition: function.cpp:1057
static int sort(lua_State *L)
Definition: ltablib.cpp:246
GLuint const GLchar * name
Definition: glew.h:1782
std::map< std::string, int > terminations_
Definition: metrics.hpp:77
int most_consecutive_requests_
Definition: metrics.hpp:72
GLclampd n
Definition: glew.h:5903
bool operator()(const simple_wml::string_span &a, const simple_wml::string_span &b)
Definition: metrics.cpp:28
void record_sample(const simple_wml::string_span &name, clock_t parsing_time, clock_t processing_time)
Definition: metrics.cpp:80
int nrequests_waited_
Definition: metrics.hpp:75
std::string::const_iterator iterator
Definition: tokenizer.hpp:21
GLdouble s
Definition: glew.h:1358
GLsizei const GLcharARB ** string
Definition: glew.h:4503