Question

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!

Was it helpful?

Solution

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.

OTHER TIPS

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top