Search This Blog

Sunday, 2 May 2010

Tomacat on Linux Part 3 - Performance

Performance.
Tomcat offers many configuration options that can improve the performance on your system. Here I'll be covering some of the main configuration options for boosting performance you may want to look at.
TCP/IP configuration
Ipv6 as of yet is not widely supported on all networks, this can cause a problem and even connections can be dropped. By default this Tomcat installation uses Ipv6 but this can be changed easily. If you open up a terminal and type $ lsof -i you will be presented with a table that shows what is using Ipv6 and what is using Ipv4. [Christopher Schultz. (2010).]
To configure Tomcat so that it uses Ipv4 instead of Ipv6 shut down Tomcat using either the shutdown.sh script or the “$ service tomcat stop” command then type
$ export CATALINA_OPTS=-Djava.net.preferIPv4Stack=true
Then start Tomcat once again and check that it is now using Ipv4 with the $ lsof -i command.
Heap Size
Runaway programs could cause a problem, eating up all your RAM and overwhelming your Operating System. The Java runtime environment has what is called a “maximum heap size”, this is a limit on how much memory can be used and prevents any potential problems caused by any runaway program. For JDK 1.3 for example the default limit was 32MB, this was established at a time when memory was more expensive. [Jason Brittian (2007). p19]
You can improve the memory usage by setting the environment variables your self or for each class for example $ java -Xmx=256M MyClass will run a class file called MyClass with a maximum memory size of 256MB. To change the environment variable in JAVA_OPTS you can type in
$ export JAVA_OPTS=-Xmx256M
This will now set the environment to run classes at a maximum of 256MB thus improving the performance over the default option. Using the JAVA_OPTS function will make system wide changes, you can limit the changes to be Tomcat only by instead using
$ export CATALINA_OPTS=-Xmx256M
Now I'm going to use a load testing tool to measure and see the difference in performance that changing the heap size can have. The tool I'm using is Jmeter, you can get this for Ubuntu by typing
$ sudo apt-get install jmeter
First with CATALINA_OPTS being set to 32M Jmeter came up with these results
From these results you can see that 5000 HTTP requests were thrown at Tomcat and the average time to process those requests were 327 milliseconds with the Maximum time being 5161 milliseconds and the throughput being 116.6 requests per second. Now we can compare these results with what we get after changing the environment variable to 512M
The average time has dropped from 327 to 17 milliseconds and the maximum time to 1164 milliseconds. The throughput has increased from 116.6 requests per second to 167.7. So here you can see the performance boost you can gain from increasing the heap size.

Timeouts and connections

Idle connections can cause a lot of consumption of the system's memory resources, this can be prevented with the use of a connection timeout. By configuring the connection timeout variable you can control how long server resources are allocated to tasks and clients. [Microsoft. (2003).] To change the connection timeout variable go back to edit your server.xml file as was done for changing the port number, you'll find the conectionTimeout variable just below where the port number is defined. Change this number to suit your needs, this time is in milliseconds but keep the timeout within a sensible range as making it too low can be counter productive.
If we change this variable so that connectionTimeout="20000" and run Jmeter we get these results :
And if we now change this variable to connectionTimeout=”50000” and run Jmeter we get these results :
As you can see from these results the higher the connection timeout time the longer each sample takes to process. Now just to show the effects of having the timeout set too low, here are the Jmeter results with a connection timeout value of 1 :
From these results you can see that not only is the process time greater than the other two timeout values but there is a significant error percentage level shown where many samples where just simply unable to connect. [Jason Brittain (2007). p156]
You can also define the maximum number of connections you want to allow to your server, by doing this you can prevent Denial of Service attacks and too many connections eating up your system's memory. You can set this variable in the server.xml file by simply uncommenting the following line :
Here the default maximum connections allowed is 150, you can change this value to suit your needs and the demands on the server.

DNS Server

Web Applications can log a client's details such as looking up their Domain Name Service data. These DNS look ups can cause delays as it causes traffic from your server that could pass through many servers that maybe far away. To stop this from happening edit the line enableLookups so that the value is changed from true to false in the server.xml file. By changing this variable the server will no longer go off to find the DNS data, instead it can just log the numeric IP address, then if required the DNS data can be looked up outside of tomcat. This setting can also reduce disk space usage as less data is stored. [Jason Brittain (2007). p156]

Performance Monitoring

Changing these values can improve performance but over time with the possibility that visitor numbers may increase you might need to tweak these settings to keep good performance. You could periodically run Jmeter to check your server's performance optimisation or even write a script to automatically run a load testing tool such as siege and log these details for you.
Now that we've looked at installing, configuring and optimising performance of the Tomcat server you'll want to be able to secure it, the next part is on making your server secure.

No comments:

Post a Comment