Locust 是一款基于python语言开发的一款新的性能压测工具,它是以协程(比线程还小)的方式运行。

先看一张 locust 运行图:
性能压测工具之 Locust_第1张图片

源码如下:

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

from locust import HttpLocust, TaskSet, task
import subprocess
import json
import time
import random
import hashlib
import os

class UserBer(TaskSet):
def on_start(self):
pass

@task
def pubsend(self):
    url = "/pub/send"
    f = open("F:/python/data.json", 'r')
    data = f.read()
    data = json.loads(data)
    data["from"]["time"] = int(time.time())
    data["from"]["nonce"] = random.randint(100000, 999999)

    pubToken = [data["from"]["no"], data["from"]["pub"], data["from"]["pubtoken"]
    pubToken.sort()
    pubToken = "".join(pubToken)
    sha1 = hashlib.sha1(pubToken.encode('utf-8'))
    sha1 = sha1.hexdigest()
    data["from"]["pubtoken"] = sha1
    dataCop = json.dumps(data)
    head = {"Content-Type": "application/json"}

    req = self.client.post(url=url,data=dataCop,headers=head)

    if req.status_code == 200 and req.['success']==True:
        print("响应正常!")          
    else:
        print("服务端返回异常!")

class MobileUserLocust(HttpLocust):
task_set = UserBer
host = "http://hlep.his.com"
min_wait = 1000
max_wait = 3000

if name == "main":

os.system("locust -f locust-script.py")

说明:wins 下 pip install locustio ;运行脚本后 在浏览器中输入: localhost:8089