将性能从自动加农炮转换为蝗虫



我需要将autocannon性能测试翻译成蝗虫python代码并达到相同的每秒请求标准>3000

这是autocannon命令:
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]

注意:我正在使用fastttpuser +蝗虫插件安装但我不能达到同样的结果。我运行这个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 &

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

申请失败/s/s

224.49 - 0.00

我希望你有想法

我不熟悉自动炮,所以我不完全确定,但快速浏览文档说,--connections似乎不像它翻译为蝗虫的--users/-u。它说的是"要使用的并发连接数"。要获得类似的东西,我认为您必须设置fastttpsession并在那里指定concurrency。比如:

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

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

但是这应该会让你得到并发连接的数量,然后你会想要增加你产生的用户的数量。当您使用您创建的会话拨打电话时,用户将重用30个打开的连接,这将取决于您发现需要生成多少用户才能"饱和"。像autocannon这样的连接要求和/或你运行它的机器可以处理多少。

最新更新