Python自动化训练营2-三剑客实战Linux性能统计分析

课后作业

  • 统计某个进程的性能,输出20s内aliyundun进程每秒的cpu和mem,并最后空出一行统计平均性能。

课后调查表单

参考

top -b -d 1 -n 20 | grep --line-buffered -i yundun$ | awk 'BEGIN{print "cpu","mem"}{cpu+=$9;mem+=$10;print $9,$10}END{printf "\ncpu: %s mem:%s\n",cpu/NR,mem/NR}'
1 Like
(top -n 20 -d 1| grep 'AliYunDun'|awk '{print $10 "\t" $11}') > info.txt && sed -e '1i cpu mem' info.txt &&sed -e '$a AVG' info.txt && awk '{cpunum+=$1}{memnum+=$2}END{print cpunum/NR"\t" memnum/NR}' info.txt

image

2 Likes

输出20s内AliYuDun进程每秒的cpu和mem,并最后空出一行统计平均性能:

AliYuDun PID:1078
  • CPU平均值:1
  • MEM平均值:0.5
PS:由于没想到也不会组合 :joy: :joy:所以就写了两段代码:
  • CPU:
top -d 1 -n 20 -p 1078 | grep --line-buffered 1078 | awk 'BEGIN{print "cpu:"}{print $10}{cpu=$10+cpu}END{printf "\ncpu:%s\n",cpu/NR}'

image

  • MEM:
top -d 1 -n 20 -p 1078 | grep --line-buffered 1078 | awk 'BEGIN{print "mem:"}{print $11}{mem=$11+mem}END{printf "\nmem:%s\n",mem/NR}'

image

1.找到aliyundun的进程

ps -ef |grep -i aliyundun

image
2.统计性能

top -p 1078 -n 20 -d 1|grep 1078 |awk '{print $10,$11}END{print $10/NR,$11/NR}'

image

top -b -n 20 -d 1 -p 1078|grep --line-buffered 1078|awk 'BEGIN{print "cpu","mem"}{print $9,$10}{cpu=$9+cpu}{mem=$10+mem}END{print cpu/NR,mem/NR}'

top -p 1078 -n 20 -d 1 |grep --line-buffered 1078 |awk 'BEGIN{print "cpu","mem"}{print $10,$11}{cpusum=cpusum+$10;memsum=memsum+$11}END{printf "cpuavg=%.2f\tmemavg=%.2f\n",cpusum/NR,memsum/NR}'

top -p 1078 -n 20 -d 1 |grep --line-buffered 1078 |awk 'BEGIN{print "cpu:,mem:"}{print $(NF-4),$(NF-3)}{cpu=$(NF-4)+cpu}{mem=$(NF-3)+mem}END{print"cpuavg=" cpu/NR,"memavg="mem/NR}'

统计某个进程的性能,输出20s内aliyundun进程每秒的cpu和mem,并最后空出一行统计平均性能
1.搜索aliyundun的进程ID号

ps -ef|grep -i aliyundun

1 搜索ailiyundun的进程ID
2.统计性能

top -d 1 -n 20 -p 1078|grep --line-buffered 1078|awk 'BEGIN{print "cpu","mem"}{print $10,$11}{cpu=$10+cpu}{mem=$11+mem}END{print "cpu:"cpu/NR,"mem:"mem/NR}'

top -d 1 -n 20 -p 1078|grep --line-buffered 1078|awk 'BEGIN{print "cpu","men"}{print $10,$11}{cpu=$10+cpu}{men=$11+men}END{print "\n""cpu:"cpu/NR,"men:"men/NR}'

关闭