Transcription

Java EE 7 PerformanceTuning and OptimizationBoost the efficiency of your enterprise applicationsby performance tuning and optimization with JavaOsama OransaBIRMINGHAM - MUMBAI

Java EE 7 Performance Tuning and OptimizationCopyright 2014 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 book issold 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: June 2014Production reference: 1160614Published by Packt Publishing Ltd.Livery Place35 Livery StreetBirmingham B3 2PB, UK.ISBN 978-1-78217-642-8www.packtpub.comCover image by Sean FitzGerald ([email protected])

CreditsAuthorOsama OransaReviewersProject CoordinatorPriyanka GoelProofreadersE.P. RamakrishnanSimran BhogalChirag SanganiMaria GouldDeepak VohraAmeesha GreenJeff ZhangPaul HindleCommissioning EditorMary Jasmine NadarIndexersMehreen DeshmukhRekha NairAcquisition EditorNikhil KarkalContent Development EditorDayan HyamesTejal SoniPriya SubramaniGraphicsSheetal AuteRonak DhruvTechnical EditorsNovina KewalramaniHumera ShaikhCopy EditorsDisha HariaAbhinash SahuProduction CoordinatorManu JosephAlisha AranhaRoshni BanerjeeAdithi ShettyCover WorkManu Joseph

About the AuthorOsama Oransa is an IT solution architect with more than 12 years of solid technicalexperience in Java EE. He is a certified Java enterprise architect and an SME in webservices technology. He has worked for most of the key players in the IT industry,such as IBM, Oracle, and Hewlett Packard. He previously worked as a performanceconsultant at DevFactory, and he is currently working with the Vodafone Group asa solution architect. He has also participated in establishing Pulse Corp as a medicalsoftware services company in Egypt.He has a diploma in IT from the Information Technology Institute (ITI) and adiploma in CS from the Arab Academy for Science, Technology and MaritimeTransport (AASTM). He is currently working towards a Master's degree in CS.Being from Cairo, he is a frequent speaker at the Java Developer Conference (JDC)in Cairo.In 2010, one of his projects in Pulse Corp, "Health Intact", won Oracle Duke'sChoice Award. He is the founder of more than 12 open source projects hostedon SourceForge. He has also been selected by Oracle for the future of the Javacampaign for his valuable contribution to the industry.He is a volunteer Java technology evangelist who gives technical sessions atdifferent companies, conferences, and on blogs. His technical blog can be foundat http://osama-oransa.blogspot.com/.

AcknowledgmentsAfter more than 17 years since my first IT book on the Assembly language, I gotthe chance to write my second book. It was really a nice experience to deal withthe wonderful people at Packt Publishing. After thanking my God, I would liketo thank my parents and my wife for their full support while writing this book.Special thanks to Reshma Raman, the Author Acquisition Executive who contactedme to write this book.I would like to thank Priyanka Goel, the Project Coordinator who helped me todeliver the book following the agreed timeline and who gave me her full support.I would like to thank Nikhil Karkal, the Acquisition Editor, for his support in theearly, critical phases of the book and his continuous efforts in providing me withthe proper guidelines.I would like to thank Dayan Hyames, the Lead Technical Editor, for his efforts inreviewing the content of this book and guiding me to provide the best outcome.I would like to thank the technical reviewers E.P. Ramakrishnan, Chirag Sangani,Deepak Vohra, and Jeff Zhang, who caught my errors and provided me with veryconstructive and valuable comments to enhance the content of this book.I would like to thank the technical editors Novina Kewalramani and Humera Shaikhfor their tremendous efforts in the book.Finally, thanks goes to all the people who participated in this book who I don't knowby name—without you, this book wouldn't see the light. Thank you!

