【问题标题】:Translating performance run from autocannon to locust将性能从自动加农炮转换为蝗虫
【发布时间】:2021-08-04 10:47:54
【问题描述】:

我需要将 autocannon 性能测试转换为 locust python 代码并达到每秒相同的请求标准 > 3000

这是自动炮命令:

AUTOCANNON="taskset -c 8-15 /opt/autocannon-tests/node_modules/.bin/autocannon --amount 100000 --connections 30 --bailout 5 --json"

$AUTOCANNON $URL/applications -m PUT -H "Content-Type:application/json" -H "Authorization=$AUTHORIZATION_HEADER" -b '{"name":"test"}'

我设法达到每秒 > 3000 个请求数

我写了一个python代码

class _PerformanceTask(SequentialTaskSet):
    def __init__(self, *args, **kwargs):
        SequentialTaskSet.__init__(self, *args, **kwargs)
        self.username = 'admin'
        self.password = 'admin'
        self.token = None
        self.identifier = time.time()
        self.error = None
        self.as3_user_id = None
        self.non_admin_user_token = None
        self.as3_user_token = None
        self.system_id = None
        self.open_api_retrieve_count = 0
        self.declare_id = None
        self.network_id = None
        self.irule_app = None
        self.irule_network_id = None
        self.application_editor_user = None

    def on_start(self):
        self.login()

    def _log(self, fmt, *args):
        print('[%s] %s' % (self.identifier, fmt % args))

    def _request(self, method, path, non_admin_user_token=False, headers=None, **kwargs):
        self._log('[%s]%s', method, path)
        self._log('%s', repr(kwargs))
        if not headers:
            headers = {'Content-Type': 'application/json'}
        if self.token:
            headers['Authorization'] = 'Bearer %s' % self.token
        if non_admin_user_token:
            headers['Authorization'] = 'Bearer %s' % self.non_admin_user_token
        resp = self.client.request(method, path, headers=headers, **kwargs)
        self._log('resp status code: %s', resp.status_code)
        self._log('resp content: %s', resp.text)
        assert resp.status_code in (200, 201, 204, 202)
        if (re.search('^[\[\{]', resp.text)):
            return resp.json()
        return resp.text

    def login(self):
        self._log('login')
        resp = self._request(
            method='GET',
            path='/login',
            auth=(self.username, self.password),
        )
        self.token = resp['token']
        self._log('token is: %s', self.token)

 @task
    def run_performance(self):
        self._log('PUT request to $URL/applications with auth. header.')
        resp = self._request(
            method='PUT',
            path='/applications',
            json={
                "name":"test",
            }
        )

        self._log('response is: %s', resp)



class PerformanceTask(FastHttpUser):
    tasks = [_PerformanceTask]

注意:我正在使用 FastHttpUser + locust-plugins 安装 但我无法达到相同的结果。 我运行这个 performance.py 脚本的方式

locust --locustfile performance.py --host https://localhost:5443/api/v1 --headless -u 30 -i 100000

并且还分发:

locust --locustfile performance.py --host https://localhost:5443/api/v1 --headless -u 30 -i 10000 --master --expect-workers=8

and start workers like 
locust --locustfile performance.py --worker --master-host=127.0.0.1 -i 10000 &

无论如何 - 我得到了结果表,无论我如何运行,速度都会低得多:

请求/秒失败/秒


224.49 0.00

希望你有想法

【问题讨论】:

    标签: performance locust


    【解决方案1】:

    我不熟悉 autocannon,所以我不能完全确定,但是快速浏览一下文档会发现 --connections 似乎并不能转化为 Locust 的 --users/-u。它说它是“要使用的并发连接数”。为了得到类似的东西,我相信你必须设置一个 FastHttpSession 并在那里指定concurrency。比如:

    fast_http_session = FastHttpSession(environment=env, base_url="https://localhost:5443/api/v1", user=None, concurrency=30)
    

    您需要在 Locust 运行时从 Locust 获取环境以将其传递到那里,并且可能希望或可能不希望指定您的实际用户(如果您将其放入您的用户类中,您可以将其传递为 self )。

    但这应该可以让您获得要使用的并发连接数,然后您会想要增加生成的用户数。当您使用您创建的会话进行呼叫时,用户将重用 30 个打开的连接,这取决于您需要生成多少用户来“饱和”连接,如 autocannon 声称的那样和/或您运行它的机器可以处理多少台。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-13
      • 2019-01-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多