使用 python 对 API 进行负载测试

dav*_*ler 8 python load-testing locust

我目前正在编写用于负载测试 API 的 python 脚本。我想检查一次 API 可以接受多少请求。API 用于注册,因此我每次都必须发送唯一参数。

无论如何我可以通过蝗虫或任何其他方式实现它吗?

任何帮助,将不胜感激。

这是我的单用户注册代码。

def registration:
    URL = "ip"
    PARAMS = {'name':'test','password':'test1','primary_email':'test667@gmail.com','primary_mobile_number':'9999999999','country_abbrev':'US'} 
    r = requests.post(url = URL,params = PARAMS,auth=HTTPDigestAuth('user', 'pass')) 
    response = r.text 
    print response
Run Code Online (Sandbox Code Playgroud)

GPT*_*T14 8

看看Faker Python 包。无论您是需要引导数据库、创建漂亮的 XML 文档、填写持久性以对其进行压力测试,还是对从生产服务中获取的数据进行匿名化,这都会为您生成虚假数据,Faker 适合您。

from locust import HttpLocust, TaskSet, task
class UserBehavior(TaskSet):
    def on_start(self):
        pass  # add code that you want to run during ramp up

    def on_stop(self):
        pass  # add code that you want to run during ramp down

    def registration(self):
        name = fake.first_name()
        last_name = fake.last_name()
        password = ''
        email = name + last_name + '@gmail.com'
        phone = fake.phone_number()
        URL = "ip"
        PARAMS = {'name':name,'password': password,'primary_email': email,'primary_mobile_number':phone,'country_abbrev':'US'} 
        self.client.post(URL, PARAMS)

class WebsiteUser(HttpLocust):
    task_set = UserBehavior
    min_wait = 5000
    max_wait = 9000
Run Code Online (Sandbox Code Playgroud)

要开始负载测试,请运行 locust -f locust_files/my_locust_file.py --host=http://example.com 有关更多信息,请访问Locust Quickstart