About the ReviewersE.P. Ramakrishnan is an enthusiastic developer and a technical writer. Heearned his postgraduate degree in Computer Science and Engineering from AnnaUniversity, Tamil Nadu, India. He has a steady industry exposure of 6 years. Hisareas of expertise include Java Server Faces (JSF), the Java Persistence API, CDI,RESTful Services, Swings, Tomcat Clustering, and load balancing. Also, his areas ofinterest are Linux, Android, and systems security. Besides development, his majorinterest lies in writing technical blogs which simplify the latest technologies forbeginners. You are welcome to visit his blog at http://www.ramkitech.com andgive your feedback. He can be reached at [email protected] Sangani is a computer scientist living in the Seattle area. He holds anMS degree in Computer Science from Stanford University and a B.Tech degree inComputer Science and Engineering from the Indian Institute of Technology, Kanpur.An exposure to computers since his early childhood has allowed Chirag to explorehis varied interests while witnessing the birth of the Internet. He has dedicated thelast 10 years to perfecting his skills while simultaneously diving deep into computerscience theory.He has had varied interests and experiences—from computer architecture tonetworking, distributed systems to machine learning and data mining, complexitytheory to cryptography, and mobile and web development to 3D game design.He has found that merging different fields of computer science allows for aholistic approach towards solving any problem. He currently works for Microsoft.

Deepak Vohra is a consultant and a principal member of the NuBean.comsoftware company. He is a Sun certified Java programmer and web componentdeveloper, and he has worked in the fields of XML, Java programming, and Java EEfor over 5 years. Deepak is the co-author of the Apress book Pro XML Developmentwith Java Technology, and he is the technical reviewer for the O'Reilly book WebLogic:The Definitive Guide. He is also the technical reviewer for the Course Technology PTRbook Ruby Programming for the Absolute Beginner. He is also the author of the PacktPublishing books JDBC 4.0 and Oracle JDeveloper for J2EE Development, Processing XMLDocuments with Oracle JDeveloper 11g, EJB 3.0 Database Persistence with Oracle FusionMiddleware 11g, and Java EE Development with Eclipse IDE.Jeff Zhang started working with middleware software in 2002. He joined IONAand worked on Corba and web service products. In 2008, Jeff entered Redhat andbecame core developer of the JBossAS team.After more than 10 years of working on middleware, Jeff has learned a lot aboutapplication servers, PAAS, containers, and services. He believes that mobiletechnology and the Internet will change people's lives.I would like to thank the author who has written a great book.Thanks goes to Priyanka Goel and Aurita D'souza for their helpin the reviewing stage.Thanks also goes to my wife and my son—your support ismost important.

www.PacktPub.comSupport files, eBooks, discount offers, and moreYou might want to visit www.PacktPub.com for support files and downloads related toyour book.Did you know that Packt offers eBook versions of every book published, with PDF and ePubfiles available? You can upgrade to the eBook version at www.PacktPub.com and as a printbook customer, you are entitled to a discount on the eBook copy. Get in touch with us [email protected] for more details.At www.PacktPub.com, you can also read a collection of free technical articles, sign up for arange of free newsletters and receive exclusive discounts and offers on Packt books and eBooks.TMhttp://PacktLib.PacktPub.comDo you need instant solutions to your IT questions? PacktLib is Packt's online digital booklibrary. Here, you can access, read and search across Packt's entire library of books.Why subscribe? Fully searchable across every book published by Packt Copy and 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 credentials forimmediate access.

To my dear parents, the owners of all my life achievements.To my lovely wife, Rasha, the angel of my life.To my diamond daughters—the best gifts in my life—Judy and Kenzy.–Osama Oransa

Table of ContentsPrefaceChapter 1: Getting Started with Performance TuningUnderstanding the art of performance tuningUnderstanding performance issuesClassifying performance issues by the discovery phaseRequirement phase and design-time issuesDevelopment-time issuesTesting-time issuesProduction-time issues1910121314151616Classifying performance issues by the root phase17Performance-handling tacticsProactive measures (preventive)Reactive measures (curative)191921The cycle of learningTuning yourself before tuning the application2426Requirement phase issuesDesign/architecture phase issuesDevelopment phase issuesTesting phase issuesOperational and environmental-specific issuesUnderstanding the different layers of an enterprise applicationThe three pillars required for performance tuningBe a true leaderUse your powerBe responsibleTrust your teamKeep it simpleRespect roles and responsibilitiesUnderstand the application domain and contextProtect your reputation181818181921222626262727272728

