接口压力测试脚本

1、单进程多线程模式

# #!/usr/bin/env python
# # -*- coding:utf-8 -*- import time
import logging
import requests
import threading
from concurrent import futures # download_url = 'http://192.168.188.110:8081//workspace/record_download/polls/82003533467_18b305da-e313-11e8-aa39-00163e0a6bde.mp3'
# download_url = 'http://192.168.188.110:8081//workspace/record_download/polls/test.log'
download_url = 'http://192.168.188.110:8081//workspace/record_download/polls/9921_057128214999_18210532807_20181113110420_00163e104dbfbb8b11e8e6f0d0990876(3).wav'
workers = 1000
mutex = threading.Lock()
session = requests.Session()
contain = {'average_cost':0,'min_cost':0,'max_cost':0,'hit_count':0} def handle(cost):
with mutex:
min_cost = contain['min_cost']
max_cost = contain['max_cost']
hit_count = contain['hit_count']
average_cost = contain['average_cost']
if min_cost == 0:
contain['min_cost'] = cost
if min_cost > cost:
contain['min_cost'] = cost
if max_cost < cost:
contain['max_cost'] = cost
average_cost = (average_cost*hit_count + cost) / (hit_count + 1)
hit_count +=1
contain['average_cost'] = average_cost
contain['hit_count'] = hit_count
logging.info(contain) def download_one():
while True:
try:
stime = time.time()
request = requests.Request(method='GET', url=download_url,)
prep = session.prepare_request(request)
response = session.send(prep, timeout=100)
etime = time.time()
# print(response.content)
logging.info('thread[%s] status[%s] cost[%s]',threading.current_thread().ident,
response.status_code,etime-stime)
handle(float(etime-stime))
except Exception as e:
logging.error(e)
print(e) def main():
with futures.ThreadPoolExecutor(workers) as executor:
for i in range(workers):
executor.submit(download_one) if __name__ == '__main__':
logging.basicConfig(filename="client.log", level=logging.INFO,
format="%(asctime)s [%(filename)s:%(lineno)d] %(message)s", datefmt="%m/%d/%Y %H:%M:%S [%A]")
main()

2、多进程多线程模式

# #!/usr/bin/env python
# # -*- coding:utf-8 -*- import os
import time
import logging
import requests
import threading
from multiprocessing import Lock,Manager
from concurrent import futures download_url = 'http://192.168.188.105:8888'
workers = 250
cpu_count = 4 session = requests.Session() def handle(cost,mutex,contain):
with mutex:
min_cost = contain['min_cost']
max_cost = contain['max_cost']
hit_count = contain['hit_count']
average_cost = contain['average_cost']
if min_cost == 0:
contain['min_cost'] = cost
if min_cost > cost:
contain['min_cost'] = cost
if max_cost < cost:
contain['max_cost'] = cost
average_cost = (average_cost*hit_count + cost) / (hit_count + 1)
hit_count +=1
contain['average_cost'] = average_cost
contain['hit_count'] = hit_count
logging.info(contain) def download_one(mutex,contain):
while True:
try:
stime = time.time()
request = requests.Request(method='GET', url=download_url,)
prep = session.prepare_request(request)
response = session.send(prep, timeout=50)
etime = time.time()
print(response.status_code)
logging.info('process[%s] thread[%s] status[%s] cost[%s]',os.getpid(),threading.current_thread().ident,
response.status_code,etime-stime)
handle(float(etime-stime),mutex,contain)
# time.sleep(1)
except Exception as e:
logging.error(e)
print(e) def new_thread_pool(mutex,contain):
with futures.ThreadPoolExecutor(workers) as executor:
for i in range(workers):
executor.submit(download_one,mutex,contain) def subprocess():
manager = Manager()
mutex = manager.Lock()
contain = manager.dict({'average_cost': 0, 'min_cost': 0, 'max_cost': 0, 'hit_count': 0}) with futures.ProcessPoolExecutor(cpu_count) as executor:
for i in range(cpu_count):
executor.submit(new_thread_pool,mutex,contain) if __name__ == '__main__':
logging.basicConfig(filename="client.log", level=logging.INFO,
format="%(asctime)s [%(filename)s:%(lineno)d] %(message)s", datefmt="%m/%d/%Y %H:%M:%S [%A]")
subprocess()

  

