网上开源的压力测试工具超级的多,但是总有一些功能不是很符合自己预期的,于是自己动手搭建了一个简单的http load的压测环境

1.首先从最简单的http环境着手,当你在浏览器上输入了http://www.google.com并且按下回车的时候其实已经发生了很多事情。

  1.你的PC会去查找本地DNS缓存,查看是否存在www.google.com的IP,一般来说第一次访问是没有的。

  2.于是,你的PC在本地DNS没有查找的IP的情况下,只能去求助于DNS server了(DNS server可以通过命令"ipconfig /all"查看),这个时候通过一系列的DNS query后,PC已经获取到了谷歌的IP了

  3.这时浏览器便会发出第一个报文,TCP SYN与谷歌的服务器建立TCP连接,当你的这个TCP SYN报文的时候,咱们伟大的天朝防火墙把我们的TCP SYN报拦截了!!!,于是浏览器在尝试多次建连为未成功的情况下只能返回给你一个"无法连接到此网站"。

  4.这是一个失败的案例,于是你重新在浏览器中输入了咱们伟大的国产搜索网站http://www.baidu.com

  5.所有的流程和前面是一样的,浏览器发送了TCP SYN,baidu server返回SYN+ACK,浏览器再次回复确认报文ACK,这时你与百度已经建立了一条可靠的连接,双方可以在这条连接里相互发送报文了

2.简单的一个用户访问百度的流程就结束了,但是真实场景是这样的吗?肯定不是啊,天朝上亿网民都需要访问百度来搜索一些在夜深人静的时候偷偷看的爱情动作片,这个时候,baidu server集群的部分成员会感觉身体被掏空了一般,而有一些则感觉还没有被满足,于是在有一款强悍的盾牌叫做web负载均衡,web负载均衡会在baidu server的前面把所有的http request都承包下来,然后挨个发给baidu server的服务器集群,此时的baidu server集群的成员都能得到相应的http request

3.尽管如此,baidu server在正常情况下还是能够很好的工作,可是一旦遇到很大的突发请求流量时就会感觉很难处理过来,比如说某宝的双十一活动,某东的双十二活动,这些都是突发业务流量,此时server们就会感觉接客有点接不过来,所以我们这些作为质量保证的QA测试攻城狮面前,这些都是我们需要解决的,由此就有了咱们的HTTP load测试环境

4.从概念上来说http load就是对web软件进行高负载,高压力,高流量的测试,并且这种测试是很有必要的

5.如何搭建自己所需要的http load压测环境呢

  1.从需求考虑,http request包括哪些内容呢? url, 各种header, cookie, 还有什么auth 等等字段和头部,这些python requests模块都能帮你解决

    def request(self, method, url,
params=None, data=None, headers=None, cookies=None, files=None,
auth=None, timeout=None, allow_redirects=True, proxies=None,
hooks=None, stream=None, verify=None, cert=None, json=None):
"""Constructs a :class:`Request <Request>`, prepares it and sends it.
Returns :class:`Response <Response>` object. :param method: method for the new :class:`Request` object.
:param url: URL for the new :class:`Request` object.
:param params: (optional) Dictionary or bytes to be sent in the query
string for the :class:`Request`.
:param data: (optional) Dictionary, bytes, or file-like object to send
in the body of the :class:`Request`.
:param json: (optional) json to send in the body of the
:class:`Request`.
:param headers: (optional) Dictionary of HTTP Headers to send with the
:class:`Request`.
:param cookies: (optional) Dict or CookieJar object to send with the
:class:`Request`.
:param files: (optional) Dictionary of ``'filename': file-like-objects``
for multipart encoding upload.
:param auth: (optional) Auth tuple or callable to enable
Basic/Digest/Custom HTTP Auth.
:param timeout: (optional) How long to wait for the server to send
data before giving up, as a float, or a :ref:`(connect timeout,
read timeout) <timeouts>` tuple.
:type timeout: float or tuple
:param allow_redirects: (optional) Set to True by default.
:type allow_redirects: bool
:param proxies: (optional) Dictionary mapping protocol or protocol and
hostname to the URL of the proxy.
:param stream: (optional) whether to immediately download the response
content. Defaults to ``False``.
:param verify: (optional) Either a boolean, in which case it controls whether we verify
the server's TLS certificate, or a string, in which case it must be a path
to a CA bundle to use. Defaults to ``True``.
:param cert: (optional) if String, path to ssl client cert file (.pem).
If Tuple, ('cert', 'key') pair.
:rtype: requests.Response
"""
# Create the Request.

  2.参数理解:

    1.params={'key1':'value1'},代表的是在url中加入query,也就是查询字符串(具体可以查看HTTP权威指南)

r = requests.get("http://172.16.83.69:80", auth=("taomin","123"), params={'query':'12345'})
print(r.url)
http://172.16.83.69:80/?query=12345

    举个例子你可以在浏览器中输入"http://www.sogou.com/web?query=12345",此时你会发现神奇现象,可以尝试一下,很快就能理解什么是query

    2.header={'host':'google'}

    这个字段没什么可以解释的,就是在http请求中添加头部,很好理解

    

    3.cookies={'mycookie':123},cookies是诞生是因为http是无状态的,而cookie则像一张会员卡一样,告诉服务器,我之前来过或者我之前登录过

r = requests.get("http://172.16.83.69:80", auth=("taomin","123"), params={'query':'12345'},cookies={'mycookie':'123'})

    

    4.auth=('user',passwd),这个很有用处,有些网站需要验证你的身份才能登录,此时如果你能够在添加auth头部的话,可以将用户名和密码添加在auth字段中

r = requests.get("http://172.16.83.69:80", auth=("taomin","123"), params={'query':'12345'},cookies={'mycookie':'123'})

    5.post通过data参数传递参数

