JBoss AS 5 Performance Tuning - Api.pageplace.de

Transcription

JBoss AS 5 PerformanceTuningBuild faster, more efficient enterprise Java applicationsFrancesco MarchioniBIRMINGHAM - MUMBAI

JBoss AS 5 Performance TuningCopyright 2010 Packt PublishingAll rights reserved. No part of this book may be reproduced, stored in a retrievalsystem, or transmitted in any form or by any means, without the prior writtenpermission of the publisher, except in the case of brief quotations embedded incritical articles or reviews.Every effort has been made in the preparation of this book to ensure the accuracyof the information presented. However, the information contained in this bookis sold without warranty, either express or implied. Neither the author nor PacktPublishing, and its dealers and distributors will be held liable for any damagescaused or alleged to be caused directly or indirectly by this book.Packt Publishing has endeavored to provide trademark information about all of thecompanies and products mentioned in this book by the appropriate use of capitals.However, Packt Publishing cannot guarantee the accuracy of this information.First published: December 2010Production Reference: 1291110Published by Packt Publishing Ltd.32 Lincoln RoadOltonBirmingham, B27 6PA, UK.ISBN 978-1-849514-02-6www.packtpub.comCover Image by Duraid Fatouhi (duraidfatouhi@yahoo.com)

CreditsAuthorFrancesco MarchioniReviewersDevon HillardKushal PaudyalAcquisition EditorSarah CullingtonDevelopment EditorMeeta RajaniTechnical EditorsSakina KaydawalaKartikey PandeyManasi PoonthottamIndexersHemangini BariRekha NairEditorial Team LeaderMithun SehgalProject Team LeaderPriya MukherjiProject CoordinatorShubhanjan ChatterjeeProofreaderKevin McGowanGraphicsNilesh MohiteProduction CoordinatorAdline Swetha JesuthasCover WorkAdline Swetha Jesuthas

About the AuthorFrancesco Marchioni is a Sun Certified Enterprise Architect employed by anItalian company based in Rome. He started learning Java in 1997 and since then hehas followed the path to the newest Application Program Interfaces released by Sun.Since 2000, he has joined the JBoss Community when the application server wasrunning release 2.X.He has spent many years as a software consultant, where he has created manysuccessful software migrations from vendor platforms to the open source productslike JBoss AS, fulfilling the tight budget requirements of current times.In the past five years he has started authoring technical articles for O'Reilly Media andrunning an IT portal focused on JBoss products (http://www.mastertheboss.com).In December 2009, he published the title, JBoss AS 5 Development, which describeshow to create and deploy Java Enterprise applications on JBoss t/book.I'd like to thank Packt Publishing for offering another opportunityto write about my favorite application server. I'd also like to thankmy family for supporting me during the creation of this book, inparticular my father for teaching me all the tricks of the gloriousC-64 when I was a kid.Also a special thanks to the people that collaborated to this book: myfriend Luca Viola who lent me the hardware for these benchmarks.JBoss guys Carlo de Wolf and Alessio Soldano for their preciousadvices. And last but not least the technical reviewers - DevonHillard and Kushal Paudyal—and all the Packt Pub staff for theircompetent cooperation.

About the ReviewersDevon Hillard is a J2EE Architect with over 12 years of experience working onsome of the largest e-commerce sites in the world including AT&T, Target, JCrew,and many more. Working with large-scale enterprise environments has led to anatural focus on performance and security.Devon created http://10MinuteMail.com, a popular temporary e-mail service,which is one of the top 10,000 sites in the world (based on http://www.alexa.com/ ranking). Devon is also a founding partner at Spark::red, a premier hostingcompany for Fortune 500 clients using ATG e-commerce software, and often consultsin the e-commerce, performance, and security arenas. He has also reviewed severaltechnical book manuscripts on topics such as JBoss, GWT, and JBoss Seam.Kushal Paudyal has been a professional Software Engineer for over five yearson the Java/J2EE platform. He currently works as a Technical Lead for one of thebiggest companies in USA that provides warranty management software. Apartfrom design, development, testing, and global delivery of the software, Kushalhas extensive experience of leading a team of support analysts in supporting theenterprise applications deployed to top notch automotive industries in USA andoptimizing the performance of production applications deployed to JBoss andWebSphere application servers.Kushal's domain of expertise is warranty, fleet, and service lifecycle management.He holds a masters degree in Computer Science and a bachelor's degree in ComputerEngineering.Apart from work, Kushal is a hobbyist photographer. He can be reached atkushalzone@gmail.com.

