游戏服务器之性能统计

来源:互联网 发布:好的代理软件 编辑:程序博客网 时间:2024/05/17 06:23

http://blog.csdn.net/chenjiayi_yun/article/details/35276861


性能统计分析器用来统计多个函数在一定执行次数下的执行时间,并输出函数执行时间较长的函数名和时间。


目录:

(1)性能统计分析器

(2)统计方便函数类,在构造函数和析构函数里执行时间统计

(3)定时器

(4)测试实例

内容:

(1)性能统计分析器

[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. class Performance_Statistics  
  2. {  
  3.     private:  
  4.         struct Performance  
  5.         {  
  6.             Performance():_times(0),_total_time(0) {}  
  7.             /**  
  8.              * \author: cjy  
  9.              * \description: 执行的次数  
  10.              */  
  11.             unsigned long _times;  
  12.             /**  
  13.              * \author: cjy  
  14.              * \description:执行的总时间  
  15.              */  
  16.             unsigned long _total_time;  
  17.         };  
  18.         std::map<std::string, Performance> _times;  
  19.         timer _log_timer;  
  20.     public:  
  21.         Performance_Statistics(const int how_long) : _log_timer(how_long) {}  
  22.         ~Performance_Statistics() {}  
  23.         void inc(const std::string &func, const unsigned long total)  
  24.         {  
  25.             Performance &mt = _times[func];  
  26.             if (mt._times)  
  27.             {  
  28.                 ++mt._times;  
  29.                 mt._total_time += total;  
  30.             }  
  31.             else  
  32.             {  
  33.                 ++mt._times;  
  34.                 mt._total_time = total;  
  35.             }  
  36.         }  
  37.         void reset(const realtime &ct, const bool force_print)  
  38.         {  
  39.             if (force_print || _log_timer(ct))  
  40.             {  
  41.                 g_log->debug("[分时统计]:%s, %lu", force_print ? "force" : "timer", _times.size());  
  42.                 for(std::map<std::string, Performance>::iterator it = _times.begin(); it != _times.end(); ++it)  
  43.                 {  
  44.                     if (it->second._times && (it->second._total_time / 1000000L) > 5)//输出5ms以上的  
  45.                     {  
  46.                         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);  
  47.                     }  
  48.                 }  
  49.             }  
  50.             _times.clear();  
  51.         }  
  52. };  

(2)统计方便函数,在构造函数和析构函数里执行时间统计

