【问题标题】:Example for using Locust with Boto3将 Locust 与 Boto3 一起使用的示例
【发布时间】:2022-01-22 19:52:18
【问题描述】:

我正在尝试将 Locust 与 Boto3 一起用于 S3 的负载测试,尤其是 PutObject。

我找到了一个教程和另一个 github 项目,但两者都是为较旧的 locustio 包编写的,并且似乎与当前版本的 locust 2.5.1 不兼容。

https://medium.com/@allankp/populating-dashboards-with-boto3-and-locust-ff38b113349a https://github.com/twosigma/locust-s3

我曾短暂尝试过将此代码调整到较新的版本,但这似乎需要对我还没有掌握的 locust 有一个很好的了解。

谁有一个简单的初学者的例子可以分享?还是您更愿意推荐使用旧的 locustio 包或其他工具?

非常感谢。

【问题讨论】:

    标签: amazon-s3 boto3 locust


    【解决方案1】:

    使代码适应 Locust 1+ 版本所需的更改非常小。

    引用文档变更日志:

    Locust class renamed to User
    
    We’ve renamed the Locust and HttpLocust classes to User and HttpUser. The locust attribute on TaskSet instances has been renamed to user.
    

    【讨论】:

      【解决方案2】:

      在官方文档中的this example 和 Cyber​​wiz 的回答的帮助下,我为我的具体问题创建了这个简单的解决方案。

      import time
      import boto3
      from locust import User, task, constant, events
      
      
      class BotoClient:
          def __init__(self):
              self.s3_client = boto3.client(
                  's3',
                  aws_access_key_id="ACCESS_KEY",
                  aws_secret_access_key="SECRET_KEY"
              )
      
          def send(self, f, bucket_name, object_name):
      
              request_meta = {
                  "request_type": "upload file",
                  "name": "S3",
                  "start_time": time.time(),
                  "response_length": 0,
                  "response": None,
                  "context": {},
                  "exception": None,
              }
              start_perf_counter = time.perf_counter()
      
              try:
                  self.s3_client.upload_fileobj(f, bucket_name, object_name)
              except Exception as e:
                  request_meta['exception'] = e
      
              request_meta["response_time"] = (time.perf_counter() - start_perf_counter) * 1000
      
              events.request.fire(**request_meta)
      
      
      class BotoUser(User):
          abstract = True
      
          def __init__(self, env):
              super().__init__(env)
              self.client = BotoClient()
      
          wait_time = constant(1)
      
      
      class MyUser(BotoUser):
      
          wait_time = constant(1)
      
          @task
          def send_request(self):
              with open("FILE_NAME", 'rb') as f:
                  self.client.send(f, "BUCKET_NAME", "OBJECT_NAME")
      
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-09-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多