0

I need to use Locust to send custom bytes on a socket to load test my apps.

I don't quite see how do that. I can only see it being used as a HTTP traffic injector which is not suitable for my purposes.

I need something more along the lines of this pseudocode. That is, I need to inject my custom bytes into the socket.

class User(I don;t know what suppose to go here. I'm not using HTTP):
    wait_time = between(1, 3)
    host = "https://docs.locust.io"

    @task
    def my_task(self):
        self.getSocket.write(bytes)

Can anyone give me a hint on how to achieve this?

Jenia Ivanov
  • 2,485
  • 3
  • 41
  • 69

1 Answers1

1

You'll have to define your own User, and have it log an event based on the time it took to send those bytes.

See https://docs.locust.io/en/stable/testing-other-systems.html or https://github.com/SvenskaSpel/locust-plugins/blob/master/locust_plugins/users.py for some inspiration/examples.

If you are just writing bytes to a socket, and not waiting for a response that will be very hard to do because there really is no "response time" to measure.

Edit: here is an old example (needs updating for locust 1.0): https://www.programmersought.com/article/53484428905/

Cyberwiz
  • 11,027
  • 3
  • 20
  • 40
  • Hey. No no. I;m waiting for a response for sure. I just need to figure out how to get the socket from Locust... – Jenia Ivanov Jul 27 '20 at 17:03
  • You cant. At least not in a meaningful way. HttpUser uses the requests module, and that doesnt expose the underlying socket (and if it did, it wouldnt work anyway, because HttpUser only does timing for http requests, by wrapping the calls). Sorry, but I dont know of a ”SocketUser” (though I could see the use for it) – Cyberwiz Jul 27 '20 at 17:30
  • Hi. This example https://github.com/SvenskaSpel/locust-plugins/blob/master/locust_plugins/users.py is what I need I think. I have one question though. Where is the `SocketIOUser` used? I can only see it being defined. – Jenia Ivanov Jul 27 '20 at 21:01
  • 1
    Unfortunately I think you are confused by the name: SocketIO is a protocol based on WebSockets and has nothing to do with ”raw” TCP sockets. The User classes in that file are meant to be subclassed (like with HttpUser). There are examples of use here: https://github.com/SvenskaSpel/locust-plugins/blob/master/examples/socketio_ex.py (but again, it doesnt do what you want, but it does give you an example of a custom protocol implementation) – Cyberwiz Jul 27 '20 at 21:07
  • Why do they register `receive` function with `gevent` but not the `send` function. Who executes the `send` function? – Jenia Ivanov Jul 27 '20 at 21:32
  • Because SocketIO is asyncronous, and there is no relation between sent requests and received ones (typically, in my particular use case the requests being sent are subscribe messages and the received ones are completely async). It does not relate to anything in other use cases. Focus your attention on the other two links in my answer (particularly the last one) – Cyberwiz Jul 27 '20 at 21:41
  • Ok. I managed to put something together that looks usable but I got one more question: how do you execute a task a specific number of times – Jenia Ivanov Jul 28 '20 at 14:35
  • Great! The easiest way is just to use a regular python for loop inside the task. You can also specify the weight of a task, but that will only make them run more frequently, not X times in a row. If you get your "SocketUser" working with Locust 1.x, will you consider making a pull request to locust-plugins? – Cyberwiz Jul 28 '20 at 15:37