Glimpse on Tomcat performance tuning.

Production environment ;-)

Have You ever wondered about Tomcat configuration in production environment, or just let “this things” to the admins, or even worse, don’t care at all about it? If the answer is “Tomcat configuration ? I/We/Our client just installs tomcat and deploy our application. Why border about any additional configuration ?” You should read this post.

I will not write about all Tomcat’s configuration. It’s pointless. I just want to show some problems with performance with default Tomcat’s configuration in production enviroment. Especially if You are using Tomcat in as web server in internet, with many simultaneous clients and connections. In such cases performance and high responsivity is important.

1. Let’s start from logs. Standard Tomcat’s logs are configured to appear in two places: file and console. In production it’s pointless to have duplicate logs so first thing to gain some speed boost is to replace following line from logging.properties:

.handlers = 1catalina.org.apache.juli.FileHandler, java.util.logging.ConsoleHandler with this one: .handlers = 1catalina.org.apache.juli.FileHandler

2. Second thing to do with logs is to set max file size and protection from overflow. It’s also very easy. Just add new handler like following one:

catalina.java.util.logging.FileHandler

and configure it like this (max 4 filesx10Mb):

1catalina.java.util.logging.FileHandler.pattern = ${catalina.base}/logs/catalina.%g.log 1catalina.java.util.logging.FileHandler.limit = 10000000 1catalina.java.util.logging.FileHandler.count = 4

3. Last thing You have TO HAVE in production environment are asynchronous logs. Synchronous logging is far more time consuming then asynchronous one. Especially when You have numerous clients. Check if Your Tomcat is configured in proper way (I won’t write about this. Just search in web about log4j configuration. It’s lot of this there.)

4. That’s all about logging. Now something much more influent on connection speed-connectors. They are configured in server.xml under node.

Tomcat have 3 main connectors:

BIO – Blocking Java connector which is default one

APR – Uses native C code fo IO (very fast)

NIO – Non blocking connectror in Java (also faster than default)

The first BIO connector (“org.apache.coyote.http11.Http11Protocol”) is set as default one. Why ? Becouse in many cases such configuration it’s enough. Tomcat usually is used in intranets where it’s not required to handle high traffic volume. Moreover BIO connector is very stable.

But if our applications have to serve many http requests the blocking connector isn’t the best choice. So here comes ARP and NIO connector.

The first one (org.apache.coyote.http11.Http11AprProtocol) requires to compile native library (just search in google for ARP) and could be less stable than BIO connector. In exchange ARP connector is very fast, could handle requests simultanously in non blocking mode, have pooling of unlimited size and could handle unlimited threads (in theory, becouse threads are limited with CPU power)

Last connector – NIO (org.apache.coyote.http11.Http11NioProtocol) is something between ARP and BIO. It’s good choice if You don’t want to compile native libraries. NIO connector is also non blocking, little slower in reading static content than ARP, but far more configurable (pool size, no of threads etc).

5. Ok, so now We know, which connector should we choose, but every connector have to be set up in proper way. There are several parameters but the important ones are:

– maxThreads – typical from 150-800 (For BIO this is max nr of open connections)

– maxKeepAliveRequests – typical 1 or 100-250. For BIO this should be set to 1 to disable keep alive (only if we have high concurency and not using SSL). BIO connector automatically disables keep alive for high connection traffic

– connectionTimeout – typical 2000-60000 WARNING: default Tomcat has it set to 20 000! It’s to high for production environment. Good choice is to decrese it to 3000-5000 unless Your production env is working with slow clients. This parameters describes max time between TCP packets during blocking read/write

6. This is “almost” the end of tunning Tomcat for production. The last thing is to configure cache. Default cache is configured to 10 MB. You can set this a little more if You have a lot of static content. Also cache revalidation (standard 5 sec) should be tuned. How ? It’s difficult to say. The best way is to tune this parameters by own during tests.

That’s all. I hope I realized to everyone why not rely on standard Tomcat configuration.

You May Also Like

Thought static method can’t be easy to mock, stub nor track? Wrong!

No matter why, no matter is it a good idea. Sometimes one just wants to check or it's necessary to be done. Mock a static method, woot? Impossibru!

In pure Java world it is still a struggle. But Groovy allows you to do that really simple. Well, not groovy alone, but with a great support of Spock.

Lets move on straight to the example. To catch some context we have an abstract for the example needs. A marketing project with a set of offers. One to many.

import spock.lang.Specification

class OfferFacadeSpec extends Specification {

    OfferFacade facade = new OfferFacade()

    def setup() {
        GroovyMock(Project, global: true)
    }

