游戏服务器之性能统计
来源:互联网 发布:好的代理软件 编辑:程序博客网 时间:2024/05/17 06:23
http://blog.csdn.net/chenjiayi_yun/article/details/35276861
性能统计分析器用来统计多个函数在一定执行次数下的执行时间,并输出函数执行时间较长的函数名和时间。
目录:
(1)性能统计分析器
(2)统计方便函数类,在构造函数和析构函数里执行时间统计
(3)定时器
(4)测试实例
内容:
(1)性能统计分析器
- class Performance_Statistics
- {
- private:
- struct Performance
- {
- Performance():_times(0),_total_time(0) {}
- /**
- * \author: cjy
- * \description: 执行的次数
- */
- unsigned long _times;
- /**
- * \author: cjy
- * \description:执行的总时间
- */
- unsigned long _total_time;
- };
- std::map<std::string, Performance> _times;
- timer _log_timer;
- public:
- Performance_Statistics(const int how_long) : _log_timer(how_long) {}
- ~Performance_Statistics() {}
- void inc(const std::string &func, const unsigned long total)
- {
- Performance &mt = _times[func];
- if (mt._times)
- {
- ++mt._times;
- mt._total_time += total;
- }
- else
- {
- ++mt._times;
- mt._total_time = total;
- }
- }
- void reset(const realtime &ct, const bool force_print)
- {
- if (force_print || _log_timer(ct))
- {
- g_log->debug("[分时统计]:%s, %lu", force_print ? "force" : "timer", _times.size());
- for(std::map<std::string, Performance>::iterator it = _times.begin(); it != _times.end(); ++it)
- {
- if (it->second._times && (it->second._total_time / 1000000L) > 5)//输出5ms以上的
- {
- g_log->debug("[分时统计]:%s, %lu毫秒, %lu次, %lu毫秒/次",it->first.c_str(), it->second._total_time /1000000L, it->second._times, (it->second._total_time / 1000000L) / it->second._times);
- }
- }
- }
- _times.clear();
- }
- };
(2)统计方便函数,在构造函数和析构函数里执行时间统计
- class Function_Exe_Time
- {
- private:
- const std::string _func;
- struct timespec _tv_1;
- public:
- static Performance_Statistics my_func;
- Function_Exe_Time(const std::string &func) : _func(func)
- {
- clock_gettime(CLOCK_REALTIME, &_tv_1);
- }
- ~Function_Exe_Time()
- {
- struct timespec _tv_2;
- clock_gettime(CLOCK_REALTIME, &_tv_2);
- unsigned long end=(unsigned long)_tv_2.tv_sec*1000000000L + _tv_2.tv_nsec;
- unsigned long begin=(unsigned long)_tv_1.tv_sec*1000000000L + _tv_1.tv_nsec;
- my_func.inc(_func, end-begin);
- }
- };
(3)定时器(4)测试实例测试ai攻击处理花费时间
- {
- Function_Exe_Time func("NPC_AI_ATTACK"STR(__LINE__));
- return doAttackAI();
- }
0 0