[英]Locust with Python: No Locust Stats are showing on UI
I am new to Locust (and Python) and am trying to load test an API.我是 Locust(和 Python)的新手,正在尝试对 API 进行负载测试。 My Locust UI shows no stats whatsoever - no users hatched, no API calls, etc. I need help figuring out how to get them to show up on the UI so I can get the stats.
我的 Locust UI 不显示任何统计信息——没有用户孵化,没有 API 调用等。我需要帮助弄清楚如何让它们显示在 UI 上,以便我可以获得统计信息。
Here is my locust.py file:这是我的 locust.py 文件:
from locust import HttpUser, SequentialTaskSet, task, constant
import finops_service.locust_files.finops_fx_load_testing as load_test
class FXTransaction(SequentialTaskSet):
def __init__(self, parent):
super().__init__(parent)
self.comp_data = dict()
self.rate_lock_response = dict()
self.transaction_response = dict()
self.fx_providerID = '57638f08-e938-48d7-accf-325b6728a9ee'
self.headers = {"Content-Type": "application/json"}
def on_start(self):
load_test.login(self),
@task
def get_company_data(self):
# Get company data
self.comp_data = load_test.get_company_data(self)
print("Company is: ", self.comp_data['company'])
print("Vendor is: ", self.comp_data['vendor'])
print("Payment Method is: ", self.comp_data['payment_method'])
print("Funding Method is: ", self.comp_data['funding_method'])
# @task
# def rate_lock(self):
print("Starting rate lock")
load_test.rate_lock(self)
print("This is the returned rate lock response:")
print(self.rate_lock_response)
# @task
# def approve_transaction(self):
# print("Starting transaction")
# load_test.approve_transaction(self)
# print("This is the returned transaction response:")
# print(self.transaction_response)
class MyUser(HttpUser):
wait_time = constant(1)
# weight = 1
host = "http://localhost:8080/PaymentService/base/"
tasks = [FXTransaction]
And here are my functions:这是我的功能:
import json
import random
import uuid
from utils import json_util as json_util
enter code here
def login(self):
try:
with self.client.post(
"security/login",
headers={'Content-Type': 'application/json',
'Authorization': 'BASIC 0ee89b88-5c4b-4922-b1f9-c1584ab26e7e:12345'},
name=login,
timeout=55.6,
catch_response=True) as response:
if response.status_code != 200:
response.failure("Login failed")
else:
response.success()
print("Login succeeded")
self.headers.update({'if-Match': "{}".format(response.headers["ETag"])})
except ConnectionRefusedError:
print("The test could not connect to {}".format("http://localhost:8080/PaymentService/security/login"))
raise
def get_company_data(self):
comp_index = random.randrange(0, 9)
print("Random company data index is: ", comp_index)
try:
body = json.load(open("finops_service/locust_files/load_testing_data.json", 'r'))
comp_data = body['fx']['multipleCompanies_10']['company_data'][comp_index]
except ConnectionRefusedError:
print("The test could not connect to {}".format("http://localhost:8080/PaymentService/security/login"))
raise
return comp_data
def rate_lock(self):
body = json_util.get_json_object(
"/finops_service/locust_files/rate_lock_load_testing.json",
'fx'.lower(),
'valid_multiple_20'.lower()
)
body["debtorCompanyProfileId"] = self.comp_data['company']
i = 0
# there are 20 rate locks so need to update each request
while i < 20:
body["rateRequestPayments"][i]["creditorProfileId"] = self.comp_data['vendor']
body["rateRequestPayments"][i]["debtorProfileId"] = self.comp_data['company']
body["rateRequestPayments"][i]["paymentMethodId"] = self.comp_data['payment_method']
random_float_no = round(random.uniform(1.11, 999.99), 2)
body['rateRequestPayments'][i]['amount'] = random_float_no
i += 1
try:
print("RIGHT BEFORE RATE LOCK CALL")
with self.client.post("services/transaction/fx/rate/lock",
data=json.dumps(body),
headers=self.headers,
name="rate lock",
timeout=55.6,
catch_response=True) as response:
if "GENERIC_FAILURE" in response.text:
response.failure("FAIL")
print("Rate lock call failed")
else:
response.success()
print("rate lock succeeded")
print("Rate lock response is:.........", response)
print("Rate lock response TEXT is:.........", response.text)
self.rate_lock_response = response.text
except ConnectionRefusedError:
print("The test could not connect to {}".format(
"http://localhost:8080/PaymentService/services/transaction/fx/rate/lock"))
raise
When I run this, I can see that the self.client.post("services/transaction/fx/rate/lock"... is happening and either succeeding or failing. However, in the Locust UI, I show 0 users hatched (console shows I hatched 5) and 0 tasks shown.当我运行它时,我可以看到 self.client.post("services/transaction/fx/rate/lock"... 正在发生并且成功或失败。但是,在 Locust UI 中,我显示 0 个用户孵化(控制台显示我孵化了 5 个)和 0 个任务。
I get this error each time I run which it looks related to my stats not showing up but I don't know why it is happening:我每次运行时都会收到此错误,它看起来与我的统计数据没有显示有关,但我不知道为什么会这样:
Traceback (most recent call last):
File "/Users/michellegautier/.pyenv/versions/3.7.4/lib/python3.7/site-packages/gevent/pywsgi.py", line 999, in handle_one_response
self.run_application()
File "/Users/michellegautier/.pyenv/versions/3.7.4/lib/python3.7/site-packages/gevent/pywsgi.py", line 945, in run_application
self.result = self.application(self.environ, self.start_response)
File "/Users/michellegautier/.pyenv/versions/3.7.4/lib/python3.7/site-packages/flask/app.py", line 2464, in __call__
return self.wsgi_app(environ, start_response)
File "/Users/michellegautier/.pyenv/versions/3.7.4/lib/python3.7/site-packages/flask/app.py", line 2450, in wsgi_app
response = self.handle_exception(e)
File "/Users/michellegautier/.pyenv/versions/3.7.4/lib/python3.7/site-packages/flask/app.py", line 1867, in handle_exception
reraise(exc_type, exc_value, tb)
File "/Users/michellegautier/.pyenv/versions/3.7.4/lib/python3.7/site-packages/flask/_compat.py", line 39, in reraise
raise value
File "/Users/michellegautier/.pyenv/versions/3.7.4/lib/python3.7/site-packages/flask/app.py", line 2447, in wsgi_app
response = self.full_dispatch_request()
File "/Users/michellegautier/.pyenv/versions/3.7.4/lib/python3.7/site-packages/flask/app.py", line 1952, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/Users/michellegautier/.pyenv/versions/3.7.4/lib/python3.7/site-packages/flask/app.py", line 1821, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/Users/michellegautier/.pyenv/versions/3.7.4/lib/python3.7/site-packages/flask/_compat.py", line 39, in reraise
raise value
File "/Users/michellegautier/.pyenv/versions/3.7.4/lib/python3.7/site-packages/flask/app.py", line 1950, in full_dispatch_request
rv = self.dispatch_request()
File "/Users/michellegautier/.pyenv/versions/3.7.4/lib/python3.7/site-packages/flask/app.py", line 1936, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/Users/michellegautier/.pyenv/versions/3.7.4/lib/python3.7/site-packages/locust/web.py", line 319, in wrapper
return view_func(*args, **kwargs)
File "/Users/michellegautier/.pyenv/versions/3.7.4/lib/python3.7/site-packages/locust/util/cache.py", line 21, in wrapper
cache["result"] = func(*args, **kwargs)
File "/Users/michellegautier/.pyenv/versions/3.7.4/lib/python3.7/site-packages/locust/web.py", line 208, in request_stats
"safe_name": escape(s.name, quote=False),
File "/Users/michellegautier/.pyenv/versions/3.7.4/lib/python3.7/html/__init__.py", line 19, in escape
s = s.replace("&", "&") # Must be done first!
AttributeError: 'function' object has no attribute 'replace'
2021-03-18T16:44:29Z {'REMOTE_ADDR': '::1', 'REMOTE_PORT': '49242', 'HTTP_HOST': 'localhost:8089', (hidden keys: 25)} failed with AttributeError
Can someone please help me figure out how to make my stats show up?有人可以帮我弄清楚如何显示我的统计信息吗?
Thank you!!!!谢谢!!!!
The stats not showing up in Locust is because of that Exception you posted. Locust 中未显示的统计数据是因为您发布的异常。 If you resolve that and get to where your code runs without issues, you should see stats in the Locust UI.
如果您解决了该问题并在没有问题的情况下运行代码,您应该会在 Locust UI 中看到统计信息。
There are a number of issues with your code.您的代码存在许多问题。
I'm not sure if it's just an issue with pasting your code into SO, but your on_start()
and get_company_data()
functions (and the @task
decorator) need to be indented so they're part of FXTransaction
.我不确定是否只是将代码粘贴到 SO 中的问题,但是您的
on_start()
和get_company_data()
函数(以及@task
装饰器)需要缩进,因此它们是FXTransaction
的一部分。
Also, trying to use self.client
in login()
or get_company_data()
isn't going to work because they aren't part of a class so there's no such thing as self
.此外,尝试在
login()
或get_company_data()
中使用self.client
是行不通的,因为它们不是 class 的一部分,所以没有self
这样的东西。 You'd probably want to change self
in that function to just client
and then pass in self.client
when you call it from the on_start()
function.您可能希望将 function 中的
self
更改为client
,然后在从on_start()
function 调用它时传入self.client
。
But what's possibly your main issue is here:但是你的主要问题可能在这里:
with self.client.post(
"security/login",
headers={'Content-Type': 'application/json',
'Authorization': 'BASIC 0ee89b88-5c4b-4922-b1f9-c1584ab26e7e:12345'},
name=login,
…
name
here is the name of the endpoint (URL) you're hitting.这里的
name
是您要访问的端点 (URL) 的名称。 This is supposed to be a string but you're giving it your function login
.这应该是一个字符串,但你给它你的 function
login
。 So when the Locust code tries to do some thing with your name
value that it wants to do, expecting it to be a string, it's going to fail because it's trying to do those on a function instead.因此,当 Locust 代码尝试使用您的
name
值做一些它想要做的事情时,期望它是一个字符串,它会失败,因为它试图在 function 上做这些事情。 That's what AttributeError: 'function' object has no attribute 'replace'
means.这就是
AttributeError: 'function' object has no attribute 'replace'
意思。
I think if you fix all of those things, Locust should work.我认为如果你解决所有这些问题,蝗虫应该可以工作。
i am running non-http test using locust.我正在使用蝗虫运行非 http 测试。 when i run the test, no results stats are visible.
当我运行测试时,看不到任何结果统计信息。 below is my peice of code.
下面是我的一段代码。
class AddFiles(User): #class AddFiles(TaskSet): t1=Test() p4clients= ["p4cli-client1","p4cli-client2","p4cli-client3","p4cli-client4","p4cli-client5"] p4users= ["user1","user2","user3","user4","user5","user6","user7","user8","user9","user10","user11","user12","user13","user14","user15"] uarr=0 carr=0 def on_start(self): os.environ["P4PASSWD"]="User123#" self.carr=random.randint(0, 4) self.uarr=random.randint(0, 10) self.t1.connect(self.p4users[self.uarr])类 AddFiles(用户):#class AddFiles(任务集):t1=测试() ] p4users= [“用户 1”,“用户 2”,“用户 3”,“用户 4”,“用户 5”,“用户 6”,“用户 7”,“用户 8”,“用户 9”,“用户 10”,“用户 11”,“用户 12” ","user13","user14","user15"] uarr=0 carr=0 def on_start(self): os.environ["P4PASSWD"]="User123#" self.carr=random.randint(0, 4 ) self.uarr=random.randint(0, 10) self.t1.connect(self.p4users[self.uarr])
@task(3)
def add_files(self):
self.t1.addsubmit(self.p4users[self.uarr],self.p4clients[self.carr])
#print(self.p4users[self.uarr]," - " ,self.p4clients[self.carr])
time.sleep(2)
@task
def edit_files(self):
self.t1.editsubmit(self.p4users[self.uarr],self.p4clients[self.carr])
time.sleep(2)
@task
def delete_files(self):
self.t1.deletesubmit(self.p4users[self.uarr],self.p4clients[self.carr])
time.sleep(2)
@task
def revert(self):
self.t1.revert(self.p4users[self.uarr],self.p4clients[self.carr])
time.sleep(2)
def on_stop(self):
self.t1.disconnect(self.p4users[self.uarr],self.p4clients)
and when i run, i get below.当我跑步时,我会在下面。
Aggregated 0 0(0.00%) |汇总 0 0(0.00%) | 0 0 0 0 |
0 0 0 0 | 0.00 0.00
0.00 0.00
Aggregated 0 0(0.00%) |汇总 0 0(0.00%) | 0 0 0 0 |
0 0 0 0 | 0.00 0.00
0.00 0.00
Aggregated 0 0(0.00%) |汇总 0 0(0.00%) | 0 0 0 0 |
0 0 0 0 | 0.00 0.00
0.00 0.00
Aggregated 0 0(0.00%) |汇总 0 0(0.00%) | 0 0 0 0 |
0 0 0 0 | 0.00 0.00
0.00 0.00
Aggregated 0 0(0.00%) |汇总 0 0(0.00%) | 0 0 0 0 |
0 0 0 0 | 0.00 0.00
0.00 0.00
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.