首页 > 解决方案 > 任务在 Locust 中使用 FastHttpUser 引发 422 错误

问题描述

我有我的负载测试代码,它正在对站点进行 OAuth,并且对于任务集来说工作得很好。这是在使用 HttpUser 类时。

from locust import HttpUser, TaskSet, task
import urllib.parse
from locust.env import Environment
import json
import urllib3
from locust.contrib.fasthttp import FastHttpUser

global_header = None
class RecommenderTasks(TaskSet):
    def __init__(self, parent):
        super().__init__(parent)
        self.global_header = None
        self.cold_start_url = "https://my-web-site"

    def on_start(self):
        urllib3.disable_warnings()
        self.global_header = self.login()
        
    def login(self):
        call_url = urllib.parse.urljoin(self.cold_start_url, "/oauth/token")
        headers = {'Authorization': 'Basic (my-token)=='}
        response = self.client.post(call_url, {'grant_type': 'client_credentials'}, headers=headers, verify=False)        
        
        access_token = json.loads(response.text.encode('utf8'))['access_token']
        headers = {'Authorization': 'Bearer ' + access_token}
        return headers
    
    @task
    def test_recommender_multiple_platforms(self):
        
        params = {'rec_id': str(0)}
        response = self.client.request("GET", '/recommendations',
                                params=params,
                                headers=self.global_header,
                                verify=False)
        print(response)

class RecommenderUser(HttpUser):
    tasks = [RecommenderTasks]
    min_wait = 0
    max_wait = 0

    host = "https://my-web-site"

但是,当我从 HttpUser 更改为 FastHttpUser 时,如下所示

class RecommenderUser(FastHttpUser):
        tasks = [RecommenderTasks]
        min_wait = 0
        max_wait = 0
    
        host = "https://my-web-site"

我的 GET 请求出现 422 错误。谁能帮帮我?

标签: oauthload-testinglocusthttp-status-code-422fasthttp

解决方案


推荐阅读