python之gevent模擬minio文件上傳/下載高併發

import gevent
from gevent import monkey
monkey.patch_all()
from minio import Minio
import time, random
import uuid


def get_client():
    """
    連接minio
    :return:
    """
    client = Minio(endpoint="xxx.xxx.xxx.xxx:9001",
                   access_key='xxxx',
                   secret_key='xxxxx',
                   secure=False  # 使用http
                   )
    return client


def upload_file(client, t, f):
    """
    上傳文件
    :param client:
    :param t:運行時長,單位s
    :param f:每秒上傳頻率
    :return:
    """
    file_name = '19d5c50e833d4fa8af3b7412d40000a2.jpg'
    file_path = r'E:\集成資料\視頻素材'
    barrel = "testdata"
    for i in range(t):
        client.fput_object(bucket_name=barrel, object_name="data1/" + str(uuid.uuid1()) + ".jpg",
                           file_path=file_path + "/" + file_name)
        time.sleep(1/f)
    stop_time = time.strftime("%y-%m-%d: %H:%M:%S", time.localtime())
    print(stop_time)


def download_file(client, t, f):
    """
    下載文件
    :param t:運行時長,單位s
    :param f:每秒上傳頻率
    :return:
    """
    file_path = r'E:/集成資料/測試項目/minio/'
    barrel = "testdata"
    files = client.list_objects(barrel, prefix="data1/")
    for i in range(1, t):
        file = random.choice(files)
        client.fget_object(bucket_name=barrel, object_name=file.object_name,
                           file_path=file_path + str(file.object_name))
        time.sleep(1/f)

    stop_time = time.strftime("%y-%m-%d: %H:%M:%S", time.localtime())
    print(stop_time)


def run_gevent_upload(num, t, f):
    """
    調用gevent模擬併發上傳
    :param num:
    :param t:運行時長,單位s
    :param f:每秒上傳頻率
    :return:
    """
    start_time = time.time()
    run_list = list()
    for i in range(num):
        run_list.append(gevent.spawn(upload_file(get_client(), t, f)))
    gevent.joinall(run_list)
    stop_time = time.time()
    print("執行總時長:", stop_time-start_time)


def run_gevent_download(num, t, f):
    """
    調用gevent模擬併發下載
    :param num:
    :param t:運行時長,單位s
    :param f:每秒上傳頻率
    :return:
    """
    start_time = time.time()
    run_list = list()
    for i in range(num):
        run_list.append(gevent.spawn(download_file(get_client(), t, f)))
    gevent.joinall(run_list)
    stop_time = time.time()
    print("執行總時長:", stop_time - start_time)


if __name__ == '__main__':
    num = 5
    t = 10
    f = 2
    run_gevent_upload(num, t, f)

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章