python压测demo

import requests
import time
import concurrent.futures
import json


class APITest(object):

    def __init__(self):
        self.url = 'http://'
        self.token = ''
        self.headers = {'Content-Type': 'application/json',
                        'app_token': self.token}

        self.total_time = 0

        self.data = {

        }

    def common_get_events_list(self):
        t = time.time()
        ret = requests.post(url=self.url, headers=self.headers, data=json.dumps(self.data))
        # print(ret)
        end = (time.time() - t) % 60
        return end

    def time_total(self):
        with concurrent.futures.ThreadPoolExecutor(max_workers=5000) as executor:
            res = {executor.submit(self.common_get_events_list) for i in range(10)}

            for future in concurrent.futures.as_completed(res):
                try:
                    data = future.result()
                except Exception as exc:
                    pass
                else:
                    self.total_time += data
        print('common_get_events_list:接口响应的总时间为%s秒' % str(self.total_time))
        print('common_get_events_list:平均时间为%s秒' % str(self.total_time / 10))
        print('-' * 50)


if __name__ == '__main__':
    api = APITest()
    api.common_get_events_list()
    api.time_total()

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值