Table of ContentsStandalone applications versus web applicationsThe standalone applicationThick client application – client-server modelThin client application – web-based modelDealing with web applications' performance tuningThe two dimensions of web applications' performance tuningHorizontal dimension (node-to-node)Vertical dimension (intranode)Exploring vertical dimension nodes in horizontal dimension nodesClient sideNetwork componentsHTTP servers (web servers)Application serversDatabase serversMiddleware integration serversOperating system and hardwareCPU utilizationNetwork trafficMemory usageStorage I/O performanceSummaryChapter 2: Understanding Java FundamentalsDiscussing the new Java EE 7 featuresBean Validation 1.1 (JSR 349)Java API for JSON processing – JSON-P 1.0 (JSR 353)Java API for RESTful web services – JAX-RS 2.0 (JSR 339)Java Servlet 3.1 (JSR 340)Context and Dependency Injection – CDI 1.1 (JSR 346)Interceptors 1.2 (JSR 318)Enterprise JavaBeans – EJB 3.2 (JSR 345)Java Message Service – JMS 2.0 (JSR 343)Concurrency Utilities 1.0 (JSR 236)Batch Applications 1.0 (JSR 352)Java Persistence APIs – JPA 2.1 (JSR 338)JavaServer Faces – JSF 2.2 (JSR 344)Expression language 3.0 (JSR 341)Java Transaction APIs – JTA 1.2 (JSR 907)Java API for WebSocket 1.0 (JSR 356)Understanding memory structure in the JVMThe JVM specificationsHeap areaMethod area and runtime constant poolJVM stack[ ii 1424243444445464647474848495051525252

Table of ContentsNative method stacks (C stacks)PC registersMemory structure in the Java HotSpot virtual machineGenerational memory structureThe Java HotSpot virtual machine generations5353535354Understanding the Java garbage collection policiesDifferent GC options5656Different application scopesUnderstanding concurrency in JavaProcess versus threadExploring an object monitorUsing the Java concurrency utilitiesCreating a thread poolUsing explicit locking with the Lock interfaceConcurrent resources and utilities6263646566676871The important Java EE concurrency featuresThe SingleThreadModel interfaceAsynchronous servlet and filterThe new Java EE non-blocking I/O APIsSession beans asynchronous method invocationA singleton session beanSending asynchronous JMS messagesMore information about Java EE 7Summary737374747576777879Concurrent versus stop-the-worldSerial versus parallel collectorCompacting versus non-compactingThe Garbage-first collector – G1The ManagedExecutorService classThe ManagedScheduledExecutorService classThe ManagedThreadFactory classChapter 3: Getting Familiar with Performance TestingDissecting performance testingExploring performance testing aspectsSelecting the performance test environmentProject milestones and performanceDefining different rules and responsibilitiesPerformance testing typesPerformance testing componentsPerformance testing toolsPerformance benchmarking and baselineIsolation testingPerformance fixing cycleWhen to stop tuning?[ iii ]5657596071727281818283848485858889899090

Table of ContentsPerformance testing terminologiesPerformance testing in a cloud environmentStarting with Apache JMeterDifferent components of the JMeter test plan91929495Testing with JMeterUsing JMeter to test web services9798The execution order of componentsCreating a thread groupCreating the SOAP samplerCreating listeners97102102103Using JMeter to test a web application107Using JMeter to test database scripts114Recording our testing scenariosConfiguring the JDBC connectionAdding a JDBC request samplerAdding a CSV dataset configurationAdding listeners to capture test resultsSummaryChapter 4: Monitoring Java ApplicationsExploring the Java monitoring toolsThe operating system monitoring toolsThe Microsoft Windows toolsThe Unix/Linux toolsThe Java monitoring toolsThe JDK monitoring toolsThe monitoring tools for application serversThe IDE monitoring toolsThe standalone monitoring toolsThe multifunction monitoring toolsUnderstanding the profiling toolsProfilers modesJVM TIProfiler agentThe command-line optionsAgent 127127127127128128129129129Different profiling patterns129What we need to know from this sectionUnderstanding the different JDK toolsThe monitoring tool for Java virtual machine statisticsThe JVM memory map toolThe Java heap analysis toolThe Java monitoring and management console toolJava VisualVM[ iv ]130130131133135137138

Table of ContentsOracle Java Mission ControlStarting with the NetBeans profilerThe NetBeans profiler calibrationUsing the NetBeans profilerThe Eclipse tools/pluginsThe JVM monitorThe Test and Performance Tools PlatformAdvanced profiler – JProfilerUsing the offline profiling modeBuilding our script using JProfiler triggersFurther readingSummaryChapter 5: Recognizing Common Performance IssuesGoing through a slow response time umbrellaIsolating the issueClient sideHTTP server side (web servers)Application server issueDatabase server issueIntegrated systemsNetworking componentsCode and script analysisProfiling the 164164165165165166166166166166Common performance issues167Threading performance issuesBlocking threads169170Thread deadlockMemory performance issuesMemory leakage172173173Threading performance issuesMemory performance issuesAlgorithmic performance issuesWork as designed performance issuesInterfacing performance issuesMiscellaneous performance issuesFake performance issues167167168168168169169Performance symptomsAn example of thread blocking170170Performance symptomsAn example of memory leakage173174Improper data cachingImproper caching issue typesPerformance symptomsAn example of improper caching techniques[v]177177179179

Table of ContentsWork as designed performance issuesSynchronous where asynchronous is requiredPerformance symptomsAn example of improper synchronous code181181181181Neglecting remoteness182Excessive serialization performance impact182Performance symptomsAn example of using remote calls as local callsPerformance symptomsAn example of excessive serialization182182183183Selected performance issuesUnnecessary application logicExcessive application loggingDatabase performance issuesMissing proactive tuningClient-side performance issuesChrome developer tools185185186187189189190Internet Explorer developer toolsFirefox developer toolsNavigating time specificationsSummary194196196197Network analysisJavaScript profilingSpeed Tracer190192193Chapter 6: CPU Time Profiling199When to use CPU profilingDifferent CPU profiling optionsUsing a NetBeans profiler199200200Profiling a Java applicationProfiling a web application201203Using Java Mission Control (JMC)Using JProfilerReading and interpreting CPU profiling resultsThe call tree viewThe HotSpots viewAnalyzing the method time versus the method invocation countThe HotSpot method types206208210211211213214Identifying a HotSpot typeIdentifying potential performance issuesAlgorithmic/logic issues215218219Methods with high self-timeMethods with high invocation eventsMethods with high self-time and invocation events[ vi ]214214215

Table of ContentsCaching issuesResourcing issuesThreading issuesFixing algorithmic/logic performanceSimple algorithmic evaluationEvaluating an algorithm's complexityOur first performance fixing strategyFixing the application logic/algorithmAdding support for cachingOptimizing the performance of resourcesImplementing concurrency or multithreadingUsing asynchronous methodsSummaryChapter 7: Thread ProfilingDetermining when to use thread profilingExploring the different thread profiling optionsThread monitoring using NetBeansThread monitoring using JProfilerThread monitoring using Java Mission ControlReading the thread profiling resultsDealing with thread dumpsTaking a thread dump using the operating system commandsUsing the keyboard shortcut Ctrl Pause BreakSending SIGQUIT to the Java processTaking a thread dump using the JDK toolsThread dump using jstackThread dump using the Java VisualVMTaking thread dumps using an application's server admin console/toolsTaking a thread dump using profiler toolsReading and analyzing the thread dumpsUnderstanding the thread dump structureAnalyzing the thread dumpsUsing Thread Dump AnalyzerExploring potential threading issuesThreading performance issuesThreading deadlockBlocked/starving/stuck threadsLow/over threadingThreading memory issuesUsing unmanaged threadsDetecting the root cause of a hung applicationDetecting the hang location using thread dumps[ vii 259259259259260260261262262

Table of ContentsDetecting the hang location using profilersEnhancing our fixing strategyFixing thread deadlocks and thread blockingSummaryChapter 8: Memory ProfilingWhen to use memory profiling?Different memory profiling optionsMemory profiling using NetBeansMemory profiling using JProfilerAnalyzing memory profiling resultsAnalyzing memory space graphsAnalyzing detailed object statisticsAnalyzing garbage collection activity logs (HotSpot JVM)Reading garbage collection activity logs (HotSpot VM)Visualizing the garbage collection activityDealing with memory heap dumpsTaking heap dumps on the occurrence of JVM OutOfMemoryErrorTaking heap dumps using the JDK toolsTaking heaps dump using jmapTaking heap dumps using Java VisualVMTaking heap dumps using the JRockit command utilityTaking heap dumps using the profiler toolsTaking heap dumps using the NetBeans profilerTaking heap dumps using Eclipse Memory Analyzer Tool (MAT)Taking heap dumps using 2285287287288288289291291291292293Analyzing the heap dumpNavigating inside a heap dump using visual toolsQuery heap dumps using OQL294294