    def 'delegates an add offer call to the domain with proper params'() {
        given:
            Map params = [projId: projectId, name: offerName]

        when:
            Offer returnedOffer = facade.add(params)

        then:
            1 * Project.addOffer(projectId, _) >> { projId, offer -> offer }
            returnedOffer.name == params.name

        where:
            projectId | offerName
            1         | 'an Offer'
            15        | 'whasup!?'
            123       | 'doskonała oferta - kup teraz!'
    }
}
So we test a facade responsible for handling "add offer to the project" call triggered  somewhere in a GUI.
We want to ensure that static method Project.addOffer(long, Offer) will receive correct params when java.util.Map with user form input comes to the facade.add(params).
This is unit test, so how Project.addOffer() works is out of scope. Thus we want to stub it.

The most important is a GroovyMock(Project, global: true) statement.
What it does is modifing Project class to behave like a Spock's mock. 
GroovyMock() itself is a method inherited from SpecificationThe global flag is necessary to enable mocking static methods.
However when one comes to the need of mocking static method, author of Spock Framework advice to consider redesigning of implementation. It's not a bad advice, I must say.

Another important thing are assertions at then: block. First one checks an interaction, if the Project.addOffer() method was called exactly once, with a 1st argument equal to the projectId and some other param (we don't have an object instance yet to assert anything about it).
Right shit operator leads us to the stub which replaces original method implementation by such statement.
As a good stub it does nothing. The original method definition has return type Offer. The stub needs to do the same. So an offer passed as the 2nd argument is just returned.
Thanks to this we can assert about name property if it's equal with the value from params. If no return was designed the name could be checked inside the stub Closure, prefixed with an assert keyword.

Worth of  mentioning is that if you want to track interactions of original static method implementation without replacing it, then you should try using GroovySpy instead of GroovyMock.

Unfortunately static methods declared at Java object can't be treated in such ways. Though regular mocks and whole goodness of Spock can be used to test pure Java code, which is awesome anyway :)No matter why, no matter is it a good idea. Sometimes one just wants to check or it's necessary to be done. Mock a static method, woot? Impossibru!

In pure Java world it is still a struggle. But Groovy allows you to do that really simple. Well, not groovy alone, but with a great support of Spock.

Lets move on straight to the example. To catch some context we have an abstract for the example needs. A marketing project with a set of offers. One to many.

import spock.lang.Specification

class OfferFacadeSpec extends Specification {

    OfferFacade facade = new OfferFacade()

    def setup() {
        GroovyMock(Project, global: true)
    }

    def 'delegates an add offer call to the domain with proper params'() {
        given:
            Map params = [projId: projectId, name: offerName]

        when:
            Offer returnedOffer = facade.add(params)

        then:
            1 * Project.addOffer(projectId, _) >> { projId, offer -> offer }
            returnedOffer.name == params.name

        where:
            projectId | offerName
            1         | 'an Offer'
            15        | 'whasup!?'
            123       | 'doskonała oferta - kup teraz!'
    }
}
So we test a facade responsible for handling "add offer to the project" call triggered  somewhere in a GUI.
We want to ensure that static method Project.addOffer(long, Offer) will receive correct params when java.util.Map with user form input comes to the facade.add(params).
This is unit test, so how Project.addOffer() works is out of scope. Thus we want to stub it.

The most important is a GroovyMock(Project, global: true) statement.
What it does is modifing Project class to behave like a Spock's mock. 
GroovyMock() itself is a method inherited from SpecificationThe global flag is necessary to enable mocking static methods.
However when one comes to the need of mocking static method, author of Spock Framework advice to consider redesigning of implementation. It's not a bad advice, I must say.

Another important thing are assertions at then: block. First one checks an interaction, if the Project.addOffer() method was called exactly once, with a 1st argument equal to the projectId and some other param (we don't have an object instance yet to assert anything about it).
Right shit operator leads us to the stub which replaces original method implementation by such statement.
As a good stub it does nothing. The original method definition has return type Offer. The stub needs to do the same. So an offer passed as the 2nd argument is just returned.
Thanks to this we can assert about name property if it's equal with the value from params. If no return was designed the name could be checked inside the stub Closure, prefixed with an assert keyword.

Worth of  mentioning is that if you want to track interactions of original static method implementation without replacing it, then you should try using GroovySpy instead of GroovyMock.

Unfortunately static methods declared at Java object can't be treated in such ways. Though regular mocks and whole goodness of Spock can be used to test pure Java code, which is awesome anyway :)

JCE keystore and untrusted sites

Recently at work I was in need of connecting to a web service exposed via HTTPS. I've been doing this from inside Servicemix 3.3.1, which may seem a bit inhibiting, but that was a requirement. Nevertheless I've been trying my luck with the included ser...Recently at work I was in need of connecting to a web service exposed via HTTPS. I've been doing this from inside Servicemix 3.3.1, which may seem a bit inhibiting, but that was a requirement. Nevertheless I've been trying my luck with the included ser...