接口性能测试脚本

来源:互联网 发布:淘宝ued是什么意思 编辑:程序博客网 时间:2024/05/18 00:20

文件名:performance.py

# -*-coding:utf8-*-# 性能测试基类import reimport timeimport requestsimport threadingclass Performance(threading.Thread):    def __init__(self, url="", method="get", header={}, body="", body_type="json"):        threading.Thread.__init__(self)        self.url = url        self.method = method        self.header = header        self.body = body        self.body_type = body_type    def run(self):        self.test_performance()    def test_performance(self):        start_time = time.time()        try:            response = self.send_request()            if response.status_code == 200:                status = "success"            else:                status = "fail"        except Exception, e:            print e            status = "except"        end_time = time.time()        spend_time = end_time - start_time        return status, spend_time    def send_request(self):        if re.search(self.method, 'GET', re.IGNORECASE):            response = requests.get(self.url, headers=self.header)        else:            if self.body_type == "json":                response = requests.post(self.url, headers=self.header, json=self.body)            elif self.body_type == "file":                response = requests.post(self.url, headers=self.header, files=self.body)            elif self.body_type == "data":                response = requests.post(self.url, headers=self.header, data=self.body)        return response

调用

# -*-coding:utf8-*-import performance# 取数组的百分比值,如90%响应时间# 90%响应时间的获取规则,参考loadrunner官方文档# 1. Sort the transaction instances by their value.# 2. Remove the top 10% instances.# 3. The highest value left is the 90th percentile.def get_percent_time(data_list, percent):    data_list = sorted(data_list)    if len(data_list)*(1-percent) <= 1:        r_length = 1    else:        r_length = len(data_list)*(1-percent)        r_length = int(round(r_length))    data_list = data_list[:len(data_list)-r_length]    return data_list[-1]# 设置并发数thread_count = 50# 所有线程花费时间列表spend_time_list = []# 最大响应时间max_time = 0# 最小响应时间min_time = 3600# 小于3秒的请求数less_than_3_total = 0# 大于3秒的请求数more_than_3_total = 0# 成功的请求数success_total = 0# 失败的请求数fail_total = 0# 异常的请求数except_total = 0# 总请求数total = 0# 请求地址url = "http://api-app.smartisan.com/app/index.php?r=api/v1_4/Recommend/List"# 请求头header = {"Market-Version": "3.1"}i = 0# 所有线程总花费时间time_total = 0while i < thread_count:    pf = performance.Performance(url=url, header=header)    status, spend_time = pf.test_performance()    spend_time_list.append(spend_time)    total += 1    if status == "success":        success_total += 1    elif status == "fail":        fail_total += 1    elif status == "except":        except_total += 1    if spend_time > max_time:        max_time = spend_time    if spend_time < min_time:        min_time = spend_time    if spend_time > 3:        more_than_3_total += 1    else:        less_than_3_total += 1    time_total += spend_time    pf.start()    i += 1# 平均响应时间avg_time = time_total/thread_count# 响应时间列表从小到大排序spend_time_list = sorted(spend_time_list)print u"平均响应时间:%s" % avg_timeprint u"最大响应时间:%s" % max_timeprint u"最小响应时间:%s" % min_timeprint u"90%%响应时间:%s" % (get_percent_time(spend_time_list, 0.9))print u"99%%响应时间:%s" % (get_percent_time(spend_time_list, 0.99))print u"80%%响应时间:%s" % (get_percent_time(spend_time_list, 0.8))print u"总请求数:%s" % totalprint u"成功请求数:%s" % success_totalprint u"失败请求数:%s" % fail_totalprint u"异常请求数:%s" % except_totalprint u"大于3秒的请求数:%s" % more_than_3_totalprint u"小于3秒的请求数:%s" % less_than_3_total

运行结果
这里写图片描述

2 0