python--并发测试脚本

2020-07-22  本文已影响0人  w_dll

写的玩的,压力测试 , 跑并发,拿自己服务器跑,10000并发会崩溃;

#!/usr/bin/python
# -*- coding: utf-8 -*-

import requests
import json
import threading
import time


class myThread (threading.Thread):
    def __init__(self, name):
        threading.Thread.__init__(self)
        self.name = name
    def run(self):
        this_str="start :"+self.name
        print (this_str)
        headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36',
        'Content-Type': 'application/json; charset=UTF-8',
        }
        url = 'http://www.test.com'
        try:
            r = requests.post(url, headers)
        except Exception as e:
            print(e)
        this_str="end :"+self.name
        print (this_str)
 
 
 
try:
    i = 0
    # 开启线程数目
    tasks_number = 3000
    print('启动')
    time1 = time.clock()
    while i < tasks_number:
        t_name = i+1
        t = myThread(str(t_name))
        t.start()
        i +=1
    time2 = time.clock()
    times = time2 - time1
    print(times/tasks_number)
except Exception as e:
    print(e)
上一篇下一篇

猜你喜欢

热点阅读