locust 通过队列实现 task级集合点并发

最近公司在搞压测 笔者也是查了很多工具 

lr收费 用不起 jm 测试机扛不住  最后就选择了locust

使用中有个场景覆盖不到 就是几个task中 有一个需要并发提交,其余task正常运行即可

也是查了一些资料官网上给的钩子方法 是基于协程的运行开始前可实现并发一旦运行开始即开始即不可控

话不多说直接上代码

# !/usr/bin/env python
# -*- coding:utf-8 -*-
# Author:victor Li

# 任务类
import queue
import random
import time

from gevent._semaphore import Semaphore

all_locusts_spawned = Semaphore()
all_locusts_spawned.acquire()
from locust import HttpUser, task, events, SequentialTaskSet


class TestTaskList(SequentialTaskSet):

    def test_002(self):
       return random.Random().randint(1,100)
    @task
    def test_0004(self):
        print("test_0004")
    def test_003(self):

        if not self.user.out_queue.empty():
            print("test_003")
            self.user.my_queue.put(self.user.out_queue.get())
            self.user.out_queue.task_done()

    @task
    def test_001(self):


        if not self.user.my_queue.empty() and self.user.out_queue.empty():

            num = self.user.my_queue.get()
            self.user.my_queue.task_done()
            if self.user.my_queue.empty():
                pass
            else:
                self.user.my_queue.join()

            print("执行了呀{}".format(num))
            print("test_003返回结果是{}".format(self.test_002()))
            self.user.out_queue.put(num)
            self.test_003()



class TestUser(HttpUser):
    host = "www.baidu"
    out_queue = queue.Queue(20)
    tasks = {TestTaskList:1}
    max_wait = 1
    min_wait = 1
    my_queue = queue.Queue(20)
    for i in range(20):
        my_queue.put(i)



if __name__ == '__main__':
    import os

    os.system("locust -f test111.py --headless -u 20 -r 2 --run-time 1h30m")

 

 

执行效果

locust 通过队列实现 task级集合点并发_第1张图片

locust 通过队列实现 task级集合点并发_第2张图片

locust 通过队列实现 task级集合点并发_第3张图片

 

上面这个版本有个问题  就是 当没有异常得时候可以正常运行  有异常得时候可能会直接挂掉 然后协程再次进入得时候判断条件不满足 无法再次执行 线程进入假死状态

# !/usr/bin/env python
# -*- coding:utf-8 -*-
# Author:victor Li

# 任务类
import queue
import random
import time
import traceback

from gevent._semaphore import Semaphore

all_locusts_spawned = Semaphore()
all_locusts_spawned.acquire()
from locust import HttpUser, task, events, SequentialTaskSet


def on_request_success(request_type, name, response_time, response_length):
    print('Type: %s, Name: %s, Time: %fms, Response Length: %d' % \
          (request_type, name, response_time, response_length))


def on_request_failure(request_type, name, response_time, exception):
    print("on_request_failure")
    print('Type: %s, Name: %s, Time: %fms, Reason: %r' % \
          (request_type, name, response_time, exception))


def on_hatch_complete(user_count):
    print("Haha, Locust have generate %d users" % user_count)


def on_quitting():
    print("Locust is quiting")


def on_hatch_complete(**kwargs):
    print("孵化完成咯")
    all_locusts_spawned.release()


def on_test_start(**kwargs):
    print("test_start")
def on_test_stop(**kwargs):
    print("on_test_stop{}".format(kwargs))

# 加上了程序异常得处理 就可以正常运行了
def on_locust_error(user_instance, exception, tb):
    print("触发了 on_locust_error")
    print("%r, %s, %s" % (user_instance.user.test_111, exception, "".join(traceback.format_tb(tb))))
    user_instance.user.my_queue.put(user_instance.user.out_queue.get())
    user_instance.user.out_queue.task_done()


events.user_error.add_listener(on_locust_error)
events.quitting.add_listener(on_quitting)
events.test_start.add_listener(on_test_start)
events.hatch_complete.add_listener(on_hatch_complete)
events.test_stop.add_listener(on_test_stop)



class TestTaskList(SequentialTaskSet):

    def test_002(self):
        return random.Random().randint(1, 100)





    @task
    def test_001(self):
        print("判断上得执行步骤")
        is_empty_out_queue = self.user.out_queue.empty()
        is_empty_my_queue = self.user.my_queue.empty()
        print("is_empty_out_queue={},is_empty_my_queue={}".format(is_empty_out_queue, is_empty_my_queue))
        if not self.user.my_queue.empty() and self.user.out_queue.empty():

            num = self.user.my_queue.get()
            self.user.my_queue.task_done()
            if self.user.my_queue.empty():
                pass
            else:
                self.user.my_queue.join()

            print("执行了呀{}".format(num))
            raise RuntimeError("12312321")
            print("test_003返回结果是{}".format(self.test_002()))
            self.user.out_queue.put(num)
            self.test_003()
    def test_003(self):

        if not self.user.out_queue.empty():
            print("test_003")
            self.user.my_queue.put(self.user.out_queue.get())
            self.user.out_queue.task_done()

class TestUser(HttpUser):
    host = "www.baidu"
    out_queue = queue.Queue(3)
    tasks = {TestTaskList: 1}
    max_wait = 1
    min_wait = 1
    my_queue = queue.Queue(3)
    test_111=0
    for i in range(3):
        my_queue.put(i)


if __name__ == '__main__':
    import os

    os.system("locust -f test111.py --headless -u 3 -r 3 --run-time 1h30m")
    #os.system("locust -f test111.py")

 

ps:只想到了用队列锁得方式 如果大家有什么其他更好得办法也可以私信我 /加qq群:834515346

你可能感兴趣的:(locust,python)