Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extended socket protocol #776

Closed
oleeks opened this issue Apr 17, 2018 · 3 comments
Closed

Extended socket protocol #776

oleeks opened this issue Apr 17, 2018 · 3 comments

Comments

@oleeks
Copy link

oleeks commented Apr 17, 2018

Hello,
How to expand the socket protocol? There are lots of connection errors in the following ways.
thaks.

from locust import Locust, events, task, TaskSet
import socket
import time

class TcpSocketClient(socket.socket):
    def __init__(self, af_inet, socket_type):
        super(TcpSocketClient, self).__init__(af_inet, socket_type)

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

    def send(self, data, flags=None):
        start_time = time.time()
        try:
            super(TcpSocketClient, self).send(data)
        except Exception as e:
            total_time = int((time.time() - start_time) * 1000)
            events.request_failure.fire(request_type="tcpsocket", name="send", response_time=total_time, exception=e)
        else:
            total_time = int((time.time() - start_time) * 1000)
            events.request_success.fire(request_type="tcpsocket", name="send", response_time=total_time,
                                        response_length=0)

    def recv(self, buffersize, flags=None):
        start_time = time.time()
        try:
            r = super(TcpSocketClient, self).recv(buffersize)
        except Exception as e:
            total_time = int((time.time() - start_time) * 1000)
            events.request_failure.fire(request_type="tcpsocket", name="recv", response_time=total_time, exception=e)
        else:
            total_time = int((time.time() - start_time) * 1000)
            events.request_success.fire(request_type="tcpsocket", name="recv", response_time=total_time,
                                        response_length=0)
            return r


class TcpSocketLocust(Locust):
    """
    This is the abstract Locust class which should be subclassed. It provides an TCP socket client
    that can be used to make TCP socket requests that will be tracked in Locust's statistics.
    """
    def __init__(self):
        super(TcpSocketLocust, self).__init__()
        self.client = TcpSocketClient(socket.AF_INET, socket.SOCK_STREAM)



class TcpTestUser(TcpSocketLocust):
    min_wait = 100
    max_wait = 1000

    class task_set(TaskSet):
        @task
        def send_data(self):
            ADDR = ("127.0.0.1", 8000)
            self.client.connect(ADDR)
            send_data = 'abcd'.encode('utf-8')
            self.client.send(send_data)
            data = self.client.recv(1024).decode('utf-8')
            print(data)

Environment settings (for bug reports)

  • OS: win10
  • Python version:3.6.4
  • Locust version:0.8.1

Steps to reproduce (for bug reports)

image

TBD - example code appreciated

@aldenpeterson-wf
Copy link
Contributor

@guifa2015 do you still need help with this issue?

@oleeks
Copy link
Author

oleeks commented Nov 30, 2018 via email

@cgoldberg
Copy link
Member

closing for lack of detail.

If you found an issue with Locust, please submit a new report containing a clear description of the problem.
Make sure to include a concise test case that reproduces the issue, including expected and actual results.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants