接口性能测试脚本

文件名:performance.py

# -*-coding:utf8-*-
# 性能测试基类
import re
import time
import requests
import threading


class 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 = 0
while 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_time
print u"最大响应时间:%s" % max_time
print u"最小响应时间:%s" % min_time
print 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" % total
print u"成功请求数:%s" % success_total
print u"失败请求数:%s" % fail_total
print u"异常请求数:%s" % except_total
print u"大于3秒的请求数:%s" % more_than_3_total
print u"小于3秒的请求数:%s" % less_than_3_total

运行结果
这里写图片描述

  • 4
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值