www.PacktPub.comSupport files, eBooks, discount offersand moreYou might want to visit www.PacktPub.com for support files and downloads relatedto your book.Did you know that Packt offers eBook versions of every book published, with PDFand ePub files available? You can upgrade to the eBook version at www.PacktPub.com and as a print book customer, you are entitled to a discount on the eBook copy.Get in touch with us at service@packtpub.com for more details.At www.PacktPub.com, you can also read a collection of free technical articles, signup for a range of free newsletters and receive exclusive discounts and offers on Packtbooks and eBooks.http://PacktLib.PacktPub.comDo you need instant solutions to your IT questions? PacktLib is Packt's onlinedigital book library. Here, you can access, read and search across Packt's entirelibrary of books.

Why Subscribe? Fully searchable across every book published by Packt Copy & paste, print and bookmark content On demand and accessible via web browserFree Access for Packt account holdersIf you have an account with Packt at www.PacktPub.com, you can use this to accessPacktLib today and view nine entirely free books. Simply use your login credentialsfor immediate access.

"Quod non fecerunt barbari, Barberini fecerunt. Quod non fecerunt Barberini,fecit Berlusconi."We're living in Italy the last days of Pompeii: defend our cultural heritagebefore it's too late!

Table of ContentsPrefaceChapter 1: Performance Tuning Concepts17PrefaceWhat you will get from this book?What is performance?Scalability: the other side of performanceThe tuning processTuning in the software development cycleBuilding the performance test78910121315Tuning Java Enterprise applicationsAreas of tuningSummary181922Establish a baselineCollect dataAnalyze dataConfigure and test againChapter 2: Installing the Tools for TuningWelcome to scientific tuningThe rationale behind the choice of the toolsProfiling the Java Virtual MachineInstalling the JVMInstalling VisualVMConnecting to a local serverConnecting to a remote serverMonitoring your serverThe Monitor tabThe Threads tabThe Profiler tab15161718232324252527282830313233

Table of ContentsCollecting snapshotsExtending VisualVMProfiling your applications with Eclipse Test andPerformance Tools Platform (TPTP) ProjectInstalling the TPTP suiteUpdating EclipseHow to profile an application343637383839Going beyond the basics of TPTP42Load testing your application with JMeterBuilding a Test Plan4344How to create a complex Test PlanRunning JMeter as a shellOperating system tools and commandsWindows usersUnix users4851525254Summary59Step 1: Create a Thread GroupStep 2: Create a SamplerStep 3: Create a ListenerMaking your test plan a bit more realisticDealing with low CPU utilizationDealing with high CPU utilizationDealing with high resource contentionDealing with high disk utilization4445464756575858Chapter 3: Tuning the Java Virtual Machine61The correct amount of memory to grant your applicationI cannot allocate enough memory for the JVM!Where do I configure JVM settings in JBoss AS?666971The starting pointChoosing the JVM settingsSetting the correct heap sizeSizing the garbage collector generationsWhich is the correct ratio between the young and old generations?The garbage collector algorithmsChoosing the right garbage collector for your applicationThe G1 garbage collectorDebugging garbage collectionMaking good use of the memory62646571727376777880Avoid creating large Java objectsHandling 'Out of Memory' errorsFinding the memory leak in your code808283[ ii ]

