shell linux环境下内存压测
shell linux环境下内存压测
公司在测试prometheus检测node节点内存,但是不允许修改prometheus的阈值,只能用脚本模拟内存增加来测试
脚本详情
vim memory_usage.sh
#!/bin/bash
# Destription: testing usemem
# Example : sh memory_usage.sh usemem 500M | sh memory_usage.sh usemem 1G | sh memory_usage.sh flush
FILE_NAME=`basename $0`
memsize=$2
function usage()
{
echo "Usage:$FILE_NAME usemem memory_size|flush -----the value of memory_size like 100M 2G and etc"
echo "Example: $FILE_NAME usemem 1G"
echo " $FILE_NAME flush"
}
function usemem()
{
if [ -d /data/memory ];then
echo "/data/memory already exists"
else
mkdir /data/memory
fi
mount -t tmpfs -o size=$1 tmpfs /data/memory
dd if=/dev/zero of=/data/memory/block
}
function flush()
{
rm -rf /data/memory/block;ret=$?
if [ $ret != 0 ]; then
echo "remove memory data failed"
return $ret
fi
umount /data/memory;ret=$?
if [ $ret != 0 ]; then
echo "umount memory filedir failed"
return $ret
fi
rmdir /data/memory;ret=$?
if [ $ret != 0 ]; then
echo "remove memory filedir failed"
return $ret
fi
}
function main()
{
case "$1" in
usemem) consume $memsize;;
flush) flush;;
*) usage;exit 1;;
esac
}
main $*
用法
把脚本上传到服务器后
//这里以消耗机器2G内存为例
bash memory_usage.sh usemem 2G
这时候使用top或者free -m可以查看内存使用率,会发现已经涨上去了
//压测完成后,需要清理产生的文件,从而降低恢复内存
bash memory_usage.sh flush