有些时候纯粹用shell似乎感觉有点力不从心,在python的帮助下似乎简单很多
using following commands:
1
| echo '{"foo": "bar"}' | python -m json.tool
|
analyze results from timer
在系统的performance的相关问题中常常需要测性能,比如某个部分的时间,之后设置好的timer会打印出很多的格式化的数据存在log中。之前的时候常常用shell过滤出来所需要的log信息然后放在excel中处理,但是效率还是低,而且linux环境下使用excel不很方便,于是就采用shell+python结合起来的方式。
比如log中的信息如下
1 2 3
| timer1: the time 1.0 timer1: the time 2.0 timer1: the time 3.0
|
这个时候可以用如下的bash先讲data过滤出来
1
| cat testdata.txt |cut -d " " -f 4
|
之后再将这些数据使用pip传送给python scripts 比如
1
| cat testdata.txt |cut -d " " -f 4 | python3 analysis.py
|
具体的python scripts如下,需要对于list添加什么样的运算都可以灵活处理,简单直接
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| import sys import statistics
data_list = [] real_data_list = [] def isfloat(value): try: float(value) return True except ValueError: return False
for line in sys.stdin: new_list = [(elem) for elem in line.split()] if(len(new_list)>0): data_list.append(new_list[0])
#print (data_list) # delete useless string for d in (data_list): if isfloat(d): real_data_list.append(float(d))
#print (real_data_list) print ("len", len(real_data_list)) print ("avg", statistics.mean(real_data_list)) print ("min", min(real_data_list) ) print ("max", max(real_data_list) ) print ("std",statistics.stdev(real_data_list)) print ("sum",sum(real_data_list))
|