笔者在进行压测过程中,用shell 编写 怎么采集 heap 和 io 的性能数据,现分享如下:

loop_count=1
count=1

#sleep 3

echo -n "Please input pid:"
read cmd1 cmd2 cmd3

echo "" > heap${cmd1}.log
echo "" > heap
${cmd2}.log
echo "" > heap_${cmd3}.log

fun_io() {

echo "磁盘 I/O 采集数据开始! "
while (($loop_count <=10))
do 
   sleep 1  
   iostat -x 3 1 >> system.log 
   let "loop_count++"
done
echo "磁盘 I/0 采集数据结束!"

}

fun_mem() {

echo "HEAP 数据采集开始!"
while (($count <=3 ))
do
   sleep 5 

   if [ ${cmd1} -gt 1 ]
   then
       jmap -heap ${cmd1}  >> heap_${cmd1}.log
   else
       echo "没有输入pid1"
   fi

   sleep 1

   if [ ${cmd2} -gt 1 ]
   then
       jmap -heap ${cmd2}  >> heap_${cmd2}.log
   else
       echo "没有输入pid2"
   fi

   sleep 1

   if [ ${cmd3} -gt 1 ] 
   then
       jmap -heap ${cmd3}  >> heap_${cmd3}.log
   else
       echo "没有输入pid3"
   fi

   let "count++"
done
echo "HEAP 数据采集结束!"

}

fun_io
fun_mem