[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. class Function_Exe_Time  
  2. {  
  3. private:  
  4.     const std::string _func;  
  5.     struct timespec _tv_1;  
  6. public:  
  7.     static Performance_Statistics my_func;  
  8.     Function_Exe_Time(const std::string &func) : _func(func)  
  9.     {     
  10.         clock_gettime(CLOCK_REALTIME, &_tv_1);  
  11.     }     
  12.     ~Function_Exe_Time()  
  13.     {     
  14.         struct timespec _tv_2;  
  15.         clock_gettime(CLOCK_REALTIME, &_tv_2);  
  16.         unsigned long end=(unsigned long)_tv_2.tv_sec*1000000000L + _tv_2.tv_nsec;  
  17.         unsigned long begin=(unsigned long)_tv_1.tv_sec*1000000000L + _tv_1.tv_nsec;  
  18.         my_func.inc(_func, end-begin);  
  19.     }     
  20. };  


(3)定时器

[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. /** 
  2.  * \author cjy 
  3.  * \description 定时器 
  4.  * 固定时间间隔的定时器,方便对于时间间隔的判断,精确到毫秒级 
  5.  */  
  6. class timer  
  7. {  
  8. public:  
  9.     /** 
  10.     * \author cjy 
  11.     * \description构造函数 
  12.     * \param how_long 定时器的时间,单位:毫秒 
  13.     * \param first 有些定时器可能希望在启动时就可以执行一次,所以不能直接addDelay哦 
  14.     */  
  15.     explicit timer(const int64 how_long, bool first=falseconst int64 delay=0) : _long(how_long), _timer()  
  16.     {  
  17.         if(!first)  
  18.         _timer.addDelay(_long+delay);  
  19.     }  
  20.   
  21.     /** 
  22.     * \author cjy 
  23.     * \description构造函数 
  24.     * \param how_long 定时器的时间,单位:毫秒 
  25.     * \param first 有些定时器可能希望在启动时就可以执行一次,所以不能直接addDelay哦 
  26.     * \param ctv 当前时间 
  27.     */  
  28.     explicit timer(const int64 how_long, bool first , realtime &ctv) : _long(how_long), _timer(ctv)  
  29.     {  
  30.         if(!first)  
  31.         _timer.addDelay(_long);  
  32.     }  
  33.   
  34.     /** 
  35.     * \author cjy 
  36.     * \description重新设置定时器的精度和开始计时时间 
  37.     * \param how_long 定时器的时间,单位:毫秒 
  38.     * \param ctv 当前时间 
  39.     */  
  40.     void reset(const uint64 how_long, const realtime &cur)  
  41.     {  
  42.         _long = how_long;  
  43.         _timer = cur;  
  44.         _timer.addDelay(_long);  
  45.     }  
  46.   
  47.     /** 
  48.     * \author cjy 
  49.     * \description重新设置定时器的时间 
  50.     * \param cur 指定定时器启动的时间 
  51.     */  
  52.     void current(const realtime &cur)  
  53.     {  
  54.         _timer = cur;  
  55.     }   
  56.   
  57.     /** 
  58.     * \author cjy 
  59.     * \description延时定时器时间 
  60.     * \param cur 指定定时器启动的时间 
  61.     * \param delay 延时时间 
  62.     */  
  63.     void next(const realtime &cur, const uint32 delay)  
  64.     {  
  65.         _timer = cur;  
  66.         _timer.addDelay(delay);  
  67.     }   
  68.     /** 
  69.     * \author cjy 
  70.     * \description重新设置定时器的时间 
  71.     * \param cur 指定定时器启动的时间 
  72.     */  
  73.     void next(const realtime &cur)  
  74.     {  
  75.         _timer = cur;  
  76.         _timer.addDelay(_long);  
  77.     }   
  78.     /** 
  79.     * \author cjy 
  80.     * \description倒计时剩余秒数.不受时间调整影响. 
  81.     * \param cur 当前时间  
  82.     * return 剩余描述 
  83.     */  
  84.     inline uint32 leftSec(const realtime &cur)  
  85.     {  
  86.         return (_timer.sec() > cur.sec()) ? (_timer.sec() - cur.sec()) : 0;  
  87.     }   
  88.   
  89.     /** 
  90.     * \author cjy 
  91.     * \description倒计时剩余毫秒数.受时间调整影响 
  92.     * \param cur 当前时间  
  93.     * return 剩余值 
  94.     */  
  95.     inline uint64 leftMSec(const realtime &cur)  
  96.     {  
  97.         return (_timer._msec > cur._msec) ? (_timer._msec - cur._msec) : 0;   
  98.     }   
  99.   
  100.     /** 
  101.     * \author cjy 
  102.     * \description定时器检查 
  103.     * \param cur 检查定时器的时间 
  104.     * return 是否到达指定时间 
  105.     */  
  106.     inline bool operator() (const realtime &cur)  
  107.     {  
  108.         if (_timer._msec > cur._msec)  
  109.         {  
  110.             return false;  
  111.         }  
  112.         addDelay(cur);  
  113.         return true;  
  114.     }  
  115.   
  116.     inline void addDelay(uint64 addLong)//添加延迟  
  117.     {  
  118.         _timer.addDelay(addLong);  
  119.     }  
  120. private:  
  121.     /** 
  122.     * \author cjy 
  123.     * \description定时器时间间隔 
  124.     */  
  125.     uint64 _long;  
  126.     /** 
  127.     * \author cjy 
  128.     * \description上次检查定时器的时间 
  129.     */  
  130.     realtime _timer;  
  131. private:  
  132.     /** 
  133.     * \author cjy 
  134.     * \description非严格检测,存在积累误差 
  135.     * 从效率方面考虑,严格监测定时器的需求并不是必须的,去除对严格监测的支持 
  136.     */  
  137.     inline void addDelay(const realtime& cur)  
  138.     {  
  139.         _timer = cur;  
  140.         _timer.addDelay(_long);  
  141.     }  
  142. };  

(4)测试实例

测试ai攻击处理花费时间

[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. {  
  2.     Function_Exe_Time func("NPC_AI_ATTACK"STR(__LINE__));  
  3.     return doAttackAI();  
  4. }  

0 0
原创粉丝点击