杨钦元 博客

python 多线程性能压测

# -*- coding:utf-8 -*-
import threading,time,requests

def post():
    url = 'http://***/'
    param = {'message':'***','client_acc_code':'***'}
    start = time.time()
    res = requests.post(url,param)
    if res.status_code!=200:
        print(threading.current_thread().getName() + ' error : ' + str(res.status_code))
    else:
        end = time.time()
        print(threading.current_thread().getName() + ' : ' + str(end - start))

if __name__=='__main__':
    run_time = 60 #执行次数
    thread_count = 100 #并发数
    for x in range(run_time):
        i = 0
        while i < thread_count:
            i += 1
            t = threading.Thread(target=post)
            t.start()