Table of ContentsA practical exampleApplication descriptionSetting up a test bed868788Summary93Benchmark aftermath89Chapter 4: Tuning the JBoss ASFrom release 4.x to 5.x, and onComparing server releasesCreating a custom application server configurationJBoss AS poolsThe System thread poolFinding out if the System thread pool is a bottleneckJDBC connection poolCalculating the optimal min-pool-size and max-pool-sizeUsing Prepared Statements efficientlyDetecting connection leaksShould you reuse connections or acquire new onesfrom the pool?95959698101102104105106108111114Logging for performanceChoosing the best logging strategy114115Summary121Which is the fastest appender?Should I use the AsyncAppender to improve my log throughput?Which layout should I choose for my logs?Is it enough to increase the log threshold to get rid of log charge?How does logging hierarchy influence performance?Chapter 5: Tuning the Middleware ServicesIntroduction to Enterprise Java BeansSession BeansHow to configure the optimal size for stateless pool?How to configure the optimal size of stateful cache?Comparing SLSBs and SFSBs performanceSession Beans and troduction to the Java Messaging systemEntering the JBoss Messaging systemTuning JBoss JMS provider137139140An example use case with HornetQSummary150154Tuning JBoss Messaging (JBoss AS 5.x)Tuning HornetQ (JBoss AS 6.x)Basic JMS tuning[ iii ]141144148

