Gnash  0.8.11dev
Stats.h
Go to the documentation of this file.
1 // Stats.h -- classes for generic statistics gathering
2 //
3 // Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
4 // Free Software Foundation, Inc
5 //
6 // This program is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation; either version 3 of the License, or
9 // (at your option) any later version.
10 //
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software
18 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 
20 #ifndef GNASH_STATS_H
21 #define GNASH_STATS_H
22 
23 
24 #include <map>
25 #include <iostream>
26 #include <iomanip>
27 
28 #include "string_table.h"
29 
30 namespace gnash {
31 namespace stats {
32 
33 class KeyLookup {
34 
35  typedef std::map<string_table::key, unsigned long int> Stat;
36 
37 public:
38 
46  KeyLookup(const std::string& label, const string_table& st, int dumpTrigger=0,
47  string_table::key restrict=0, int dumpCount=5)
48  :
49  _st(st),
50  _dumpCount(dumpCount),
51  _dumpTrigger(dumpTrigger),
52  _label(label),
53  _restrict(restrict)
54  {}
55 
57  {
58  dump(_dumpCount);
59  }
60 
62  int gotTo = ++stat[k];
63  if ( _restrict && k != _restrict ) return;
64  if ( ! _dumpTrigger ) return;
65  if ( ! ( gotTo % _dumpTrigger ) ) dump(_dumpCount);
66  }
67 
68  void dump(int count) {
69  typedef std::map<unsigned long int, string_table::key> Sorted;
70  Sorted sorted;
71  for (Stat::iterator i=stat.begin(), e=stat.end(); i!=e; ++i)
72  sorted[i->second] = i->first;
73  std::cerr << _label << " lookups: " << std::endl;
74  for (Sorted::reverse_iterator i=sorted.rbegin(), e=sorted.rend();
75  i!=e; ++i) {
76  std::cerr
77  << std::setw(10)
78  << i->first
79  << ":"
80  << _st.value(i->second) << "("
81  << i->second << ")"
82  << std::endl;
83  if ( ! --count ) break;
84  }
85  }
86 
87 private:
88 
89  Stat stat;
90  const string_table& _st;
91  int _dumpCount;
92  int _dumpTrigger;
93  std::string _label;
94  string_table::key _restrict;
95 
96 
97 };
98 
99 } // namespace gnash.stats
100 } // namespace gnash
101 
102 #endif
~KeyLookup()
Definition: Stats.h:56
const std::string & value(key to_find) const
Find a string by its key.
Definition: string_table.h:102
Definition: GnashKey.h:157
Anonymous namespace for callbacks, local functions, event handlers etc.
Definition: dbus_ext.cpp:40
void check(string_table::key k)
Definition: Stats.h:61
A general use string table.
Definition: string_table.h:41
Definition: Stats.h:33
void dump(int count)
Definition: Stats.h:68
KeyLookup(const std::string &label, const string_table &st, int dumpTrigger=0, string_table::key restrict=0, int dumpCount=5)
Definition: Stats.h:46
Definition: GnashKey.h:155
Definition: GnashKey.h:151
std::size_t key
Definition: string_table.h:83