性能测试 - Locust Stomp client

性能测试 - Locust Stomp client

Max.Bai
2018-02


1. 需要安装stomp

stomp 可以连接activemq,还有其他。

pip install stomp.py

2. 实现

详细过程不写了,直接上代码,代码是向activemq queue 发送消息.

需要看详解的到这里

import time
import random
import stomp
from locust import Locust, TaskSet, events, task



class StompClient(object):
    def __init__(self, host, port):
        self.conn = stomp.Connection10([(host, port)])

    def __del__(self):
        if self.conn:
            print "disconnect..."
            self.conn.disconnect()

    def start(self):
        start_time = time.time()
        try:
            self.conn.start()
        except Exception as e:
            total_time = int((time.time() - start_time) * 1000)
            events.request_failure.fire(request_type="stomp", name="start", response_time=total_time, exception=e)
        else:
            total_time = int((time.time() - start_time) * 1000)
            events.request_success.fire(request_type="stomp", name="start", response_time=total_time, response_length=0)

    def connect(self):
        start_time = time.time()
        try:
            self.conn.connect()
        except Exception as e:
            total_time = int((time.time() - start_time) * 1000)
            events.request_failure.fire(request_type="stomp", name="connect", response_time=total_time, exception=e)
        else:
            total_time = int((time.time() - start_time) * 1000)
            events.request_success.fire(request_type="stomp", name="connect", response_time=total_time, response_length=0)
        
    def send(self, body, destination):
        start_time = time.time()
        try:
            self.conn.send(body=body, destination=destination)
        except Exception as e:
            total_time = int((time.time() - start_time) * 1000)
            events.request_failure.fire(request_type="stomp", name="send", response_time=total_time, exception=e)
        else:
            total_time = int((time.time() - start_time) * 1000)
            events.request_success.fire(request_type="stomp", name="send", response_time=total_time, response_length=0)

class StompLocust(Locust):
    """
    This is the abstract Locust class which should be subclassed. It provides an Stomp client
    that can be used to make Stomp requests that will be tracked in Locust's statistics.
    """
    def __init__(self, *args, **kwargs):
        super(StompLocust, self).__init__(*args, **kwargs)
        self.client = StompClient(self.host, self.port)
        self.client.start()
        self.client.connect()


def random_str():
    a2z = [chr(i) for i in range(97,123)]
    return ''.join(random.sample(a2z, 6))


class TestUser(StompLocust):
    host = "200.200.200.55"
    port = 61613
    min_wait = 100
    max_wait = 1000

    class task_set(TaskSet):
        
        @task
        def send_data(self):
            des = "foo.bar"
            msg = random_str()
            self.client.send(msg, des)
            print "sent ", msg


if __name__ == "__main__":
    user = TestUser()
    user.run()



猜你喜欢

转载自blog.csdn.net/max229max/article/details/79360070