服务器满负载稳定性测试,服务器简易高负荷暴力测试

1、[模拟高CPU占用率]

编写C程序占用CPU闲余空间:

#include

#include

#include

#include

int main()

{

pid_t pid;

while(1)

{

if((pid=fork()) <0)

continue;

else if(pid==0)

{

while(1)

{

}

}

else

continue;

}

return 0;

}

注意:以上程序将会使服务器CPU占用负荷提升至100%,且占用率不可调节;

使用top工具可以查看当前系统CPU占用率:

Cpu(s):

us 用户空间占用CPU百分比

sy 内核空间占用CPU百分比

ni 用户进程空间内改变过优先级的进程占用CPU百分比

id 空闲CPU百分比

wa 等待输入输出的CPU时间百分比

这里介绍一款限制程序CPU占用率的开源工具:cpulimiter。可以限制程序使用CPU的百分比;

svn checkout https://cpulimit.svn.sourceforge.net/svnroot/cpulimit/trunk cpulimit

cd cpulimit

./configure

make

然后可将生成的执行文件放置于/usr/bin/目录下,

再执行:cpulimit --exe FileName --limit 80

(将FileName程序的CPU占用率负荷限制到80%)

这样就可以随意模拟调试CPU负荷占用比率了。

2、[模拟高内存占用率]

编写C++程序,占用系统闲余内存空间:

#include

#include

#include

#include

#include

int main()

{

int timeuse=0;

struct timeval tpstart,tpend;

while (1)

{

gettimeofday(&tpstart,NULL);

while (1)

{

gettimeofday(&tpend,NULL);

timeuse = 1000000 * (tpend.tv_sec - tpstart.tv_sec) + (tpend.tv_usec - tpstart.tv_usec);

timeuse /= 1000;

//char * Logmsg = new char[65535];

char * Logmsg = new char[512];

}

sleep(20 / 1000);

}

return 0;

}

此程序将会使系统内存占满,且当内存被占满后系统将自行kill掉该程序进程;

通过top命令查看内存信息:

Mem:

total 物理内存总量

used 使用的物理内存总量

free 空闲内存总量

buffers 用作内核缓存的内存量

用以上方式做内存高占用率测试,有一定的缺陷,就是不能对内存占用率做限制调节,在此如果有哪位就限制并能调节进程内存占用的方法指点一二,本人将表示感谢。

3、[测试磁盘读写能力]

模拟磁盘文件高频率读写操作情况:

#include

#include

#include

#include

/*

*读写40M文件,读5ms写5ms,无限循环

*/

int main(void)

{

FILE *fp = NULL;

char* testRead = NULL;

char* testWrite = NULL;

int i = 0;

unsigned long ulFileLen = 0;

for(;;)

{

testWrite = (char *)malloc(1024*1024*50);

memset(testWrite, 0, sizeof(testWrite));

for(i=0;i<1024;i++)//4K=1024*4

{

strcat(testWrite, "Test");

}

fp = fopen("/tmp/testReadWrite1.ini", "w");

if(fp == NULL)

{

printf("Cannot Open This File !");

exit(1);

}

for(i=0;i<10*1024;i++)//40M=1024*4K*10

{

fwrite(testWrite,sizeof(testWrite),1024,fp);

}

fclose(fp);

free(testWrite);

usleep(5);

testRead = (char *)malloc(1024*1024*50);

memset(testRead, 0, sizeof(testRead));

fp = fopen("/tmp/testReadWrite1.ini", "r");

if(fp == NULL)

{

printf("Cannot Open This File !");

exit(1);

}

fseek(fp, 0, SEEK_END);

ulFileLen = ftell(fp);

fseek(fp, 0, SEEK_SET);

fread(testRead, sizeof(testWrite), ulFileLen/sizeof(testWrite), fp);

printf(">>>>>>>>>\n[TEST]READ:%s [%d]\n", testRead, ulFileLen);

fclose(fp);

free(testRead);

usleep(5);

}

return 0;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值