import botocore.session import os import linecache import botocore.exceptions import gc import time import sys import tracemalloc import logging import re print(f"Using: {sys.argv[1]}") def display_top(snapshot): top_stats = snapshot.statistics('traceback') for stat in top_stats: print("%s memory blocks: %.1f KiB" % (stat.count, stat.size / 1024)) for line in stat.traceback.format(): print(line) def test(s3_client): try: method = getattr(s3_client, sys.argv[1]) method(Bucket='archpi.dabase.com', Key='doesnotexist') except botocore.exceptions.ClientError as e: if e.response['ResponseMetadata']['HTTPStatusCode'] != 404: raise def do_get_obj(s3_client): response = s3_client.get_object(Bucket='archpi.dabase.com', Key='style.css') data = response["Body"].read() response["Body"].close() def main(): logging.basicConfig(level=logging.INFO) boto_session = botocore.session.get_session() boto_config = botocore.config.Config(connect_timeout=15, read_timeout=15, max_pool_connections=1) s3_client = boto_session.create_client('s3', config=boto_config) do_get_obj(s3_client) start = time.time() cycles = 0 while True: test(s3_client) # to avoid getting listed in tracemalloc results re._cache.clear() gc.collect() cycles += 1 if cycles == 5: tracemalloc.start(40) if cycles == 25: snapshot = tracemalloc.take_snapshot() display_top(snapshot) if time.time() - start > (60 * 5): break if __name__ == '__main__': main()