Page Speed Optimization Libraries  1.13.35.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
statistics_template.h
Go to the documentation of this file.
1 /*
2  * Copyright 2010 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http:///www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
18 
19 #ifndef PAGESPEED_KERNEL_BASE_STATISTICS_TEMPLATE_H_
20 #define PAGESPEED_KERNEL_BASE_STATISTICS_TEMPLATE_H_
21 
22 #include <algorithm>
23 #include <cstddef>
24 #include <map>
25 #include <vector>
26 
33 
34 namespace net_instaweb {
35 class MessageHandler;
36 
40 template<class Var, class UpDown, class Hist,
41  class TimedVar> class StatisticsTemplate
42  : public Statistics {
43  public:
45  virtual ~StatisticsTemplate() {
46  STLDeleteContainerPointers(variables_.begin(), variables_.end());
47  STLDeleteContainerPointers(up_downs_.begin(), up_downs_.end());
48  STLDeleteContainerPointers(histograms_.begin(), histograms_.end());
49  STLDeleteContainerPointers(timed_vars_.begin(), timed_vars_.end());
50  }
51 
54  virtual Var* AddVariable(const StringPiece& name) {
55  Var* var = FindVariable(name);
56  if (var == NULL) {
57  var = NewVariable(name);
58  variables_.push_back(var);
59  variable_names_.push_back(name.as_string());
60  variable_map_[name.as_string()] = var;
61  }
62  return var;
63  }
64 
65  virtual UpDown* AddUpDownCounter(const StringPiece& name) {
66  UpDown* var = FindUpDownCounter(name);
67  if (var == NULL) {
68  var = NewUpDownCounter(name);
69  up_downs_.push_back(var);
70  up_down_names_.push_back(name.as_string());
71  up_down_map_[name.as_string()] = var;
72  }
73  return var;
74  }
75 
76  virtual UpDown* AddGlobalUpDownCounter(const StringPiece& name) {
77  UpDown* var = FindUpDownCounter(name);
78  if (var == NULL) {
79  var = NewGlobalUpDownCounter(name);
80  up_downs_.push_back(var);
81  up_down_names_.push_back(name.as_string());
82  up_down_map_[name.as_string()] = var;
83  }
84  return var;
85  }
86 
87  virtual Var* FindVariable(const StringPiece& name) const {
88  typename VarMap::const_iterator p = variable_map_.find(name.as_string());
89  Var* var = NULL;
90  if (p != variable_map_.end()) {
91  var = p->second;
92  }
93  return var;
94  }
95 
96  virtual UpDown* FindUpDownCounter(const StringPiece& name) const {
97  typename UpDownMap::const_iterator p = up_down_map_.find(name.as_string());
98  UpDown* var = NULL;
99  if (p != up_down_map_.end()) {
100  var = p->second;
101  }
102  return var;
103  }
104 
105  virtual Hist* AddHistogram(const StringPiece& name) {
106  Hist* hist = FindHistogram(name);
107  if (hist == NULL) {
108  hist = NewHistogram(name);
109  histograms_.push_back(hist);
110  histogram_names_.push_back(name.as_string());
111  histogram_map_[name.as_string()] = hist;
112  }
113  return hist;
114  }
115 
116  virtual Hist* FindHistogram(const StringPiece& name) const {
117  typename HistMap::const_iterator p = histogram_map_.find(name.as_string());
118  Hist* hist = NULL;
119  if (p != histogram_map_.end()) {
120  hist = p->second;
121  }
122  return hist;
123  }
124 
125  virtual TimedVar* AddTimedVariable(const StringPiece& name,
126  const StringPiece& group) {
127  TimedVar* timedvar = FindTimedVariable(name);
128  if (timedvar == NULL) {
129  timedvar = NewTimedVariable(name);
130  timed_vars_.push_back(timedvar);
131  timed_var_map_[name.as_string()] = timedvar;
132  timed_var_group_map_[group.as_string()].push_back(name.as_string());
133  }
134  return timedvar;
135  }
136 
137  virtual TimedVar* FindTimedVariable(const StringPiece& name) const {
138  typename TimedVarMap::const_iterator p =
139  timed_var_map_.find(name.as_string());
140  TimedVar* timedvar = NULL;
141  if (p != timed_var_map_.end()) {
142  timedvar = p->second;
143  }
144  return timedvar;
145  }
146 
147  virtual const StringVector& HistogramNames() {
148  return histogram_names_;
149  }
150 
151  virtual const std::map<GoogleString, StringVector>& TimedVariableMap() {
152  return timed_var_group_map_;
153  }
154 
155  virtual void Dump(Writer* writer, MessageHandler* message_handler) {
156  int longest_string = 0;
157  for (int i = 0, n = variables_.size(); i < n; ++i) {
158  const GoogleString& var_name = variable_names_[i];
159  int length_number = Integer64ToString(variables_[i]->Get()).size();
160  int length_name = var_name.size();
161  longest_string = std::max(longest_string, length_name + length_number);
162  }
163  for (int i = 0, n = up_downs_.size(); i < n; ++i) {
164  const GoogleString& up_down_name = up_down_names_[i];
165  int length_number = Integer64ToString(up_downs_[i]->Get()).size();
166  int length_name = up_down_name.size();
167  longest_string = std::max(longest_string, length_name + length_number);
168  }
169 
170  GoogleString spaces_buffer = GoogleString(longest_string, ' ');
171  StringPiece spaces(spaces_buffer);
172  for (int i = 0, n = variables_.size(); i < n; ++i) {
173  const GoogleString& var_name = variable_names_[i];
174  GoogleString var_as_str = Integer64ToString(variables_[i]->Get());
175  writer->Write(var_name, message_handler);
176  writer->Write(": ", message_handler);
177  int num_spaces = longest_string - var_name.size() - var_as_str.size();
178  writer->Write(spaces.substr(0, num_spaces), message_handler);
179  writer->Write(var_as_str, message_handler);
180  writer->Write("\n", message_handler);
181  }
182  for (int i = 0, n = up_downs_.size(); i < n; ++i) {
183  const GoogleString& up_down_name = up_down_names_[i];
184  GoogleString up_down_as_str = Integer64ToString(up_downs_[i]->Get());
185  writer->Write(up_down_name, message_handler);
186  writer->Write(": ", message_handler);
187  int num_spaces = longest_string - up_down_name.size() -
188  up_down_as_str.size();
189  writer->Write(spaces.substr(0, num_spaces), message_handler);
190  writer->Write(up_down_as_str, message_handler);
191  writer->Write("\n", message_handler);
192  }
193  }
194 
197  virtual void DumpJson(Writer* writer, MessageHandler* message_handler) {
198  int longest_string = 0;
199  writer->Write("{\"variables\": {", message_handler);
200  for (int i = 0, n = variables_.size(); i < n; ++i) {
201  const GoogleString& var_name = variable_names_[i];
202  GoogleString var_as_str = Integer64ToString(variables_[i]->Get());
203  int length_name = var_name.size();
204  int length_number = var_as_str.size();
205  longest_string = std::max(longest_string, length_name + length_number);
206  writer->Write(StrCat("\"", var_name, "\": ", var_as_str),
207  message_handler);
208  if (i != n - 1) {
209  writer->Write(",", message_handler);
210  }
211  }
212  for (int i = 0, n = up_downs_.size(); i < n; ++i) {
213  const GoogleString& up_down_name = up_down_names_[i];
214  GoogleString up_down_as_str = Integer64ToString(up_downs_[i]->Get());
215  int length_name = up_down_name.size();
216  int length_number = up_down_as_str.size();
217  longest_string = std::max(longest_string, length_name + length_number);
218  writer->Write(StrCat(",\"", up_down_name, "\": ", up_down_as_str),
219  message_handler);
220  }
221  writer->Write("}, \"maxlength\": ", message_handler);
222  writer->Write(Integer64ToString(longest_string), message_handler);
223  writer->Write("}", message_handler);
224  }
225 
226  virtual void Clear() {
227  for (int i = 0, n = variables_.size(); i < n; ++i) {
228  Variable* var = variables_[i];
229  var->Clear();
230  }
231  for (int i = 0, n = up_downs_.size(); i < n; ++i) {
232  UpDownCounter* var = up_downs_[i];
233  var->Clear();
234  }
235  for (int i = 0, n = histograms_.size(); i < n; ++i) {
236  Histogram* hist = histograms_[i];
237  hist->Clear();
238  }
239  for (int i = 0, n = timed_vars_.size(); i < n; ++i) {
240  TimedVariable* timedvar = timed_vars_[i];
241  timedvar->Clear();
242  }
243  }
244 
245  protected:
247  virtual Var* NewVariable(StringPiece name) = 0;
248 
250  virtual UpDown* NewUpDownCounter(StringPiece name) = 0;
251 
253  virtual UpDown* NewGlobalUpDownCounter(StringPiece name) {
254  return NewUpDownCounter(name);
255  }
256 
257  virtual Hist* NewHistogram(StringPiece name) = 0;
258  virtual TimedVar* NewTimedVariable(StringPiece name) = 0;
259 
260  size_t variables_size() const { return variables_.size(); }
261  Var* variables(size_t pos) { return variables_.at(pos); }
262 
263  size_t up_down_size() const { return up_downs_.size(); }
264  UpDown* up_downs(size_t pos) { return up_downs_.at(pos); }
265 
266  size_t histograms_size() const { return histograms_.size(); }
267  Hist* histograms(size_t pos) { return histograms_.at(pos); }
268 
269  const GoogleString& histogram_names(size_t pos) const {
270  return histogram_names_.at(pos);
271  }
272 
273  private:
274  typedef std::vector<Var*> VarVector;
275  typedef std::map<GoogleString, Var*> VarMap;
276  typedef std::vector<UpDown*> UpDownVector;
277  typedef std::map<GoogleString, UpDown*> UpDownMap;
278  typedef std::vector<Hist*> HistVector;
279  typedef std::map<GoogleString, Hist*> HistMap;
280 
281  typedef std::vector<TimedVar*> TimedVarVector;
282  typedef std::map<GoogleString, TimedVar*> TimedVarMap;
283  VarVector variables_;
284  VarMap variable_map_;
285  UpDownVector up_downs_;
286  UpDownMap up_down_map_;
287  HistVector histograms_;
288  HistMap histogram_map_;
289  TimedVarVector timed_vars_;
290  TimedVarMap timed_var_map_;
292  std::map<GoogleString, StringVector> timed_var_group_map_;
293  StringVector variable_names_;
294  StringVector up_down_names_;
295  StringVector histogram_names_;
296 
297 
298 };
299 
313 template<class Impl> class VarTemplate : public Variable {
314  public:
315  VarTemplate(StringPiece name, Statistics* stats) : impl_(name, stats) {}
316  virtual ~VarTemplate() {}
317  virtual int64 Get() const { return impl_.Get(); }
318  virtual StringPiece GetName() const { return impl_.GetName(); }
319  virtual int64 AddHelper(int64 delta) { return impl_.AddHelper(delta); }
320  virtual void Clear() { impl_.Set(0); }
321 
322  Impl* impl() { return &impl_; }
323 
324  private:
325  Impl impl_;
326 
327 
328 };
329 
334 template<class Impl> class UpDownTemplate : public UpDownCounter {
335  public:
336  UpDownTemplate(StringPiece name, Statistics* stats)
337  : impl_(name, stats) {}
338  virtual ~UpDownTemplate() {}
339  virtual int64 Get() const { return impl_.Get(); }
340  virtual StringPiece GetName() const { return impl_.GetName(); }
341  virtual void Set(int64 value) { impl_.Set(value); }
342  virtual int64 AddHelper(int64 delta) { return impl_.AddHelper(delta); }
343  virtual void Clear() { impl_.Set(0); }
344 
345  Impl* impl() { return &impl_; }
346 
347  private:
348  Impl impl_;
349 
350 
351 };
352 
355 template<class Impl,
356  class HistC = CountHistogram,
357  class TVarC = FakeTimedVariable>
359  : public StatisticsTemplate<VarTemplate<Impl>, UpDownTemplate<Impl>,
360  HistC, TVarC> {
361  public:
365  typedef HistC Hist;
366  typedef TVarC TVar;
367 
369  virtual ~ScalarStatisticsTemplate() {}
370 
371  protected:
372  virtual Var* NewVariable(StringPiece name) {
373  return new Var(name, this);
374  }
375 
376  virtual UpDown* NewUpDownCounter(StringPiece name) {
377  return new UpDown(name, this);
378  }
379 
380  virtual TVar* NewTimedVariable(StringPiece name) {
381  return new TVar(name, this);
382  }
383 };
384 
385 }
386 
387 #endif
virtual const StringVector & HistogramNames()
Return the names of all the histograms for render.
Definition: statistics_template.h:147
virtual int64 AddHelper(int64 delta)
This is virtual so that subclasses can add platform-specific atomicity.
Definition: statistics_template.h:319
TimeDVariable.
Definition: statistics_template.h:358
virtual void Clear()=0
Throw away all data.
Definition: statistics.h:43
virtual void Dump(Writer *writer, MessageHandler *message_handler)
Dump the variable-values to a writer.
Definition: statistics_template.h:155
virtual StringPiece GetName() const
Definition: statistics_template.h:318
virtual UpDown * FindUpDownCounter(const StringPiece &name) const
Find a variable from a name, returning NULL if not found.
Definition: statistics_template.h:96
virtual Var * NewVariable(StringPiece name)=0
Interface to subclass.
virtual Hist * FindHistogram(const StringPiece &name) const
Find a histogram from a name, returning NULL if not found.
Definition: statistics_template.h:116
virtual Hist * AddHistogram(const StringPiece &name)
Definition: statistics_template.h:105
virtual Var * NewVariable(StringPiece name)
Interface to subclass.
Definition: statistics_template.h:372
Base class for implementations of monitoring statistics.
Definition: statistics.h:342
virtual StringPiece GetName() const
Definition: statistics_template.h:340
virtual TimedVar * FindTimedVariable(const StringPiece &name) const
Find a TimedVariable from a name, returning NULL if not found.
Definition: statistics_template.h:137
virtual UpDown * NewGlobalUpDownCounter(StringPiece name)
Default implementation just calls NewUpDownCounter.
Definition: statistics_template.h:253
virtual UpDown * NewUpDownCounter(StringPiece name)
Interface to subclass.
Definition: statistics_template.h:376
TimedVariable implementation that only updates a basic UpDownCounter.
Definition: statistics.h:313
virtual void Clear()
Definition: statistics_template.h:226
virtual void Clear()=0
Throw away all data.
virtual UpDown * AddUpDownCounter(const StringPiece &name)
Definition: statistics_template.h:65
virtual TimedVar * AddTimedVariable(const StringPiece &name, const StringPiece &group)
Definition: statistics_template.h:125
std::string GoogleString
PAGESPEED_KERNEL_BASE_STRING_H_.
Definition: string.h:24
Definition: statistics_template.h:313
virtual Var * AddVariable(const StringPiece &name)
Definition: statistics_template.h:54
virtual int64 AddHelper(int64 delta)
This is virtual so that subclasses can add platform-specific atomicity.
Definition: statistics_template.h:342
virtual UpDown * AddGlobalUpDownCounter(const StringPiece &name)
Definition: statistics_template.h:76
Trivial implementation. But Count() returns a meaningful value.
Definition: statistics.h:255
Interface for writing bytes to an output stream.
Definition: writer.h:29
Definition: statistics_template.h:41
VarTemplate< Impl > Var
Add typedefs for template class args to make them visible to subclasses.
Definition: statistics_template.h:363
virtual const std::map< GoogleString, StringVector > & TimedVariableMap()
Return the map of groupnames and names of all timedvariables for render.
Definition: statistics_template.h:151
Definition: statistics.h:138
virtual UpDown * NewUpDownCounter(StringPiece name)=0
Interface to subclass.
virtual void DumpJson(Writer *writer, MessageHandler *message_handler)
Definition: statistics_template.h:197
virtual Var * FindVariable(const StringPiece &name) const
Find a variable from a name, returning NULL if not found.
Definition: statistics_template.h:87
Definition: statistics.h:73
Definition: message_handler.h:39
Definition: statistics.h:298
Definition: statistics_template.h:334