本人最近接到一个服务器性能测试需求,在做完测试准备执行完测试用例之后,在处理测试数据的时候使用的python图形化工具plotly,之前写过一些脚本都是实现教程的代码,借此机会正好进行了方法的封装。分享代码,供大家参考。

下面是python读取文件的类代码:

#!/usr/bin/python
2# coding=utf-8
3
4from second import Violin as vv
5
6
7class Fission:
8 z = []
9 x = []
10
11 def __init__(self):
12 print "欢迎使用fission类!"
13
14 def getData(self, name):
15 size = 0;
16 with open("/Users/Vicky/Documents/workspace/api_test/long/" + name + ".log") as apidata:
17 for i in apidata:
18 data = i.split("\r\n")[0]
19 time = float(data)
20 if time > 2: continue
21 self.z.append(data)
22 size += 1;
23 length = size;
24 for i in range(length):
25 self.x.append(name + "线程")
26
27
28if __name__ == "__main__":
29 vvv = vv.Violin()
30 fission = Fission()
31 for i in range(10,23,1):
32 aa = str((i + 1) * 10)
33 fission.getData(aa)
34 vvv.makeViolin(fission.z, fission.x, "接口性能测试", "110-230")

下面是生成voilin图标的方法封装:

#!/usr/bin/python
2# coding=utf-8
3
4import plotly.plotly
5import pandas as pd
6import plotly.figure_factory as ff
7
8
9class Violin:
10 def __init__(self):
11 print "violin图表生成类!"
12
13 def makeViolin(self, score, group, title, name):
14 data = pd.DataFrame(dict(Score=score, Group=group))
15 fig = ff.create_violin(data, data_header='Score', group_header='Group', height=700, width=1200, title=title)
16 plotly.offline.plot(fig, filename=name + ".html")

下面是生成的图标的截图:


python plotly处理接口性能测试数据方法封装_软件测试

下面是QPS的图截图:

python plotly处理接口性能测试数据方法封装_python_02