Python开发【笔记】:接口压力测试的更多相关文章

  1. 使用Loadrunner进行http接口压力测试

    业务描述: 在业务系统里进行查询操作,查询的结果是通过请求http接口,从系统中处理并将结果以json字符串返回. 本文就讲述使用Loadrunner对此类接口进行压力测试并记录相关的性能指标数据: ...

  2. 学习总结——JMeter做http接口压力测试

    JMeter做http接口压力测试 测前准备 用JMeter做接口的压测非常方便,在压测之前我们需要考虑这几个方面: 场景设定 场景分单场景和混合场景.针对一个接口做压力测试就是单场景,针对一个流程做 ...

  3. 详细介绍windows下使用python pylot进行网站压力测试

    windows下使用python进行网站压力测试,有两个必不可少的程序需要安装,一个是python,另一个是pylot.python是一个安装软件,用来运行python程序,而pylot则是pytho ...

  4. Python Web 性能和压力测试 multi-mechanize

    http://www.aikaiyuan.com/5318.html 对Web服务做Performance & Load测试,最常见的工具有Apache Benchmark俗称ab和商用工具L ...

  5. 【转】 详细介绍windows下使用python pylot进行网站压力测试

    windows下使用python进行网站压力测试,有两个必不可少的程序需要安装,一个是python,另一个是pylot.python是一个安装软件,用来运行python程序,而pylot则是pytho ...

  6. python开发笔记-通过xml快捷获取数据

    今天在做下python开发笔记之如何通过xml快捷获取数据,下面以调取nltk语料库为例: import nltk nltk.download() showing info https://raw.g ...

  7. 一次接口压力测试qps极低原因分析及解决过程

    一次接口压力测试qps极低原因分析及解决过程 9-2日在做内部的性能测试相关培训时,发现注册接口压力测试qps极低(20左右),这个性能指标远不能达到上线标准 ,经过一系列调试,最后定位 98%的时间 ...

  8. 测试开发系列之Python开发mock接口(一)

    什么是mock接口呢,举个栗子,你在一家电商公司,有查看商品.购物.支付.发 货.收获等等等一大堆功能,你是一个测试人员,测测测,测到支付功能的时候,你就要调用第三方支付接口了,真实支付,直接扣你支付 ...

  9. python开发笔记-python调用webservice接口

    环境描述: 操作系统版本: root@9deba54adab7:/# uname -a Linux 9deba54adab7 --generic #-Ubuntu SMP Thu Dec :: UTC ...

随机推荐

  1. mercurial的几个易用性小技巧

    其实这两年,能够采用mercurial的项目我都尽量用,甚至有些上游是git的,或者需要托管到公司内gitlab上与别人协作的,我都装上hg-git.无它,只是因为mercurial易用性比git好得 ...

  2. 跟Python打包相关的一些文章

    6 things I learned about setuptools Python 101: easy_install or how to create eggs « The Mouse Vs. T ...

  3. 【iCore4 双核心板_FPGA】例程十四:基于I2C的ARM与FPGA通信实验

    实验现象: 1.先烧写ARM程序,然后烧写FPGA程序. 2.打开串口精灵,通过串口精灵给ARM发送数据从而给FPGA发送数据 ,会接收到字符GINGKO. 3.通过串口精灵发送命令可以控制ARM·L ...

  4. centos 扩容

    1. 查看挂载点信息: [root@localhost]# df -h 文件系统 容量 已用 可用 已用% 挂载点 /dev/mapper/centos-root 18G 15G 2.9G 84% / ...

  5. Java两种核心机制

    1.Java虚拟机 2.垃圾回收

  6. 解决:ngxin做http强制跳转https,接口的POST请求变成GET

    域名配置了http强制跳转htpps后发现app发起post请求会出现405错误. 所以怀疑是http强制跳转https出现了问题.修改nginx配置如下即可解决: server { listen ; ...

  7. Mysql系列三:Centos6下安装Mysql和Mysql主从复制的搭建

    一.Centos6下安装Mysql 检测下系统有没有自带的mysql:yum list installed | grep mysql, 如果已经有的话执行命令yum -y remove mysql-l ...

  8. Nginx系列二:(Nginx Rewrite 规则、Nginx 防盗链、Nginx 动静分离、Nginx+keepalived 实现高可用)

    一.Nginx Rewrite 规则 1. Nginx rewrite规则 Rewrite规则含义就是某个URL重写成特定的URL(类似于Redirect),从某种意义上说为了美观或者对搜索引擎友好, ...

  9. OpenResty 最佳实践 1

    建议先搜索<OpenResty最佳实践.pdf> 到网上下载openresty-1.13.6.1-win32 考虑到操作方便性,建议建立个bin目录,放入系统目录中,生成 nginx-st ...

  10. [Localization] SSD - Single Shot MultiBoxDetector

    Prerequisite: VGG Ref: [Object Tracking] Localization and Detection SSD Paper: http://lib.csdn.net/a ...