Windows性能计数器统计网络流量
来源:互联网 发布:python中的appends 编辑:程序博客网 时间:2024/06/07 02:35
使用性能计数器的基本步骤是:
1.打开计数器PdhOpenQuery
1.打开计数器PdhOpenQuery
2.为计数器句柄分配空间
3.把感兴趣的计数器添加进来PdhAddCounter4.收集数据PdhCollectQueryData
5.得到计数器的数值PdhGetFormattedCounterValue
6.关闭计数器PdhCloseQuery
#include <iostream>#include <Pdh.h>#include <list>#include <string>#include <numeric>#pragma comment(lib, "Pdh.lib")using namespace std; void main(){ list< HCOUNTER> m_counters; HQUERY m_hQuery; string counter_list; string instance_list; DWORD clistlen = 0; DWORD ilistlen = 0; const char * netface = "Network Interface"; const char * in_speed = "Bytes Received/sec"; const char * out_speed = "Bytes Sent/sec"; list< double> m_net_total_in_bps; list< double> m_net_total_out_bps; PdhEnumObjectItemsA(0, 0, netface, 0, &clistlen, 0, &ilistlen, PERF_DETAIL_WIZARD, 0); counter_list.assign(clistlen, 0); instance_list.assign(ilistlen, 0); if (ERROR_SUCCESS != PdhEnumObjectItemsA(0, 0, netface, &counter_list[0], &clistlen, &instance_list[0], &ilistlen, PERF_DETAIL_WIZARD, 0)) return; PDH_STATUS pdhStatus; pdhStatus = PdhOpenQuery (0, 0, &m_hQuery); if ( pdhStatus != ERROR_SUCCESS ) return; char *tmpsz = &instance_list[0]; // 枚举网卡 for (; *tmpsz!=0; tmpsz+=(strlen(tmpsz)+1)) { string query_obj_in = string("\\") + netface + "(" + tmpsz + ")" + "\\" + in_speed; string query_obj_out = string("\\") + netface + "(" + tmpsz + ")" + "\\" + out_speed; HCOUNTER tmpcounter; pdhStatus = PdhAddCounterA( m_hQuery , query_obj_in.c_str(), 0 , &tmpcounter ) ; if ( pdhStatus != ERROR_SUCCESS ) { PdhCloseQuery(m_hQuery) ; return; } m_counters.push_back(tmpcounter); pdhStatus = PdhAddCounterA(m_hQuery, query_obj_out.c_str(), 0 , &tmpcounter) ; if (pdhStatus != ERROR_SUCCESS) { PdhCloseQuery (m_hQuery) ; return; } m_counters.push_back(tmpcounter); } // 统计流量,每秒一次 while(1) { PDH_FMT_COUNTERVALUE fmtValue; DWORD dwctrType; pdhStatus = PdhCollectQueryData(m_hQuery); if (pdhStatus != ERROR_SUCCESS) { return; } double in_bps = 0.0; double out_bps = 0.0; double in_avg_bps = 0.0; double out_avg_bps = 0.0; // 遍历每块网卡,把流量加起来 for (list< HCOUNTER>::iterator iter = m_counters.begin(); iter != m_counters.end(); iter++) { pdhStatus = PdhGetFormattedCounterValue(*iter , PDH_FMT_DOUBLE , &dwctrType, &fmtValue ) ; if ( pdhStatus == ERROR_SUCCESS ) { in_bps += fmtValue.doubleValue; } iter ++; pdhStatus = PdhGetFormattedCounterValue(*iter, PDH_FMT_DOUBLE, &dwctrType, &fmtValue ) ; if ( pdhStatus == ERROR_SUCCESS ) { out_bps += fmtValue.doubleValue; } } m_net_total_in_bps.push_back(in_bps); m_net_total_out_bps.push_back(out_bps); if (m_net_total_in_bps.size() > 10) m_net_total_in_bps.pop_front(); if (m_net_total_out_bps.size() > 10) m_net_total_out_bps.pop_front(); in_avg_bps = accumulate(m_net_total_in_bps.begin(), m_net_total_in_bps.end(), 0.0) / m_net_total_in_bps.size(); out_avg_bps = accumulate(m_net_total_out_bps.begin(), m_net_total_out_bps.end(), 0.0) / m_net_total_out_bps.size(); cout << "IN = " << in_avg_bps/1024 << "KBps" << endl; cout << "OUT = " << out_avg_bps/1024 << "KBps" << endl; Sleep(1000); }}
原文地址:http://www.sizeof.cn/html/2009/76.html
- Windows性能计数器统计网络流量
- Windows 性能计数器
- Windows性能计数器解释
- Windows性能计数器
- Windows常见性能计数器
- Windows性能计数器
- Windows常见性能计数器
- windows 性能计数器
- Windows常见性能计数器
- Windows 性能计数器
- Windows性能计数器分析
- Windows常见性能计数器
- Windows常见性能计数器
- windows 性能计数器
- Windows性能计数器解释
- Windows常见性能计数器
- 统计网络流量
- 网络流量统计
- Eclipse 4.3 Kepler最快汉化方法
- spring3.0的 引入属性文件的两种方法再加一种方法
- 关于推送未解之谜
- Flex 组件生命周期概述
- hdu1195双向广搜
- Windows性能计数器统计网络流量
- LRU算法介绍
- 几个 JS 函数分享
- 反射字段属性
- android蓝牙文件传输的实现
- 自定义数字格式字符串
- Qt Designer工具与uic的使用
- android notification and notificationmanager
- python2.7 使用super关键词 报错 TypeError: must be type, not classobj 解决办法