【问题标题】:Load testing on an API using python使用 python 对 API 进行负载测试
【发布时间】:2019-08-27 06:05:37
【问题描述】:

我目前正在编写用于对 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

【问题讨论】:

  • 您可以将其包含在 for 循环中,并将计数器附加到您的姓名后,例如 PARAMS['name']+counter
  • 您可以使用faker 包在负载测试中生成唯一参数。
  • @KostasCharitidis 但这会一个接一个地发送请求,但我想同时执行请求

标签: python load-testing locust


【解决方案1】:

看看Faker Python Package。无论您是需要引导数据库、创建美观的 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

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

【讨论】:

  • 嗨,我知道它会生成假数据,但它不会一个接一个地发送请求吗?就我而言,我想一起发送多个请求。
  • @daveMiller 添加了更多信息以帮助您开始使用 locust 进行负载测试
【解决方案2】:

查看medium link for load test with python with locust - 一个开源负载测试工具。

【讨论】:

    【解决方案3】:
    from locust import HttpLocust, TaskSet
    
    def login(self):
        params= {'name':'test','password':'test1','primary_email':'test667@gmail.com','primary_mobile_number':'9999999999','country_abbrev':'US'}
        self.client.post(URL, data=params)
        #The data parameter or json can both be used here. If it's a dict then data would work but for json replace data with json. For more information you can check out requests package as Locust internally uses requests only.
    
    class UserBehavior(TaskSet):
        tasks = {index: 2, profile: 1}
    
        def on_start(self):
            login(self)
    
        def on_stop(self):
            pass
    
        @task
        def try(self):
           pass
    
    class WebsiteUser(HttpLocust):
        task_set = UserBehavior
        min_wait = 5000
        max_wait = 9000
    

    要开始负载测试,请运行 locust -f locust_files/my_locust_file.py --host=http://example.com 其中 host 是您的 IP。然后您可以转到 127.0.0.1:8089 选择要模拟的虚拟用户数。 在 Windows 上,仅限 1024 个用户。但是您可以使用 Locust 提供的 Master Slave Architecture 的惊人支持。

    PS: on_start 方法中的任何内容只对每个用户运行一次。因此,既然您想测试 API 的限制,您应该更喜欢在 @task 装饰器下添加该请求。

    希望这会有所帮助! :)

    【讨论】:

      【解决方案4】:

      有不同的方法,例如:

      • 使用random strings
      • 使用像faker这样的第三方库
      • 在同一 Python 脚本中列出凭据
      • 在 CSV 文件或数据库等外部数据源中列出凭据

      很遗憾,您的问题没有明确要求“唯一”参数,因此我建议您先熟悉一下How to Run Locust with Different Users 文章

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-05-18
        • 2022-07-29
        • 1970-01-01
        • 1970-01-01
        • 2017-10-15
        • 2017-02-08
        • 2019-03-10
        相关资源
        最近更新 更多