Plug more leaks (in the test, not the core)
[gnash.git] / libbase / Stats.h
bloba3fa1b2668e145b24aa287220030d8932264d4ee
1 // Stats.h -- classes for generic statistics gathering
2 //
3 // Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010 Free Software
4 // 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
20 #ifndef GNASH_STATS_H
21 #define GNASH_STATS_H
24 #include <map>
25 #include <iostream>
26 #include <iomanip>
28 #include "string_table.h"
30 namespace gnash {
31 namespace stats {
33 class KeyLookup {
35 typedef std::map<string_table::key, unsigned long int> Stat;
37 public:
39 /// @param label The label to print for dumps of this stat
40 /// @param st The string table to use for resolving stats values
41 /// @param dumpTrigger The number of calls to check() that should be
42 /// triggering a dump
43 /// @param restrict If non-zero dumpTrigger refers to this key lookups
44 /// @param dumpCount Number of items to print in the dump (makes sense if not restricted)
45 ///
46 KeyLookup(const std::string& label, const string_table& st, int dumpTrigger=0,
47 string_table::key restrict=0, int dumpCount=5)
49 _st(st),
50 _dumpCount(dumpCount),
51 _dumpTrigger(dumpTrigger),
52 _label(label),
53 _restrict(restrict)
56 ~KeyLookup()
58 dump(_dumpCount);
61 void check(string_table::key k) {
62 int gotTo = ++stat[k];
63 if ( _restrict && k != _restrict ) return;
64 if ( ! _dumpTrigger ) return;
65 if ( ! ( gotTo % _dumpTrigger ) ) dump(_dumpCount);
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;
87 private:
89 Stat stat;
90 const string_table& _st;
91 int _dumpCount;
92 int _dumpTrigger;
93 std::string _label;
94 string_table::key _restrict;
99 } // namespace gnash.stats
100 } // namespace gnash
102 #endif