跳转到主要内容

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 (8.2 kB 查看哈希)

上传时间

支持者

AWS AWS 云计算和安全赞助商 Datadog Datadog 监控 Fastly Fastly CDN Google Google 下载分析 Microsoft Microsoft PSF 赞助商 Pingdom Pingdom 监控 Sentry Sentry 错误日志 StatusPage StatusPage 状态页面