Python压力测试

2019-12-09  本文已影响0人  Michael不想说话

安装progressbar

pip install progressbar2

使用多线程测试代码

import threading
import os
import requests
import random
import uuid
import json
import time
import csv
import shutil
from progressbar import *

class PostFile(threading.Thread):
    def __init__(self, tid, num, url, dir, output):
        super(PostFile, self).__init__()
        self.url = url
        self.tid = tid
        self.dir = dir
        self.num = num
        self.output = output
        self.receive_count = 0
        self.token_accept = 0
        self.file_list = os.listdir(self.dir)

    def send_post(self):
        token = uuid.uuid4().__str__()
        file_name = self.file_list[random.randint(0, len(self.file_list)-1)]
        file = {'file': (file_name, open(os.path.join(self.dir, file_name), 'rb'))}
        data = {'keepFile': 1, 'token': token}
        global global_count
        global_count += 1
        try:
            start = time.time()
            response = requests.post(self.url, files=file, data=data)
            end = time.time()
            response = json.loads(response.text)
            if response['token'] is not None:
                self.receive_count += 1
            if token == response['token']:
                self.token_accept += 1
            item = [token, response['token'], file_name, response['label'], end-start]
        except Exception as e:
            item = [token, '', file_name, '', 0]
        finally:
            return item


    def run(self):
        response = []
        thread_start = time.time()
        for i in range(self.num):
            item = self.send_post()
            response.append(item)
        thread_end = time.time()
        with open(os.path.join(self.output, "thread-[{}]-receive-[{}]-token-same-[{}]-cost-[{:.4f}].csv"
                .format(self.tid, self.receive_count, self.token_accept, thread_end-thread_start)),
                  'w', newline='') as t:
            writer = csv.writer(t)
            writer.writerows(response)


def build_worksapce(dir):
    dir = os.path.abspath(dir)
    if os.path.exists(dir):
        shutil.rmtree(dir)
    os.mkdir(dir)

if __name__ == '__main__':
    send_num = 100000
    thread_num = 100
    url = 'post_url'
    dir = './post_file_dir'
    output = './log_dir'
    global_count = 0
    build_worksapce(output)
    widgets = ['Progress: ', Percentage(), ' ', Bar('#'), ' ', Timer(), ' ', ETA(), ' ']
    pbar = ProgressBar(widgets=widgets, maxval=send_num).start()
    start = time.time()
    for i in range(thread_num):
        post = PostFile(tid=i, num=send_num//thread_num, url=url, dir=dir, output=output)
        post.start()

    while global_count != send_num:
        pbar.update(global_count+1)

    end = time.time()
    print('\nSend: {}, used: {:.4f} s.'.format(send_num, end-start))

上一篇下一篇

猜你喜欢

热点阅读