Python 蝗虫与蟒蛇:没有蝗虫统计显示在用户界面上

Python 蝗虫与蟒蛇:没有蝗虫统计显示在用户界面上,python,locust,Python,Locust,我对蝗虫(和Python)是新手,正在尝试加载测试API。我的蝗虫用户界面没有显示任何统计数据-没有用户孵化,没有API调用,等等。我需要帮助弄清楚如何让他们显示在用户界面上,这样我才能获得统计数据 这是我的蝗虫.py文件: from locust import HttpUser, SequentialTaskSet, task, constant import finops_service.locust_files.finops_fx_load_testing as load_test c

我对蝗虫(和Python)是新手,正在尝试加载测试API。我的蝗虫用户界面没有显示任何统计数据-没有用户孵化,没有API调用,等等。我需要帮助弄清楚如何让他们显示在用户界面上,这样我才能获得统计数据

这是我的蝗虫.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]
以下是我的职责:

    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
有人能帮我弄清楚如何显示我的统计数据吗?

谢谢

在《蝗虫》中没有出现统计数据是因为你发布了一个例外。如果您解决了这个问题,并且代码运行没有问题,那么您应该可以在蝗虫UI中看到统计信息

您的代码存在许多问题

我不确定这是否只是将代码粘贴到SO中的问题,但您的
on_start()
get_company_data()
函数(以及
@task
装饰器)需要缩进,以便它们是
FXTransaction
的一部分

另外,尝试在
login()
get\u company\u data()
中使用
self.client
,也行不通,因为它们不是类的一部分,所以不存在
self
这样的东西。您可能希望将该函数中的
self
更改为just
client
,然后在从
on_start()调用它时传入
self.client

但你的主要问题可能是:

with self.client.post(
            "security/login",
            headers={'Content-Type': 'application/json',
                     'Authorization': 'BASIC 0ee89b88-5c4b-4922-b1f9-c1584ab26e7e:12345'},
            name=login,
            …
name
这是您要点击的端点(URL)的名称。这应该是一个字符串,但您正在为其提供函数
login
。因此,当蝗虫代码试图用它想要做的
name
值做一些事情时,希望它是一个字符串,它会失败,因为它试图在函数上做这些事情。这就是AttributeError:“function”对象没有属性“replace”的意思


我认为,如果你能解决所有这些问题,蝗虫应该会起作用。

谢谢你,索洛沃克!name=login vs name='login“的错误是我的统计数据没有显示的原因!我真是太感谢你了!!!缩进问题是粘贴到这里的结果。我也一定会解决你指出的其他问题。谢谢你如此周到和彻底的回答!
with self.client.post(
            "security/login",
            headers={'Content-Type': 'application/json',
                     'Authorization': 'BASIC 0ee89b88-5c4b-4922-b1f9-c1584ab26e7e:12345'},
            name=login,
            …