shell模拟程序消耗CPU资源

来源:互联网 发布:linux启动.sh命令 编辑:程序博客网 时间:2024/04/30 00:19

背景:

应急方案预演:在做分布式系统的时候,部分系统资源比较空闲(性能好)。在测试高可用场景中想摸底整个系统在全部在高负载的情况下:系统的容灾、主备切换、冗余服务器拉起等,需要将服务器短板资源全部达到预警值附近。

方案一:

根据测试情况,起停对应集群的节点
优缺点:该方案操作繁琐,且破坏了场景的完整性;在停止不当的情况下可能造成业务失败

方案二:

模拟消耗资源
程序:多线程模拟,需要根据服务器的配置动态能够调整
优缺点:不动程序,只消耗资源;当然操作也会造成司机,可能会全部占满计算资源,导致计算机无法工作(可以稍作修改:将入参做个判断使它最大保持90这样就不会因为误操作导致沾满计算资源)

shell实现

#!/bin/bash#定义生成的c文件名称和可执行程序名称fileName="cpu_use"#定义使用方法说明usage(){  echo "./`basename $0` <start|stop|adjust> <CPU_Rate> [sleeptime/worktime] [adjustRange]"  echo ""  echo "start:按照核的数量启动 $fileName"  echo "stop:停止所有 $fileName"  echo "adjust:自动调整占用CPU的使用率"  echo "CPU_Rate:占用cpu比率"  echo "sleeptime/worktime:CPU空闲和工作时间的占比 可以通过该参数调整"  echo "adjustRange:允许CPU的波动范围 即curentCPU-adjustRange CPU_Rate curentCPU+adjustRange就不再调整"  echo ""  exit }#判定参数的个数,参数不能少于1个,且必须为限定参数:start,stop,adjustif [ $# -lt 1 ]then  usagefi#设置需要占用的CPU比率,默认为50%if [ "$2" == "" ]then  CPU_Rate=50else  CPU_Rate=$2fi#设置允许的波动范围,默认为5if [ "$4" == "" ]then  adjustRange=5else  adjustRange=$4fi#停止#没有参数stop_thread(){    if [ `ps -ef|grep $fileName|grep -v grep|awk '{print $2}'|wc -l` -ne 0 ]    then      ps -ef|grep $fileName|grep -v grep|awk '{print $2}'|xargs kill -9    fi}#创建#参数一个:cpu空闲时间和工作时间的比率,默认是1,对应脚本入参的第三个参数[sleeptime/worktime]start_thread(){    if [ "$1" == "" ]    then      rate=1    else      rate=$1    ficat <<EOF > $fileName.c    #include <time.h>    #include <sys/time.h>    #include <unistd.h>    #include<stdlib.h>    #include<math.h>    #define DWORD unsigned long      #define UINT64 unsigned long long      const int INTERVAL = 300;    int main(int argc, char* argv[] )    {        struct timeval tms;        int half = INTERVAL/2, i;        clock_t startTime = 0;        while(1)        {            timerclear(&tms);            gettimeofday(&tms,NULL);            UINT64 startTime = tms.tv_usec;            while(1)            {                timerclear(&tms);                gettimeofday(&tms,NULL);                UINT64 nowTime = tms.tv_usec;                if((nowTime - startTime)/1000 > INTERVAL)                    break;            }            if(usleep(INTERVAL*1000*$rate))                exit(-1);            }        return 0;    }EOF    echo "编译 $fileName.c ... "    gcc $fileName.c -o $fileName    if [ $? -eq 0 ]; then        echo "执行$fileName 开始... "        echo        cpuNum=`cat /proc/cpuinfo |grep processor|wc -l`        for i in `seq 1 $cpuNum`        do        echo "  ... 执行$fileName 第 "$i"次开始 ... "        ./$fileName &        echo "  ... 执行$fileName 第 "$i"次结束 ... "        echo        done        echo "执行$fileName 结束... "      echo ""    else        echo "编译 $fileName.c ERROR! "    fi}#自动调整cpu的使用率,使其满足CPU_Rate# 第一个:sleep/worktimes=1adjustment(){  stop_thread  start_thread $1  cur_cpu=`mpstat 1 5|awk '{if(NR==8){print $4}}'`  if [ "$cur_cpu" \< "$(expr $CPU_Rate - $adjustRange)" -o "$cur_cpu" \> "$(expr $CPU_Rate + $adjustRange)" ]  then    echo "======期望CPU使用率:$CPU_Rate=====当前CPU使用率:$cur_cpu==========开始第【$((times++))】次调整==========="    echo ""    adjustment $(expr $cur_cpu/$CPU_Rate*$1)  else    echo "======期望CPU使用率:$CPU_Rate=====当前CPU使用率:$cur_cpu==========结束调整并退出========="    echo ""  fi}if [ $1 == 'start' ]then  stop_thread  start_thread $3fiif [ $1 == 'adjust' ]then  stop_thread  adjustment $3fiif [ $1 == 'stop' ]then  stop_threadfi
原创粉丝点击