Gnash  0.8.10
Stats.h
Go to the documentation of this file.
00001 // Stats.h -- classes for generic statistics gathering
00002 // 
00003 //   Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
00004 //   Free Software Foundation, Inc
00005 // 
00006 // This program is free software; you can redistribute it and/or modify
00007 // it under the terms of the GNU General Public License as published by
00008 // the Free Software Foundation; either version 3 of the License, or
00009 // (at your option) any later version.
00010 // 
00011 // This program is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 // GNU General Public License for more details.
00015 // 
00016 // You should have received a copy of the GNU General Public License
00017 // along with this program; if not, write to the Free Software
00018 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00019 
00020 #ifndef GNASH_STATS_H
00021 #define GNASH_STATS_H
00022 
00023 
00024 #include <map>
00025 #include <iostream>
00026 #include <iomanip>
00027 
00028 #include "string_table.h"
00029 
00030 namespace gnash {
00031 namespace stats {
00032 
00033 class KeyLookup {
00034 
00035     typedef std::map<string_table::key, unsigned long int> Stat;
00036 
00037 public:
00038 
00046     KeyLookup(const std::string& label, const string_table& st, int dumpTrigger=0,
00047             string_table::key restrict=0, int dumpCount=5)
00048         :
00049         _st(st),
00050         _dumpCount(dumpCount),
00051         _dumpTrigger(dumpTrigger),
00052         _label(label),
00053         _restrict(restrict)
00054     {}
00055 
00056     ~KeyLookup()
00057     {
00058         dump(_dumpCount);
00059     }
00060 
00061     void check(string_table::key k) {
00062         int gotTo = ++stat[k];
00063         if ( _restrict && k != _restrict ) return;
00064         if ( ! _dumpTrigger ) return;
00065         if ( ! ( gotTo % _dumpTrigger ) ) dump(_dumpCount);
00066     }
00067 
00068     void dump(int count) {
00069         typedef std::map<unsigned long int, string_table::key> Sorted;
00070         Sorted sorted;
00071         for (Stat::iterator i=stat.begin(), e=stat.end(); i!=e; ++i)
00072             sorted[i->second] = i->first;
00073         std::cerr << _label << " lookups: " << std::endl;
00074         for (Sorted::reverse_iterator i=sorted.rbegin(), e=sorted.rend();
00075                 i!=e; ++i) {
00076             std::cerr
00077                       << std::setw(10)
00078                       << i->first
00079                       << ":"
00080                       << _st.value(i->second) << "("
00081                       << i->second << ")"
00082                       << std::endl;
00083             if ( ! --count ) break;
00084         }
00085     }
00086 
00087 private:
00088 
00089     Stat stat;
00090     const string_table& _st;
00091     int _dumpCount;
00092     int _dumpTrigger;
00093     std::string _label;
00094     string_table::key _restrict;
00095 
00096 
00097 };
00098 
00099 } // namespace gnash.stats
00100 } // namespace gnash
00101 
00102 #endif