在官方文档中的this example 和 Cyberwiz 的回答的帮助下,我为我的具体问题创建了这个简单的解决方案。
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")