network

#include <QCoreApplication>
#define UNICODE
#include <windows.h>
#include <tchar.h>
#include <Pdh.h>
#include <pdhmsg.h>
#include <QDebug>


#pragma comment(lib,"pdh.lib")


int getcpuload()
{
    static PDH_STATUS            status;
    static PDH_FMT_COUNTERVALUE  value;

    static PDH_FMT_COUNTERVALUE  value2;

    static HQUERY                query;
    static HCOUNTER              counter;

    static HCOUNTER              counter2;

    static DWORD                 ret;
    static char                  runonce=1;
    int                         cput=0;
    int                         bandwidth=0;//带宽
//    int                         bandwidth_utilition=0;//带宽利用率

    if(runonce)
    {
        status = PdhOpenQuery(NULL, 0, &query);
        if(status != ERROR_SUCCESS)
        {
            printf("PdhOpenQuery() ***Error: 0x%X\n",status);
            return 0;
        }
        counter = (HCOUNTER *)GlobalAlloc(GPTR, sizeof(HCOUNTER));

        //        PdhAddCounter(query, LPCWSTR(QString("\\Processor Information(_Total)\\% Processor Time").utf16()), 0,&counter); // A total of ALL CPU's in the system(1)

        //PdhAddCounter(query, TEXT("\\Processor Information(_Total)\\% Processor Time"), 0,&counter);
        //PdhAddCounter(query, TEXT("\\Processor Information(_Total)\\% C1 Time"), 0,&counter);


        //PdhAddCounter(query, TEXT("\\Processor Information(_Total)\\Average Idle Time"), 0,&counter);


        PdhAddCounter(query, TEXT("\\Network Interface(Realtek PCIe GBE Family Controller)\\Bytes Total/sec"), 0,&counter);

        PdhAddCounter(query, TEXT("\\Network Interface(Realtek PCIe GBE Family Controller)\\Current Bandwidth"), 0,&counter2);



        //PdhAddCounter(query, TEXT("\\Processor(0)\\% Processor Time"),0,&counter);    // For systems with more than one CPU (Cpu0)
        //PdhAddCounter(query, TEXT("\\Processor(1)\\% Processor Time"),0,&counter);    // For systems with more than one CPU (Cpu1)
        runonce=0;
        PdhCollectQueryData(query); // No error checking here
        return 0;
    }
    printf("Error\n");
    status = PdhCollectQueryData(query);
    if(status != ERROR_SUCCESS)
    {
        printf("PhdCollectQueryData() ***Error: 0x%X\n",status);
        if(status==PDH_INVALID_HANDLE)
            printf("PDH_INVALID_HANDLE\n");
        else if(status==PDH_NO_DATA)
            printf("PDH_NO_DATA\n");
        else
            printf("Unknown error\n");
        return 0;
    }
    status = PdhGetFormattedCounterValue(counter, PDH_FMT_DOUBLE | PDH_FMT_NOCAP100 ,&ret, &value);

    status = PdhGetFormattedCounterValue(counter2, PDH_FMT_DOUBLE | PDH_FMT_NOCAP100 ,&ret, &value2);


    if(status != ERROR_SUCCESS)
    {
        printf("PdhGetFormattedCounterValue() ***Error: 0x%X\n",status);
        return 0;
    }
    cput = value.doubleValue;

    bandwidth=value2.doubleValue;

    //printf("\n\n" "CPU Total usage: %3d%%\n",cput);


    qDebug()<<"\n CPU Total usage: "<<cput<<endl;

    qDebug()<<"\n CPU2 Total usage: "<<bandwidth<<endl;
    Sleep(3000);
    int bandwidth_utilition=(cput*8)/bandwidth;

    qDebug()<<"\n Bandwidth utilization : "<<(cput*8.0/bandwidth)<<endl;

    return cput;
}
int main(int argc, char *argv[])
{
    QCoreApplication a(argc,argv);

    //Entry point
    while(1)
    {

        getcpuload();
        Sleep(1000);
    }

    return a.exec();
}



QT += core
QT -= gui

CONFIG += c++11

LIBS += -lPdh

TARGET = performance
CONFIG += console
CONFIG -= app_bundle

TEMPLATE = app

SOURCES += main.cpp

# The following define makes your compiler emit warnings if you use
# any feature of Qt which as been marked deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS

# You can also make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值