The Battle for Wesnoth  1.15.12+dev
metrics.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2003 - 2018 by David White <dave@whitevine.net>
3  Part of the Battle for Wesnoth Project https://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 
21 
22 #include <algorithm>
23 #include <iostream>
24 
27  {
28  return a < b;
29  }
30 };
31 
33  bool operator()(const metrics::sample& a, const metrics::sample& b) const {
34  return a.processing_time < b.processing_time;
35  }
36 };
37 
39  : samples_()
40  , most_consecutive_requests_(0)
41  , current_requests_(0)
42  , nrequests_(0)
43  , nrequests_waited_(0)
44  , started_at_(std::time(nullptr))
45  , terminations_()
46 {
47 }
48 
50 {
51  for(auto& s : samples_) {
52  delete[] s.name.begin();
53  }
54 
55  samples_.clear();
56 }
57 
59 {
60  if(current_requests_ > 0) {
62  }
63 
64  ++nrequests_;
68  }
69 }
70 
72 {
74 }
75 
76 void metrics::record_sample(const simple_wml::string_span& name, clock_t parsing_time, clock_t processing_time)
77 {
78  auto isample = std::lower_bound(samples_.begin(), samples_.end(), name,compare_samples_to_stringspan());
79  if(isample == samples_.end() || isample->name != name) {
80  //protect against DoS with memory exhaustion
81  if(samples_.size() > 30) {
82  return;
83  }
84  int index = std::distance(samples_.begin(), isample);
85  simple_wml::string_span dup_name(name.duplicate());
86  sample new_sample;
87  new_sample.name = dup_name;
88  samples_.insert(isample, new_sample);
89 
90  isample = samples_.begin() + index;
91  }
92 
93  isample->nsamples++;
94  isample->parsing_time += parsing_time;
95  isample->processing_time += processing_time;
96  isample->max_parsing_time = std::max(parsing_time,isample->max_parsing_time);
97  isample->max_processing_time = std::max(processing_time,isample->max_processing_time);
98 }
99 
100 void metrics::game_terminated(const std::string& reason)
101 {
102  terminations_[reason]++;
103 }
104 
105 std::ostream& metrics::games(std::ostream& out) const
106 {
107  if (terminations_.empty()) return out << "No game ended so far.";
108 
109  std::size_t n = 0;
110  out << "Games have been terminated in the following ways:\n";
111  for(const auto& t : terminations_) {
112  out << t.first << ": " << t.second << "\n";
113  n += t.second;
114  }
115  out << "Total number of games = " << n;
116 
117  return out;
118 }
119 
120 std::ostream& metrics::requests(std::ostream& out) const
121 {
122  if (samples_.empty()) return out;
123 
124  std::vector<metrics::sample> ordered_samples = samples_;
125  std::sort(ordered_samples.begin(), ordered_samples.end(), compare_samples_by_time());
126 
127  out << "\nSampled request types:\n";
128 
129  std::size_t n = 0;
130  std::size_t pa = 0;
131  std::size_t pr = 0;
132  for(const auto& s : ordered_samples) {
133  out << "'" << s.name << "' called " << s.nsamples << " times "
134  << s.parsing_time << "("<< s.max_parsing_time <<") parsing time, "
135  << s.processing_time << "("<<s.max_processing_time<<") processing time\n";
136  n += s.nsamples;
137  pa += s.parsing_time;
138  pr += s.processing_time;
139  }
140  out << "Total number of request samples = " << n << "\n"
141  << "Total parsing time = " << pa << "\n"
142  << "Total processing time = " << pr;
143 
144  return out;
145 }
146 
147 std::ostream& operator<<(std::ostream& out, metrics& met)
148 {
149  const std::time_t time_up = std::time(nullptr) - met.started_at_;
150  const int seconds = time_up%60;
151  const int minutes = (time_up/60)%60;
152  const int hours = (time_up/(60*60))%24;
153  const int days = time_up/(60*60*24);
154  const int requests_immediate = met.nrequests_ - met.nrequests_waited_;
155  const int percent_immediate = (requests_immediate*100)/(met.nrequests_ > 0 ? met.nrequests_ : 1);
156  out << "METRICS\nUp " << days << " days, " << hours << " hours, "
157  << minutes << " minutes, " << seconds << " seconds\n"
158  << met.nrequests_ << " requests serviced. " << requests_immediate
159  << " (" << percent_immediate << "%) "
160  << "requests were serviced immediately.\n"
161  << "longest burst of requests was: " << met.most_consecutive_requests_;
162 
163  return out;
164 }
metrics()
Definition: metrics.cpp:38
const std::time_t started_at_
Definition: metrics.hpp:71
std::ostream & games(std::ostream &out) const
Definition: metrics.cpp:105
std::map< std::string, int > terminations_
Definition: metrics.hpp:72
#define a
char * duplicate() const
Definition: simple_wml.cpp:186
bool operator()(const simple_wml::string_span &a, const simple_wml::string_span &b) const
Definition: metrics.cpp:26
int current_requests_
Definition: metrics.hpp:68
STL namespace.
bool operator()(const metrics::sample &a, const metrics::sample &b) const
Definition: metrics.cpp:33
void service_request()
Definition: metrics.cpp:58
#define b
std::vector< sample > samples_
Definition: metrics.hpp:65
~metrics()
Definition: metrics.cpp:49
std::ostream & requests(std::ostream &out) const
Definition: metrics.cpp:120
void no_requests()
Definition: metrics.cpp:71
clock_t processing_time
Definition: metrics.hpp:55
int nrequests_
Definition: metrics.hpp:69
simple_wml::string_span name
Definition: metrics.hpp:53
void game_terminated(const std::string &reason)
Definition: metrics.cpp:100
static map_location::DIRECTION s
std::size_t index(const std::string &str, const std::size_t index)
Codepoint index corresponding to the nth character in a UTF-8 string.
Definition: unicode.cpp:71
static int sort(lua_State *L)
Definition: ltablib.cpp:397
int most_consecutive_requests_
Definition: metrics.hpp:67
double t
Definition: astarsearch.cpp:64
void record_sample(const simple_wml::string_span &name, clock_t parsing_time, clock_t processing_time)
Definition: metrics.cpp:76
friend std::ostream & operator<<(std::ostream &out, metrics &met)
Definition: metrics.cpp:147
int nrequests_waited_
Definition: metrics.hpp:70
static map_location::DIRECTION n