【问题标题】:How to provide test data to a locustfile in Python for load testing [closed]如何在 Python 中向 locustfile 提供测试数据以进行负载测试 [关闭]
【发布时间】:2019-11-14 07:39:44
【问题描述】:

我已经使用flask 构建了一个API,并想使用locust 进行负载测试。 谁能帮助我如何创建它并进行负载测试

【问题讨论】:

    标签: python api testing load locust


    【解决方案1】:

    在得到正确结果后,我想出了在负载测试时如何将输入传递给烧瓶 API 的方式。

    文本分类 API 的负载测试

    from locust import HttpLocust, TaskSet, task
    import os
    import random
    TEST_DATA_PATH = 'load_test_data.txt' 
    
    def load_test_sentences():  
       sent = []  
       with open(TEST_DATA_PATH, 'rb') as fp:
         for row in fp:  
           row = row.strip()   
           sent.append(row)  
       return sent
    
    class UserBehavior(TaskSet):
        def on_start(self):
            self.sent=load_test_sentences()
    
        @task(1)
        def about(self):
            for ut in self.sent:
                self.client.get("/entity-extractor/location?q={}".format(ut.decode("utf-8")),name='Text Classification') 
    
    class MyLocust(HttpLocust):
        task_set = UserBehavior
        host = "http://0.0.0.0:8000"
        min_wait = 5000
        max_wait = 15000
        def __init__(self):
            print('loaded %s sentences' %len(load_test_sentences()))
            super(MyLocust, self).__init__()
    

    【讨论】:

      猜你喜欢
      • 2012-09-18
      • 1970-01-01
      • 2012-01-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-26
      • 1970-01-01
      相关资源
      最近更新 更多