Python HTTP基准工具
项目描述
HTTPBenchmark旨在介于Apache Bench (ab) 和更全面的HTTP测试套件之间。在其最简单形式中,它提供了一些与ab的基本兼容性,支持-n选项来指定请求次数,以及-c选项来指定并发用户数。
然而,真正的预期用途是子类化HTTPBenchmark,并覆盖worker方法以实现您自己的测试逻辑。在这方面,您可以认为HTTPBenchmark是一个框架,它允许您为您的HTTP应用程序构建单元测试,这些测试旨在同时压力测试服务器。请参阅下面的Worker Code部分。
安装
使用pip或easy_install可以轻松完成安装。
pip install httpbenchmark
基本用法
提供了一个名为pb的脚本,旨在作为Apache ab脚本的简单类似物。
pb -n 1000 -c 25 http://my-host.tld/endpoint/
Worker代码
默认情况下,HTTPBenchmark除了发送GET请求并期望返回200 OK状态码外,不执行任何操作。为了充分利用系统,您应该创建自己的脚本,该脚本继承自HTTPBenchmark。
这是一个不完整且未经测试的示例,用于负载测试处理用户和好友的应用程序。它旨在说明使用HTTPBenchmark的基础。
from httpbenchmark import HTTPBenchmark
from tornado import gen
import random
USER_IDS = [1111, 2222, 3333, 4444]
_url = lambda x: ''.join(['http://my-host.tld/', x])
class MyBenchmark(HTTPBenchmark):
@gen.coroutine
def worker(self):
'''
get_worker should return a callable that will be called by the async http client
'''
if random.choice([True, False]):
yield self.new_user()
else:
yield self.returning_user()
@gen.coroutine
def new_user(self):
user_id = random.choice(USER_IDS)
self.log.debug("New user: %s" % user_id)
friends = yield self.open_json(_url("register?uid=%d" % user_id))
# ... handle registration response ...
if failure:
self.log.error("Indicate reason for failure")
self.log.debug("Show debugging info if you want")
self.finish_request(False)
else:
yield self.next_step(user_id, friends['friendList'][0])
@gen.coroutine
def returning_user(self):
user_id = random.choice(USER_IDS)
self.log.debug("Returning user: %s" % user_id)
def handle_login(response, friends):
friends = yield self.open_json(_url("login?uid=%d" % user_id))
# ... handle login response ...
if failure:
self.log.error("Indicate reason for failure")
self.log.debug("Show debugging info if you want")
self.finish_request(False)
else:
yield self.next_step(user_id, friends['friendList'][0])
@gen.coroutine
def next_step(self, user_id, friend_id):
# ... do something else ...
if failure:
self.log.error("Indicate reason for failure")
self.log.debug("Show debugging info if you want")
self.finish_request(False)
else:
# success!
self.finish_request()
if __name__ == '__main__':
MyBenchmark().main()
基础知识
这使用Tornado的异步生成器接口来实现并发性,您的函数需要用@gen.coroutine包装,并且您应该在它们之间使用yield。
worker是您的代码所在之处。它将在基于并发性的空闲槽位存在时被调用。
yield self.get(url, code=200)用于发起GET请求。操作完成后,您将获得响应对象。
self.post(url, params={}, callback)用于POST数据。params应该是一个字典,并将作为POST数据发送。否则,它的工作方式与get相同。
如果您正在向PHP后端发送POST请求并需要使用PHP的嵌套数组语法传递参数,您可以将php_urlencode设置为self.post方法的值为True,它将相应地编码params。
self.get_json(url, callback)是获取并解析返回的JSON数据的快捷方式。您的回调应该接受两个参数,第一个是响应对象,第二个是解析后的JSON。
self.finish_request(True/False)应被调用来表示请求的结束。如果一切如您预期那样工作,请传递True,否则传递False
self.debug_response(response)是在开发测试用例时非常有用的函数。如果您传递它一个响应对象,它将打印出对象的摘要以及头部和正文,以便您调试实时数据。
待办事项
添加一些工作示例
项目详情
httpbenchmark-0.5.tar.gz的哈希
算法 | 哈希摘要 | |
---|---|---|
SHA256 | b0a63aa116e5768c66cb1f83f2a249f7aa04c8fb153a9153f0e89b10307a562e |
|
MD5 | 873d82b30e66dd6d38fb424671fd1c06 |
|
BLAKE2b-256 | 326a73f34c52f4bb765da866e95368766d25060a4394503efed55776bf689986 |