Chronic:服务端软件性能测试
工作中需要多不同软件测试使用性能,但目前市面上的工具用于做服务端的性能测试的工具基本都算是软件测试的附加功能
对于这个简单的需求,需要学习一个软件测试工具,有点得不偿失
找寻工具的过程中,发现python有个追踪执行时间的类库:chronic
官网简介:Chronic 介于简单的定时器和分析器。通过添加修饰符或包装代码语句来获得程序执行时间。Chronic 跟踪调用层次结构来告诉你时间块内执行其他的块。附加一个事件侦听器来记录你想要的时间。
用法很简单,提供2种调用方式,并且支持递归嵌套
import chronic @chronic.time def fun(): i= 0 i +=1 return fun() print(chronic.timings) def fun(): i = 0 with chronic.Timer('fun'): i+=1 return fun() print(chronic.timings)
以上2种结果一致
{'fun': {'count': 1, 'total_elapsed': 1.9199582217090955e-06, 'average_elapsed': 1.9199582217090955e-06}}
{'fun': {'count': 1, 'total_elapsed': 9.599791108545495e-07, 'average_elapsed': 9.599791108545495e-07}}
count:调用次数
total_elapsed:执行时间
average_elapsed:平均执行时间,也就是total_elapsed/count
如果是嵌套调用则会以层次结构返回结果
import chronic def fun1(): i = 0 with chronic.Timer('fun1'): i+=1 return @chronic.time def fun(): i= 0 i +=1 fun1() return fun() print(chronic.timings) {'fun': {'count': 1, 'total_elapsed': 1.5039672736721247e-05, 'timings': {'fun1': {'count': 1, 'total_elapsed': 9.599791108545478e-07, 'average_elapsed': 9.599791108545478e-07}}, 'average_elapsed': 1.5039672736721247e-05}}
嵌套的计时会存放在父级的timings内
chronic.timings则是全局计时存储,是dict格式
但光通过该模块并不能实现性能测试,性能测试还需自己编写测试代码,及测试纬度
但提供计时器无疑为性能测试提供了一个基准指标