Part III: The Struts Tag Libraries


Testing Application Performance

A final and sometimes overlooked area of testing is performance. Too often, an application is rolled out to production only to find that the performance of the application is unacceptable. For Struts applications, the first step to good performance is proper design and planning. Developers should plan for the expected number of concurrent users. Testing should be performed on a platform that is as similar as possible to the production environment. Developers are not often called on to test their applications for performance. However, tools are available that allow a developer to measure and test the performance of their application. One such tool, JUnitPerf, allows for performance tests to be run against existing JUnit tests. JUnitPerf accomplishes this by decorating a unit test. In other words, it wraps an existing test with a test that measures the performance of the wrapped test methods.

JUnitPerf (http://www.clarkware.com/software/JUnitPerf.html) was written and is maintained by Mike Clark of Clarkware Consulting. It can be downloaded and used free of charge. There are two main types of performance tests that JUnitPerf supports:

  • Timed tests  Measure the amount of time that a test takes. Assertions can be made about the maximum amount of time that the test should take. If the test takes longer than that, then the assertion fails.

  • Load tests  Allow the test writer to indicate the number of concurrent users (threads) that are running the test and optionally the number of times to execute the test.

To start, the following code defines a timed test for the simple JUnit EmployeeSearchServiceTest class that was created earlier, in the section "Testing the Model." Of course, the test execution should be integrated into the Ant build.

package com.jamesholmes.minihr;     import com.clarkware.junitperf.TimedTest; import junit.framework.Test; import junit.framework.TestSuite;     public class EmployeeSearchServicePerfTest {   public static final long toleranceInMillis = 10;       public static Test suite() {     long maxElapsedTimeInMillis = 10 + toleranceInMillis;     Test timedTest = new TimedTest(        new TestSuite(EmployeeSearchServiceTest.class),                      maxElapsedTimeInMillis);     TestSuite suite = new TestSuite();     suite.addTest(timedTest);     return suite;   } }

As you can see, this class is somewhat atypical of previous unit tests in that it does not extend TestCase. It does, however, provide a suite method that returns a Test. This method creates a test suite of a JUnitPerf TimedTest by wrapping a test suite created from the original EmployeeSearchServiceTest. The maximum time that the test should take to run is passed in as the second parameter to the TimedTest. This test can be run just as any other JUnit test-that is, using the JUnit test runners, IDE/JUnit integration, or via an Ant task.

Another tool, in the open-source domain, for testing and measuring the performance of Web applications is Apache JMeter (http://jakarta.apache.org/jmeter). JMeter allows a developer to create and run test plans against running applications. It was originally designed for testing Web sites, so it is well suited for most Struts applications. JMeter provides a Java Swing user interface for creating and running test plans. Performance metrics are gathered and can be visualized graphically as well as in other formats. To get started, you need to first download JMeter and install it. Follow the provided documentation to create a test plan. You can create thread groups that simulate a number of concurrent users. Then, you can specify the details of an HTTP request to test. Figure 12-4 shows an HTTP request configured to access the Search by Name feature of the Mini HR application.

image from book
Figure 12-4: JMeter HTTP request

The request is configured to access the URL http://localhost:8080/MiniHR/search.do. In addition, request parameters can be specified. In this case, the query string name=Jim will be added to the request.

The test can then be run against the running server, and the results are accumulated for each result listener. Figure 12-5 shows the display using the Graph Results listener. The thread group, in this case, was configured to run with 20 concurrent users, each making 25 requests to the Search page-a total of 500 data points.

image from book
Figure 12-5: JMeter graph results

From the results, you can see that on average the search could be performed in a bit over 50 milliseconds, with some requests taking as long as 120 milliseconds. JMeter is a powerful tool and has a great deal more capabilities than described here. It is well documented and has many additional features that you may want to use.

Performance is an area of your application that should not be overlooked-particularly if the application is targeted for high-volume, high-performance Web sites. As was shown in the preceding text, JUnitPerf and Apache JMeter can be used to test the performance of your Web application under simulated loads. These tools can be used to help identify those areas of your application that are bottlenecks. Once identified, profiling tools should be used to focus the performance analysis.



Struts. The Complete Reference
Struts: The Complete Reference, 2nd Edition
ISBN: 0072263865
EAN: 2147483647
Year: 2004
Pages: 165
Authors: James Holmes

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net