Search code examples
pythonpython-3.xjmeterbottlecherrypy

JMeter and Python bottle - not closing sockets


We've got an API, written in Python and using Bottle and Cherrypy, which we need to stress test. We've created a JMeter script to do this, but after a couple of minutes of execution, the calls begin to fail with the following exception:-

java.net.BindException: Address already in use: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)

I can't seem to work out how to tell JMeter not to hang onto the sockets, or how to tell Bottle to drop them. We've tried it with options such as JMeter's KeepAlive on and off but without success.

We've pulled almost everything out of the API and this script still has the problem (without any of our functional baggage).

"""
This script provides a dummy API, which responds to every GET as if it found something.
"""

import bottle
import os
import sys
import datetime
import routes

if __name__ == '__main__':
    HOST = 'localhost'
    PORT = 5555

    @bottle.route('/thing/:id', builtin_function_or_method='GET')
    def get_thing(id):
        response_json = {
            'data': {
                'type': 'thing',
                'id': id,
                'meta': {
                    'date_retrieved': datetime.datetime.now().isoformat()
                }
            }
        }
        bottle.response.headers['Cache-Control'] = 'public,max-age=0'
        return response_json

    bottle.run(server='cherrypy', host=HOST, port=PORT)

This is Python 3.6.5, Cherrypy 8.9.1 and JMeter 3.3 running on Windows.


Solution

  • I have come across similar while running JMeter stress tests from a windows machine. This should happen due to port limitations set up by windows as standard. There are 5000 default ports as per their docs. You may try increasing this value using this guide