headers = {"Content-Type":"applicationjson"}
data={
num:1,
two:2,
}
requests.post(headers=headers,url="http://www.baidu.com",data=data)

    字段解释大概先这么多了,最后附上自己写的一个简单的python压力测试脚本,I5处理器作为测试的客户端可以打到CPS为1000多吧

# -*- coding: utf-8 -*-
#!/usr/bin/env python
import requests
import threading
import time header = {'Agent-User':'Chrome'}
start = time.time() def f():
while(time.time()-start<100000):
r = requests.get("http://172.16.83.69:80", auth=("taomin","123"), params={'query':'12345'},cookies={'mycookie':'123'})
print(r.status_code) threads = [] for i in range(1000):
threads.append(threading.Thread(target=f,args=())) for i in threads:
i.start()
i.join()

使用了多线程增强用户数和并发数,同时打印htt返回状态,确认http访问是否成功

使用python requests模块搭建http load压测环境的更多相关文章

  1. 通过k8s(Kubernetes)搭建jmeter的压测环境master-slave架构,实现弹性伸缩

    在k8s上部署jmeter的mater和slave,根据压测需求动态增减master和slave节点数量,即可以完成压测任务,又可以是资源利用最大化 https://blog.kubernauts.i ...

  2. 在Linux CentOS上搭建Jmeter压测环境

    本文的主要内容是介绍如何在Linux CentOS 服务器上面搭建Jmeter的压测环境整个详细的流程,来满足我们日常工作中对于压力测试环境搭建.压力测试执行过程的需求. 一.首先我们要准备四个东西, ...

  3. 阿里巴巴高可用技术专家襄玲:压测环境的设计和搭建 PTS - 襄玲 云栖社区 今天

    阿里巴巴高可用技术专家襄玲:压测环境的设计和搭建 PTS - 襄玲 云栖社区 今天

  4. Windows下安装Python requests模块

    在使用自己写的或者别人的python小工具时可能会出现类似ImportError: No module named Requests的问题: D:\tool\python\fuzz>Fuzz.p ...

  5. Python requests模块学习笔记

    目录 Requests模块说明 Requests模块安装 Requests模块简单入门 Requests示例 参考文档   1.Requests模块说明 Requests 是使用 Apache2 Li ...

  6. Python—requests模块详解

    1.模块说明 requests是使用Apache2 licensed 许可证的HTTP库. 用python编写. 比urllib2模块更简洁. Request支持HTTP连接保持和连接池,支持使用co ...

  7. 使用monkey技术修改python requests模块

    例如请求前和请求后各来一条日志,这样就不需要在自己的每个代码都去加日志了. 其实也可以直接记录'urllib3.connectionpool'  logger name的日志. 修改了requests ...

  8. Python requests模块params、data、json的区别

    json和dict对比 json的key只能是字符串,python的dict可以是任何可hash对象(hashtable type): json的key可以是有序.重复的:dict的key不可以重复. ...

  9. Python requests模块

    import requests 下面就可以使用神奇的requests模块了! 1.向网页发送数据 >>> payload = {'key1': 'value1', 'key2': [ ...

随机推荐

  1. bzoj1238

    题解: 傻逼模拟题 果断的复制了题解(还没有c++题解?) 代码: program p2509; type arr=array[..] of boolean; var tot:longint; s:a ...

  2. 玩转X-CTR100 l STM32F4 l AT24C02 EEPROM存储

    我造轮子,你造车,创客一起造起来!塔克创新资讯[塔克社区 www.xtark.cn ][塔克博客 www.cnblogs.com/xtark/ ]      本文介绍X-CTR100控制器 板载EEP ...

  3. SharePoint Framework 把你的客户端web部件连接到SharePoint

    博客地址:http://blog.csdn.net/FoxDave 把你的web部件连接到SharePoint来访问SharePoint中的功能和数据,为终端用户提供更完整的体验.本篇会基于之前构 ...

  4. JavaWeb:c3p0配置问题java.lang.NoClassDefFoundError: com/mchange/v2/ser/Indirector

    错误显示 java.lang.NoClassDefFoundError: com/mchange/v2/ser/Indirector at dbdemo.JdbcUtils.<clinit> ...

  5. ChinaCock界面控件介绍-CCButton

    即将发布的ChinaCock新版本,将带来一个CCButton控件,实现可视按钮.Delphi原生的Button,在上面滑动后,当释放手指时会误触发OnClick事件,这不是我们想要的结果,CCBut ...

  6. Day3作业及默写

    1.有变量量name = "aleX leNb" 完成如下操作: 移除 name 变量对应的值两边的空格,并输出处理结果 print(name.strip()) 移除 name 变 ...

  7. 你知道怎么用Idea抽取方法、创建class吗?

    liJ IDEA的快捷键是进行重构的利器,坊间盛传,完全使用IDEA快捷键重构的代码,是不需要写测试用例保护的 本文就分享一个使用IDEA抽取方法及创建新的class的方法 工具/原料   Intel ...

  8. CPU对指令长度的判断

    译码一般包括:指令预取.指令预分析.解码.预取就是从cache或者内存取一系列的字节(大小可以保证至少包含一条指令),并设置一个待分析的位置,预分析从此位置逐字节分析,如果是前缀就设置分析状态(因为前 ...

  9. HDU 2585 Hotel(字符串的模糊匹配+递归)

    Problem Description Last year summer Max traveled to California for his vacation. He had a great tim ...

  10. linux 实用命令 —— tr

    1. [:alnum:] tr -cd '[:alnum:]' -d:delete:-c:complement:-cd:删除后边的参数以外的: [:class:] [:alpah:]:字母,[:dig ...