Performance Issues Abound! 2003 Wily Benchmark Survey shows 60% of time Java applications fail to meet user expectations Only 42% of applications perform as planned during deployment 57% of application performance spent in data access 2004 Forrester 66% of time developers find out about performance problems from user calls!
J2EE Application Complexity Enterprise Information Systems Client Side Presentation Desktop Java Application Device J2EE Client Browser Pure HTML Java Applet J2EE Platform Web Server Server-Side Presentation JSP Servlet JSP J2EE Platform EJB Container Server-Side Business Logic EJB
Premature optimization is the root of all evil - Professor Sir Charles Anthony Richard Hoare There are two rules for when to optimize: 1. Don't do it. 2. (For experts only) Don't do it yet. There are two rules for when to optimize: 1. Don't do it. 2. (For experts only) Don't do it yet. - Michael Jackson, Principles of Program Design
Test driven design can lead to emergent optimization and code that is readily optimizable. If programmers develop test first, many of their upfront concerns about performance can be deferred. - Michael Feathers, Emergent Optimization in Test Driven Design
Methodical approach to performance evaluation Develop Identify Design Test Evaluate
Approach Tuning Issues Logically and Iteratively Java Virtual Machine Application Server Hardware and Operating System Applications Component Database Tuning and debugging are ongoing iterative processes. There are no magic bullets.
Dont Forget That There are Tools to Help Profilers Debuggers Code audits and metrics Load testing tools Vendor performance and configuration guidelines
Tuning JDBC Performance: Start with the Obvious Use connection pooling – Connection objects are expensive – Tailor min and max connections to your application Avoid cycling physical database connections – Look for database connections timing out Tune statement caching – Cache distinct SQL statements
No Connection Pooling RacingFacade J2EE Container create teamOrders... Make connection, do Query Return result and disconnect (unless application itself does connection pooling)
With Connection Pooling RacingFacade J2EE Container create teamOrders... Application uses available connections Connection Pooling From the Container
Tune Your SQL! Easier said than done – What is the real SQL running in CMP EJB? Look at the SQL on the wire – Tools like P6Spy, Oracle Enterprise Manager Become good friends with your DBA – Tune using traditional techniques Explain plan Tools like SQLPlus and Oracle9 i JDeveloper
EJB - Locking-Mode and Isolation Pessimistic locking is generally slower – May be required for applications where data collisions are likely Increasing isolation decreases performance – Evaluate your data consistency requirements
Transactions and Performance Entity beans load/store data at transaction boundaries – Transactions settings affect how often database is accessed – Poor performance can be caused by transaction settings Rules of thumb – Always use transactions for entity bean methods – Scope the unit of work from a session bean
Resulting Transactional Activity TopicBean: ejbCreate id = 0 TopicBean: ejbCreate id = 1 TopicBean: ejbCreate id = 2 </Create Test TopicBean: ejbStore id = 0 TopicBean: ejbStore id = 1 TopicBean: ejbStore id = 2 Tx : createTopic Same code: 6 lifecycle calls
Take Advantage of Your EJB Container Configuration Specifies how long to keep stateless sessions cached in the pool.Stateless Session cache- timeout Specifies whether the container updates only modified fields or all fields to when ejbStore is invoked. Default true. CMP update- changedfield -only Multiple users can execute the entity bean in parallel. The container does not allow any updates to the bean's state. CMP read-only Recommend setting to false to avoid the extra select before insert which checks if the entity already exists before doing the insert. This will then detect a duplicate, if there is one, during the insert. CMP do-select- beforeinsert max-tx- retries Specifies the maximum time to wait for any resource that the EJB container needs before the container calls the EJB method (excluding DB). Session & Entity call- timeout Performance Characteristic ImpactedTypeExample Parameter Session & Entity Specifies the number of times to re-try a transaction that was rolled back due to system level failures.
Tuning Servlet Performance: Load on Startup Increases application start-up time but decreases first-request latency for servlets How? – Add sub-element in http-website.xml to load the entire web module on startup – Add sub-element to the element in web.xml to load the servlet on startup
Tuning JSP Performance: Pre-Translation Pre-compile JSPs into.class files ahead of time In Oracle Application Server, ojspc provides this functionality – jsp, and.java – Batch compilation of war, jar, ear and zip files % ojspc -dir /myapp/mybindir -srcdir /myapp/mysrcdir MyPage.sqljsp MyPage2.jsp % ojspc -deleteSource myapp.war
Use HTTPSession Appropriately Minimize the objects you store in HTTPSession – Takes up memory – Expensive serialization/deserialization if you use persistence/replication – Use transient variables to reduce serialization overhead Reduce default session timeout by using HttpSession.setMaxInactiveInterval() Remove session objects when no longer in use Use in JSP pages where you do not need a session
Look for Bottlenecks with Load Testing Tools Mercury Loadrunner Open source – Apache JMeter – Grinder Altaworks Panorama …
Threads – more arent necessarily better! The optimum number of threads required will probably vary based on application makeup and load Reduction of thread contention is key – Iterative process Dont get discouraged!
Objects – be economical! Object creation is expensive – Especially exceptions Assess whether unnecessary object creation is occurring
JVM Tuning A number of hotspot VM options are available for tuning – -Xms, -Xmx, -Xmn, -XX:SuvivorRatio,… Some platform vendors provide additional options – HP: -XX:+ForceMmapReserved Some platforms are not properly tuned out of the box for Java processing