Table of ContentsChapter 6: Tuning the Persistence Layer157Limiting the amount of data fetched with pagesFetching parent and child items with a single querySpeeding up your Hibernate queries with batchesUsing named queries to speed up your queries170171172173Designing a good databaseReducing the space needed by your databasePartitioning the databaseUsing indexesTuning JDBCUsing a Connection PoolSetting the proper fetch sizeUse batch updates for bulk insert/updatesUse Prepared Statements instead of StatementsTuning JDBC networkingTuning Hibernate and JPAOptimizing object retrievalImproving the performance of bulk SQL statementsEvaluating using caches to speed up your queriesHibernate cachesThe first-level cacheThe second-level cacheConfiguring the second-level cache on JBoss ASThe query cacheEntity cache versus query 177178178180181Optimizing data synchronizationA sample use caseSummaryChapter 7: JBoss AS Cluster TuningIntroduction to JBoss clusteringConfiguring JGroups transportHow to optimize the UDP transport configurationHow to optimize the JGroups' Protocol stackTuning JBoss CacheUnderstanding JBoss Cache configurationConfiguring cacheModeConfiguring cache concurrencyTuning session replicationOverride SFSB's isModified methodUse buddy replicationAdvanced buddy replicationBuddy replication and session affinity[ iv 18

Table of ContentsConfigure replication granularity and replication trigger218Tuning cache storage223How to configure the replication-triggerHow to configure the replication-granularityWhere does cache data get stored?219220224SummaryChapter 8: Tomcat Web Server TuningJBoss Web server basicsConfiguring Connectors for optimal performanceHow do you calculate the threads for your application?The long life of Web threadsIs there a maximum amount of connections the Web server can handle?227229230231232236238Using Apache Portable RuntimeIntegrating JBoss Web server with Apache Web server240241mod cluster to the rescue?Last tips before rolling JBoss Web server in productionSummary248249250Load testing Apache-JBoss connectivityChapter 9: Tuning Web Applications on JBoss ASChoosing a framework for your web applicationsComparing framework performanceThe performance of tabular dataThe performance of rich treeIncreasing the performance of JSF and RichFacesConfiguring JSF state saving efficientlyUsing Ajax to speed up your JSF applicationsSpeeding up CSS and JavaScript file loading243253254255257259261261263265Tuning web servicesPerformance of web services266268Summary277Elements influencing the performance of web servicesReducing the size of SOAP messagesFaster JBossWS providerAppendix: A Tuned MindIndex270271274281283[v]

PrefaceJBoss AS 5 Performance Tuning will teach you how to deliver fast applications onthe JBoss Application Server and Apache Tomcat, giving you a decisive competitiveadvantage over your competitors. You will learn how to optimize hardwareresources, meeting your application requirements with less expenditure.The performance of Java Enterprise applications is the sum of a set of componentsincluding the Java Virtual Machine configuration, the application serverconfiguration (in our case, JBoss AS), the application code itself, and ultimatelythe operating system. This book will show you how to apply the correct tuningmethodology and use the tuning tools that will help you to monitor and addressany performance issues.By looking more closely at the Java Virtual Machine, you will get a deeperunderstanding of what the available options are for your applications, and howtheir performance will be affected. Learn about thread pool tuning, EJB tuning,and JMS tuning, which are crucial parts of enterprise applications.The persistence layer and the JBoss Clustering service are two of the most crucialelements which need to be configured correctly in order to run a fast application.These aspects are covered in detail with a chapter dedicated to each of them.Finally, Web server tuning is the last (but not least) topic covered, which shows howto configure and develop web applications that get the most out of the embeddedTomcat web server.

PrefaceWhat this book coversChapter 1, Performance Tuning Concepts, discusses correct tuning methodologyand how it fits in the overall software development cycle.Chapter 2, Installing the Tools for Tuning, shows how to install and configure theinstruments for tuning, including VisualVM, JMeter, Eclipse TPTP Platform,and basic OS tools.Chapter 3, Tuning the Java Virtual Machine, provides an in-depth analysis of theJVM heap and garbage collector parameters, which are used to start up theapplication server.Chapter 4, Tuning the JBoss AS, discusses the application server's core servicesincluding the JBoss System Thread Pool, the Connection Pool, and theLogging Service.Chapter 5, Tuning the Middleware Services, covers the tuning of middleware servicesincluding the EJB and JMS services.Chapter 6, Tuning the Persistence Layer, introduces the principles of good databasedesign and the core concepts of Java Persistence API with special focus on JBoss'simplementation (Hibernate).Chapter 7, JBoss AS Cluster Tuning, covers JBoss Clustering service covering thelow-level details of server communication and how to use JBoss Cache foroptimal data replication and caching.Chapter 8, Tomcat Web Server Tuning, covers the JBoss Web server performancetuning including mod jk, mod proxy, and mod cluster modules.Chapter 9, Tuning Web Applications on JBoss AS, discusses developing fast webapplications using JSF API and JBoss richfaces libraries.What you need for this bookA technical prerequisite for this book is knowledge of and expertise with the JavaEnterprise API and the basics of the JBoss application server. Besides the technicalskills, which are necessary to grasp the tuning concepts, no other requirements arenecessary. As a matter of fact, all the software discussed in this book can be freelydownloaded and used both in development and in production systems.[2]

PrefaceWho this book is forThis book is for Java architects who design and configure Enterprise applications. Itis great for Java developers who want to get into the inner details of the applicationserver and of the correct tuning methodology. Application testers will also find thisbook useful as they will learn how to monitor the performance of the middlewarewith the correct instruments.ConventionsIn this book, you will find a number of styles of text that distinguish betweendifferent kinds of information. Here are some examples of these styles, and anexplanation of their meaning.Code words in text are shown as follows: "If you then request the ordered itemsthrough the customer.getItems() method, this would also fill the collection ofitems ordered with another database hit."A block of code is set as follows:@NamedQueries({@NamedQuery(name "listCustomers",query "FROM Customer c WHERE c.name :name")})public class Customer implements Serializable{}When we wish to draw your attention to a particular part of a code block, therelevant lines or items are set in bold:Session session ssion.beginTransaction();for (int index 0; index 1000;index ) {Person person new ;person.setName("Inhabitant n." index);session.save(person);// Flush every 50 records[3]

Prefaceif (index % 50 0) saction().commit();session.close();Any command-line input or output is written as follows:export JAVA HOME /installDir/jdk1.6.0 20New terms and important words are shown in bold. Words that you see on thescreen, in menus or dialog boxes for example, appear in the text like this: "Then pickup the method you want to test and select Configure which automatically configuresyour web service properties".Warnings or important notes appear in a box like this.Tips and tricks appear like this.Reader feedbackFeedback from our readers is always welcome. Let us know what you think aboutthis book—what you liked or may have disliked. Reader feedback is important forus to develop titles that you really get the most out of.To send us general feedback, simply send an e-mail to feedback@packtpub.com,and mention the book title via the subject of your message.If there is a book that you need and would like to see us publish, please sendus a note in the SUGGEST A TITLE form on www.packtpub.com or e-mailsuggest@packtpub.com.If there is a topic that you have expertise in and you are interested in either writingor contributing to a book, see our author guide on www.packtpub.com/authors.[4]

PrefaceCustomer supportNow that you are the proud owner of a Packt book, we have a number of things tohelp you to get the most from your purchase.Downloading the example code for this bookYou can download the example code files for all Packt books you havepurchased from your account at http://www.PacktPub.com. If youpurchased this book elsewhere, you can visit http://www.PacktPub.com/support and register to have the files e-mailed directly to you.ErrataAlthough we have taken every care to ensure the accuracy of our content, mistakesdo happen. If you find a mistake in one of our books—maybe a mistake in the text orthe code—we would be grateful if you would report this to us. By doing so, you cansave other readers from frustration and help us improve subsequent versions of thisbook. If you find any errata, please report them by visiting http://www.packtpub.com/support, selecting your book, clicking on the errata submission form link, andentering the details of your errata. Once your errata are verified, your submissionwill be accepted and the errata will be uploaded on our website, or added to any listof existing errata, under the Errata section of that title. Any existing errata can beviewed by selecting your title from http://www.packtpub.com/support.PiracyPiracy of copyright material on the Internet is an ongoing problem across all media.At Packt, we take the protection of our copyright and licenses very seriously. If youcome across any illegal copies of our works, in any form, on the Internet, pleaseprovide us with the location address or website name immediately so that we canpursue a remedy.Please contact us at copyright@packtpub.com with a link to the suspectedpirated material.We appreciate your help in protecting our authors, and our ability to bring youvaluable content.[5]

PrefaceQuestionsYou can contact us at questions@packtpub.com if you are having a problem withany aspect of the book, and we will do our best to address it.[6]

Performance TuningConcepts"All slow application abandon, ye who enter here." (Freely adapted from Dante'sDivine Comedy Poem—http://en.wikipedia.org/wiki/Divine Comedy)PrefaceOne day like many, on a JBoss AS Forum:"HiI am running the Acme project using JBoss 5.1.0. My requirement is to allow 1000concurrent users to access the application. But when I try to access the applicationwith 250 users, the server slows down and finally throws an exception "Could notestablish a connection with the database. Does anyone have an idea please help meto solve my problem."In the beginning, performance was not a concern for software. Early programminglanguages like C or Cobol were doing a decent job of developing applications andthe end user was just discovering the wonders of information technology that wouldallow him to save a lot of time.Today we are all aware of the rapidly changing business environment in whichwe work and live and the impact it has on business and information technology.We recognize that an organization needs to deliver faster services to a larger set ofpeople and companies, and that downtime or poor responses of those services willhave a significant impact on the business.

Performance Tuning ConceptsTo survive and thrive in such an environment, organizations must consider it animperative task for their businesses to deliver applications faster than their competitorsor they will risk losing potential revenue and reputation among customers.So tuning an application in today's market is firstly a necessity for survival, but, thereare even more subtle reasons, like using your system resources more efficiently. Forexample, if you manage to meet your system requirements with fewer fixed costs(let's say by using eight CPU machine instead of a 16 one) you are actually usingyour resources more efficiently and thus saving money. As an additional benefityou can also reduce some variable costs like the price of software licenses, which areusually calculated on the amount of CPUs used.On the basis of these premises, it's time to reconsider the role of performance tuningin your software development cycle, and that's what this book aims to do.What you will get from this book?This book is an exhaustive guide to improving the performance of your Java EEapplications running on JBoss AS and on the embedded web container (JakartaTomcat). All the guidelines and best practices contained in this book have beenpatiently collected through years of experience from the trenches and from thesuggestions of valuable people, and ultimately in a myriad of blogs, and each onehas contributed to improve the quality of this book.The performance of an application running on the application server is the result of acomplex interaction of many aspects. Like a puzzle, each piece contributes ultimatelyto define the performance of the final product. So our challenge will be to teach howto write fast applications on JBoss AS, but also how to tune all the components andhardware which are a part of the IT system. As we suppose that our prime readerwill not be interested in learning the basics of the application server, nor how toget started with Java EE, we will go straight to the heart of each component andelaborate on the strategies to improve their performance.Should you be interested in learning more about the application server itself youcan refer to the JBoss community at http://community.jboss.org/ or you canhave a look at my previous book: k.[8]

Chapter 1What is performance?The term "performance" commonly refers to how quickly an application can beexecuted. In terms of the user's perspective on performance, the definition is quiteeasy to grasp. For example, a fast website means one that is able to load webpages very quickly. From an administrator's point of view, the concept needs to betranslated into meaningful numbers. As a matter of fact, the expert can distinguishtwo ways to measure the performance of an application: Response Time ThroughputThe Response Time can be defined as the time it takes for one user to perform atask. For example, on a website, after the customer submits one e-commerce form,the time it takes to process the order and for rendering and displaying the result ina new page is the response time for this functionality. As you can see, the conceptof performance is essentially the same as from the end user perspective, but it istranslated into numbers.In practice, as shown in the following image, the Response Time includes thenetwork roundtrip to the application server, the time to execute the businesslogic in your middleware (including the time to contact external legacy systems)and the latency to return the response to the client.At this point the concept of Response Time should be quite clear, but you mightwonder if this measurement is a constant; actually it is not. The Response Timechanges accordingly with the load on the application. A single operation cannot beindicative of the overall performance: you have to consider how long the proceduretakes to be executed in a production environment, where you have a considerableamount of customers running.[9]

Performance Tuning ConceptsAnother performance-related counter is Throughput. Throughput is the numberof transactions that can occur in a given amount of time. This is a fundamentalparameter that is used to evaluate not only the performance of a website, butalso the commercial value of a software. The Throughput is usually measured inTransactions Per Second (TPS) and obviously an application that has a TPS higherthan its competitors is also the one with higher commercial value—all other featuresstanding equal.The following image, depicts a Throughput comparison between a LinuxServer and a Windows Server, as part of a complete benchmark windows vs linux part1/index.html):Scalability: the other side of performanceAs we have just learnt, we cannot define performance within the context of a singleuser who is testing the application. The performance of an application is tightlycoupled with the number of users, so we need to define another variable which isknown as Scalability. Scalability refers to the capability of a system to increase totalThroughput under an increased load when resources are added. It can be seen fromtwo different perspectives:[ 10 ]

Chapter 1 Vertical scalability: (otherwise known as scaling up) means to add morehardware resources to the same machine, generally by adding moreprocessors and memory. Horizontal scalability: (otherwise known as scaling out) means to add moremachines into the mix, generally cheap commodity hardware.The following image is a synthetic representation of the two different perspectives:Both solutions have pros and cons: generally vertical scaling requires a greaterhardware expenditure because it needs upgrading to powerful enterprise servers, butit's easier to implement as it requires fewer changes in your configuration.Horizontal scaling on the other hand, requires little investment on cheaper hardware(which has a linear expenditure) but it introduces a more complex programming model,thus it needs an expert hand as it concerns configuration and might require somechanges in your application too.You should also consider that concentrating all yourresources on a single machine introduces a single point offailure, which is the case if you choose an extreme type ofvertical scaling.[ 11 ]

Performance Tuning ConceptsThe tuning processAt this point you will have grasped that performance tuning spans over severalcomponents, including the application delivered and the environment where it isrunning. However, we haven't addressed which is the right moment for starting totune your applications. This is one of the most underestimated issues in softwaredevelopment and it is commonly solved by applying tuning only at two stages: While coding your classes At the end of software developmentTuning your applications as you code is a consolidated habit of software developers,at first because it's funny and satisfying to optimize your code and see an immediateimprovement in the performance of a single function. However, the other side ofthe coin is that most of these optimizations are useless. Why? It is statistically proventhat within one application only 10-15 % of the code is executed frequently, so tryingto optimize code blindly at this stage will produce little or no benefit at all to yourapplication.The second favorite anti-pattern adopted by developers is starting the tuningprocess just at the end of the software development cycle. For good reason, this canbe considered a bad habit. Firstly, your tuning session will be more complex andlonger: you have to analyze again the whole application roundtrip while hunting forbottlenecks again. Supposing you are able to isolate the cause of the bottleneck, youstill might be forced to modify critical sections of your code, which, at this stage, canturn easily into a nightmare.Think, for example, of an application which uses a set of JSF components to rendertrees and tables. If you discover that your JSF library runs like a crawl when dealingwi

optimizing the performance of production applications deployed to JBoss and WebSphere application servers. Kushal's domain of expertise is warranty, fleet, and service lifecycle management.