博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Windows IO 性能简单测试
阅读量:6577 次
发布时间:2019-06-24

本文共 4372 字,大约阅读时间需要 14 分钟。

转自:, 有改动。

 
#include 
#include 
#include 
#include
#pragma comment(lib,"ws2_32.lib")
 
ULONGLONG g_nReadCounts=0,g_nWriteCounts=0,g_nOtherCounts=0,g_nReadsBytes=0,g_nWriteBytes=0,g_nOtherBytes=0;
void PrintIODetails()
{
static DWORD lastTickCount;
static IO_COUNTERS lastIoCounters;
DWORD tickCount;
IO_COUNTERS ioCounters;
static int firstTime = 1;
 
tickCount = GetTickCount();
if (!GetProcessIoCounters(GetCurrentProcess(), &ioCounters)) {
return;
}
 
if (firstTime)
{
firstTime = 0;
printf("    Time    Reads   Writes   Others     Read(Bytes)    Write(Bytes)    Other(Bytes)\n");
printf("--------------------------------------------------------------------------------\n");
}
else
{
printf("%8lu%8llu%8llu%8llu%16llu%16llu%16llu\n",
tickCount - lastTickCount,
ioCounters.ReadOperationCount - lastIoCounters.ReadOperationCount,
ioCounters.WriteOperationCount - lastIoCounters.WriteOperationCount,
ioCounters.OtherOperationCount - lastIoCounters.OtherOperationCount,
ioCounters.ReadTransferCount - lastIoCounters.ReadTransferCount,
ioCounters.WriteTransferCount - lastIoCounters.WriteTransferCount,
ioCounters.OtherTransferCount - lastIoCounters.OtherTransferCount
);
 
g_nReadCounts += (ioCounters.ReadOperationCount - lastIoCounters.ReadOperationCount);
g_nWriteCounts += (ioCounters.WriteOperationCount - lastIoCounters.WriteOperationCount);
g_nOtherCounts += (ioCounters.OtherOperationCount - lastIoCounters.OtherOperationCount);
g_nReadsBytes += (ioCounters.ReadTransferCount - lastIoCounters.ReadTransferCount);
g_nWriteBytes += (ioCounters.WriteTransferCount - lastIoCounters.WriteTransferCount);
g_nOtherBytes += (ioCounters.OtherTransferCount - lastIoCounters.OtherTransferCount);
}
 
lastTickCount = tickCount;
memcpy(&lastIoCounters, &ioCounters, sizeof(IO_COUNTERS));
}
#define PACKCOUNT 10
#define BUFFSIEZ 50000
volatile bool bWorkThreadFinish = false;
void workerThread(void * dummy)
{
int i;
FILE * fp;
char buff[BUFFSIEZ];
WSADATA wsaData;
SOCKET s;
struct sockaddr_in localAddr;
struct sockaddr_in peerAddr;
 
memset(buff, 'X', sizeof(buff));
 
Sleep(2000);
 
// 写文件
fp = fopen("io.txt", "w");
if (fp) {
printf("fwrite = %d\n", fwrite(buff, 1, sizeof(buff), fp));
Sleep(10000);
// 关闭文件,测试Flush
printf("fclose\n");
fclose(fp);
Sleep(2000);
}
 
// 读文件
fp = fopen("io.txt", "r");
if (fp) {
printf("fread = %d\n", fread(buff, 1, sizeof(buff), fp));
fclose(fp);
Sleep(2000);
}
 
// 网络发
 
memset((void *)&localAddr, 0, sizeof(localAddr));
localAddr.sin_family = AF_INET;
localAddr.sin_addr.s_addr = htonl(INADDR_ANY);
localAddr.sin_port = htons(0);
memset((void *)&peerAddr, 0, sizeof(peerAddr));
peerAddr.sin_family = AF_INET;
peerAddr.sin_addr.s_addr = inet_addr("10.19.85.1");
peerAddr.sin_port = htons(3389);
 
printf("WSAStartup\n");
WSAStartup(MAKEWORD(2, 2), &wsaData);
Sleep(2000);
 
// UDP
printf("UDP Test:\n");
printf("socket\n");
s = socket(AF_INET, SOCK_DGRAM, 0);
Sleep(2000);
 
printf("bind\n");
bind(s, (struct sockaddr *)&localAddr, sizeof(localAddr));
Sleep(2000);
for (i = 0; i < PACKCOUNT; i++) {
printf("sendto = %d\n", sendto(s, buff, i * 100, 0, (SOCKADDR *) &peerAddr, sizeof(peerAddr)));
Sleep(2000);
}
printf("closesocket\n");
closesocket(s);
Sleep(2000);
 
// TCP
printf("TCP Test:\n");
printf("socket\n");
s = socket(AF_INET, SOCK_STREAM, 0);
Sleep(2000);
 
printf("bind\n");
bind(s, (struct sockaddr *)&localAddr, sizeof(localAddr));
Sleep(2000);
 
printf("connect\n");
connect(s, (SOCKADDR *) &peerAddr, sizeof(peerAddr));
Sleep(2000);
 
printf("send = %d\n", send(s, buff, sizeof(buff), 0));
Sleep(2000);
 
printf("closesocket\n");
closesocket(s);
Sleep(2000);
 
printf("WSACleanup\n");
WSACleanup();
 
bWorkThreadFinish = true;
}
 
UINT _stdcall ThreadProc(PVOID pv)
{
workerThread(pv);
return 1;
}
//HANDLE g_hEvt = NULL;
int main(int argc, char ** argv)
{
// 启动工作线程
::_beginthreadex(NULL,0,ThreadProc,NULL,0,0);
 
//g_hEvt = ::CreateEventW(NULL,FALSE,FALSE,NULL);
 
 
printf("Ctrl-C to exit\n\n");
for (;;)
{
PrintIODetails();
Sleep(1000);
if (bWorkThreadFinish)
{
break;
}
}
printf("*********************************Total*************************************\n");
printf("    Reads   Writes   Others     Read(Bytes)    Write(Bytes)    Other(Bytes)\n");
printf("%8llu%8llu%8llu%16llu%16llu%16llu\n",
g_nReadCounts,
g_nWriteCounts,
g_nOtherCounts,
g_nReadsBytes,
g_nWriteBytes,
g_nOtherBytes
);
return 0;
}
 

转载于:https://www.cnblogs.com/qinfengxiaoyue/p/3712227.html

你可能感兴趣的文章
百度地图
查看>>
PHP 变色验证码实例
查看>>
XPC
查看>>
《Concise课程表》开发过程总结
查看>>
Mysql Explain 详解
查看>>
[java基础]一文理解java多线程必备的sychronized关键字,从此不再混淆!
查看>>
mongodb副本集部署
查看>>
关于图片或者文件在数据库的存储方式归纳
查看>>
BigInteger的简单用法
查看>>
mysql创建utf-8字符集数据库
查看>>
Apache的443端口被占用解决方法
查看>>
XenMobile 9.0完全部署指南之安装第一台DeviceManager--04
查看>>
shiro添加错误提示信息
查看>>
小巧单据打印管理软件
查看>>
JVM原理讲解和调优
查看>>
你为什么一定要去美国?给我一个理由
查看>>
JS -------------------设置弹出框位置屏幕的中间
查看>>
几个内核有关的命令
查看>>
第一章 大网 GNS3 使用
查看>>
性能测试 vbs使用(一)
查看>>