wordpress做网站手机,网站的icp备案信息,海宁网站建设公司推荐,西安网站制作网站目录
CPU压测
内存压测 本文主要是编写了shell脚本#xff0c;对Linux系统进行CPU和内存的压测。
CPU压测
[rootlocalhost ~]# cat cpu_stress_test.sh
#!/bin/bash # 定义压测CPU的函数
function test_cpu() { # 初始化时间变量 local time # 获取参数 while geto…目录
CPU压测
内存压测 本文主要是编写了shell脚本对Linux系统进行CPU和内存的压测。
CPU压测
[rootlocalhost ~]# cat cpu_stress_test.sh
#!/bin/bash # 定义压测CPU的函数
function test_cpu() { # 初始化时间变量 local time # 获取参数 while getopts :t:h opt; do case $opt in t) # 使用OPTARG而不是OPT注意大小写 time$OPTARG ;; h) echo Usage: $0 -t time echo -t time The time to test the CPU in seconds. exit 0 ;; \?) echo Invalid option: -$OPTARG 2 echo Usage: $0 -t time exit 1 ;; :) echo Option -$OPTARG requires an argument. 2 echo Usage: $0 -t time exit 1 ;; esac done # 检查时间是否已设置 if [[ -z $time ]]; then echo Error: Missing -t argument. exit 1 fi # 转换为整数并检查是否为正数 if ! [[ $time ~ ^[0-9]$ ]]; then echo Error: Time must be a positive integer. exit 1 fi # 开始压测 echo 开始压测CPU持续时间为 $time 秒... # 这里可以添加实际的压测命令 # 例如(yes testing cpu | md5sum) PID$! ... yes /dev/null 21 PID$!sleep $timekill $PID /dev/null# 等待压测完成或用户中断 wait $PID /dev/nullecho CPU压测已停止
} # 检查是否传递了参数给脚本如果没有则显示帮助手册
if [[ $# -eq 0 ]]; then test_cpu -h exit 0
fi # 调用test_cpu函数处理命令行参数
test_cpu $[rootlocalhost ~]# chmod x cpu_stress_test.sh
[rootlocalhost ~]# ./cpu_stress_test.sh -t 30
开始压测CPU持续时间为 30 秒...
CPU压测已停止内存压测
[rootlocalhost ~]# cat memory_stress_test.sh
#!/bin/bashdirname/tmp/$RANDOMcmd_help() {echo -h : 查看帮助echo -m : 指定压测内存大小 单位 M Gecho -t : 指定压测时间默认单位是秒echo 示例$0 -m 1G -t 30
}mem_test() {mkdir $dirnamemount -t tmpfs -o size${size} tmpfs ${dirname}echo -e \033[31m 开始进行内存压测 \033[0m dd if/dev/zero of${dirname}/${RANDOM} bs${size} count1 /dev/nullsleep $time
}mem_clean() {echo -e \033[31m 压测结束清理内存 \033[0m rm -rf ${dirname}/*umount ${dirname}rm -rf ${dirname}
}if [ $# -eq 0 ];then cmd_helpexit 1
fi while getopts hm:t: opt; docase ${opt} inh)cmd_help;;m)size${OPTARG};;t)time${OPTARG};;*)echo Unknown option: ${opt}echo Please use: $0 -h;;esac
donemem_test
mem_clean[rootlocalhost ~]# chmod x memory_stress_test.sh
[rootlocalhost ~]# ./memory_stress_test.sh -m 1G -t 30开始进行内存压测 压测结束清理内存