魯德——Python開發性能測試腳本

測試開發工程師的工作主要是根據測試目標來完成,幫助測試人員完成測試目標,測試的業務需求是測試人員提出,但是由於環境的制約,手中沒有性能測試工具的時候,性能測試的工作又必須完成,測試開發工程師會按照測試人員提出需求來開發腳本,下面是我在poptest測試開發工程師就業培訓中寫的一個腳本,這套腳本進行了簡化,去掉教學腳本中伺服器性能指標的採集腳本部分,去掉了頻繁請求伺服器的次數腳本部分,去掉了thinktime的時長腳本等等。

Ceated on 2015-10-8

@author: laoli

create project: testingproject

import httplib

import urllib

import time

import json

class Transaction(object):

def __init__(self):

self.custom_timers = {}

def run(self):

conn = httplib.HTTPConnection("201.29.2.xxx:8080")

headers = {"Content-type": "application/json"} #application/x-www-form-urlencoded,"Aceept":"text/plain"

params = ({"bindHyCardInfo":{"mobileNo":"13426089279","userId":"2","hYCardno":line,"bankCardNo":"622xxxxxxxxxxxxx","ip":"201.29.2.xxx"},"header":{"version":"1.0.1","from":"1000","to":"2000","tid":line,"time":"12312","token":"SEW342WEER2342","ext":""}})

start = time.time()

conn.request("POST", "/core-oper/rest/bindHyCard", json.JSONEncoder().encode(params), headers)

response = conn.getresponse()

response_time = time.time()

data = response.read()

print data

conn.close()

transfer_time = time.time()

self.custom_timers[response received] = response_time - start

self.custom_timers[content transferred] = transfer_time - start

if __name__ == __main__:

file = open("d://card.txt")

while 1:

lines = file.readlines()

if not lines:

break

for line in lines:

line = line.strip(
)

trans = Transaction()

trans.run()

for timer in (response received, content transferred):

print %s: %.5f secs % (timer, trans.custom_timers[timer])

file.close()


推薦閱讀:

JMeter之旅01

TAG:Python | 性能測試 | 腳本 |