From 2e918c57d516be7a0c5a744a4b83f6cae5f7355c Mon Sep 17 00:00:00 2001 From: Evgenii Stratonikov Date: Wed, 3 May 2023 14:33:16 +0300 Subject: [PATCH] [#57] preset: Use temporary file for payload Signed-off-by: Evgenii Stratonikov --- scenarios/preset/helpers/cmd.py | 7 +++---- scenarios/preset/preset_grpc.py | 7 ++++--- scenarios/preset/preset_s3.py | 7 ++++--- 3 files changed, 11 insertions(+), 10 deletions(-) diff --git a/scenarios/preset/helpers/cmd.py b/scenarios/preset/helpers/cmd.py index 19986eb..54efb43 100644 --- a/scenarios/preset/helpers/cmd.py +++ b/scenarios/preset/helpers/cmd.py @@ -19,10 +19,9 @@ def execute_cmd(cmd_line): return output, success -def random_payload(payload_filepath, size): - with open('%s' % payload_filepath, 'w+b') as fout: - fout.write(os.urandom(1024 * int(size))) - +def random_payload(file, size): + file.write(os.urandom(1024 * int(size))) + file.flush() class ProgressBar: @staticmethod diff --git a/scenarios/preset/preset_grpc.py b/scenarios/preset/preset_grpc.py index 5bf6dae..6b3f834 100755 --- a/scenarios/preset/preset_grpc.py +++ b/scenarios/preset/preset_grpc.py @@ -4,6 +4,7 @@ import argparse import json import random import sys +import tempfile from argparse import Namespace from concurrent.futures import ProcessPoolExecutor @@ -39,7 +40,6 @@ print(args) def main(): container_list = [] objects_list = [] - payload_filepath = '/tmp/data_file' endpoints = args.endpoint.split(',') @@ -71,13 +71,14 @@ def main(): sys.exit(ERROR_NO_CONTAINERS) print(f"Upload objects to each container: {args.preload_obj} ") - random_payload(payload_filepath, args.size) + payload_file = tempfile.NamedTemporaryFile() + random_payload(payload_file, args.size) print(" > Create random payload: Completed") for container in container_list: print(f" > Upload objects for container {container}") with ProcessPoolExecutor(max_workers=min(MAX_WORKERS, workers)) as executor: - objects_runs = {executor.submit(upload_object, container, payload_filepath, + objects_runs = {executor.submit(upload_object, container, payload_file.name, endpoints[random.randrange(len(endpoints))], wallet, wallet_config): _ for _ in range(int(args.preload_obj))} for run in objects_runs: diff --git a/scenarios/preset/preset_s3.py b/scenarios/preset/preset_s3.py index c28f47b..140d5ea 100755 --- a/scenarios/preset/preset_s3.py +++ b/scenarios/preset/preset_s3.py @@ -3,6 +3,7 @@ import argparse import json import sys +import tempfile from concurrent.futures import ProcessPoolExecutor from helpers.cmd import random_payload @@ -32,7 +33,6 @@ MAX_WORKERS = 50 def main(): bucket_list = [] objects_list = [] - payload_filepath = '/tmp/data_file' ignore_errors = True if args.ignore_errors else False workers = int(args.workers) @@ -62,13 +62,14 @@ def main(): sys.exit(ERROR_NO_BUCKETS) print(f"Upload objects to each bucket: {args.preload_obj} ") - random_payload(payload_filepath, args.size) + payload_file = tempfile.NamedTemporaryFile() + random_payload(payload_file, args.size) print(" > Create random payload: Completed") for bucket in bucket_list: print(f" > Upload objects for bucket {bucket}") with ProcessPoolExecutor(max_workers=min(MAX_WORKERS, workers)) as executor: - objects_runs = {executor.submit(upload_object, bucket, payload_filepath, + objects_runs = {executor.submit(upload_object, bucket, payload_file.name, args.endpoint): _ for _ in range(int(args.preload_obj))} for run in objects_runs: -- 2.45.2