Tomcat: limit bandwidth for localhost

3.3k views Asked by At

Is it possible to limit bandwidth of the Tomcat server? I need it to simulate site work on slow (dial-up) Internet connection. All tests are made in localhost.

Thanks for all!

3

There are 3 answers

0
Tomasz Nurkiewicz On BEST ANSWER

It's not possible in Tomcat, you should look somewhere outside for an HTTP proxy providing such functionality and tunnel your tests through that proxy. In worst case write such a proxy yourself, shouldn't take more than a dozen lines of code.

0
UBIK LOAD PACK On
0
poussma On

This is easily feasible in Tomcat by creating a valve.

Create a class that extends the ValveBase class from tomcat.

The code inside should be something like that:

/**
 * {@inheritDoc}
 */
@Override
public void invoke(final Request request, final Response response) 
                   throws IOException, ServletException {

    if (randomDelay != 0) {
        long delay = minDelay + randomizer.nextInt(randomDelay);
        try {
            if (LOGGER.isDebugEnabled()) {
                LOGGER.debug("sleeping for " + delay + " ms");
            }
            Thread.sleep(delay);
        } catch (InterruptedException e) {
            if (LOGGER.isDebugEnabled()) {
                LOGGER.debug("someone wake me up : " + e.getMessage());
            }
        }
    }

    // let's continue !
    getNext().invoke(request, response);
}

Compile it in a jar you'll drop in the tomcat lib directory (usually {catalina.home}/lib).

Finally add the valve declaration in you server.xml:

<Server ...>
  <Service name="Catalina">
   <Engine name="Catalina" defaultHost="localhost">
     (...)
     <Host name="localhost" ...>
       <Valve className="tools.tomcat.RandomDelayValve" />

HIH