python读取excel数据与做图——实现loadrunner自带的数据分析功能

上一篇 / 下一篇  2013-04-03 11:33:44 / 个人分类:Python

个人认为,
loadrunner自带的数据分析功能,本质上是为了将数据以最真实易懂的方式呈现。
算不得真正意义上的数据分析。
一个低成本的变通策略是:loadrunner收集数据并导出至excelpython读取数据分析并做呈现。

loadrunner导出数据的方法较为简单:
测试结束后,使用analysis功能得到报告,
在报告图中单击右键选择 set granularity,粒度设置为较小的数据,以导出尽可能多的数据。
报告图下方graph data处,选择save as或者cope all,保存至excel中。

Python组件安装
python读取excel使用xlrd,下载地址:http://download.csdn.net/detail/jakung/4435033
画图使用 matplotlib,安装方法http://blog.sina.com.cn/s/blog_6c07f2b601013vqp.html

matplotlib用法
主要使用 pyplot,类matlab语法。除了面向对象和需要import之外,其他语法和思路基本都一样。
1、面向对象。
python面向对象,matlab面向过程。
面向对象,意味着需要有一个对象才能获取属性。
以figure和设置坐标轴属性为例,
python中的figure同matlab中的figure,用于初始化一个figure对象。
在matlab中,设置坐标轴的语句一般紧跟在plot之后,或者需要使用figure(n)将需要的窗口激活。本质上,matlab没有对象的概念,只能设置当前过程中活动的figure的属性。
在python中,可以声明一个figure对象fig,然后plot,legend,axis,title等都是该对象的一个方法。通过fig.title()来实现。
2、import 包
一般使用以下两行即可:
import numpy as np
import matplotlib.pyplot as plt
后续调用方法时,以np或plt为前缀。
3、语法细节
支持matlab式的语法。
具体的细节参考官方文档:Matplotlib.for.Python.Developers
Chapter 2: Getting Started with Matplotlib 主要涉及grid,axis,label,legend,title,save to file.
Chapter 3: Decorate Graphs with Plot Styles and Types 主要涉及图像的线条颜色样式等控制。

xlrd用法
使用时需要明确一下对象及其属性的概念。
首先,通过
xlrd.open_workbook打开一个excel文件,得到1个book对象。
然后,通过book对象获取sheet,根据sheet获取row/column数据。
详细用法参加官方文档:python-excel

python实现loadrunner基本功能的案例
场景:loadrunner测量得到一组time-TPS数据。做图,x轴为time,y为TPS。
注:xlrd读取时间后存储为xl_date,暂不会xl_date转text,暂用纯文本代替真实数据。
原始数据与做图结果:
 

代码:

import numpy as np
import matplotlib.pyplot as plt
import xlrd

#open excel file and get sheet
myBook = xlrd.open_workbook(r'D:\src\testdata\testPlot.xls')
mySheet = myBook.sheet_by_index(0)

#get datas
time = mySheet.col(0)
time = [x.value for x in time]
tps = mySheet.col_values(1)

#drop the 1st line of the data, which is the name of the data.
time.pop(0)
tps.pop(0)

#declare a figure object to plot
fig = plt.figure(1)

#plot tps
plt.plot(tps)

#advance settings
plt.title('time-tps')
plt.xticks(range(len(time)),time)

#show the figure
plt.show()
 

TAG: Excel excel EXCEL Python python

小鱼咖啡 引用 删除 yangdanjun   /   2013-04-05 10:41:18
小功能蛮好的
 

评分:0

我来说两句

日历

« 2024-04-18  
 123456
78910111213
14151617181920
21222324252627
282930    

数据统计

  • 访问量: 16636
  • 日志数: 23
  • 建立时间: 2012-12-26
  • 更新时间: 2014-02-12

RSS订阅

Open Toolbar