Development Environment and Tools for Java Brian Hughes IBM 1
Acknowledgements and Disclaimers Availability. References in this presentation to IBM products, programs, or services do not imply that they will be available in all countries in which IBM operates. The workshops, sessions and materials have been prepared by IBM or the session speakers and reflect their own views. They are provided for informational purposes only, and are neither intended to, nor shall have the effect of being, legal or other guidance or advice to any participant. While efforts were made to verify the completeness and accuracy of the information contained in this presentation, it is provided AS-IS without warranty of any kind, express or implied. IBM shall not be responsible for any damages arising out of the use of, or otherwise related to, this presentation or any other materials. Nothing contained in this presentation is intended to, nor shall have the effect of, creating any warranties or representations from IBM or its suppliers or licensors, or altering the terms and conditions of the applicable license agreement governing the use of IBM software. All customer examples described are presented as illustrations of how those customers have used IBM products and the results they may have achieved. Actual environmental costs and performance characteristics may vary by customer. Nothing contained in these materials is intended to, nor shall have the effect of, stating or implying that any activities undertaken by you will result in any specific sales, revenue growth or other results. Copyright IBM Corporation 2015. All rights reserved. U.S. Government Users Restricted Rights Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp. IBM, the IBM logo, ibm.com are trademarks or registered trademarks of International Business Machines Corporation in the United States, other countries, or both. If these and other IBM trademarked terms are marked on their first occurrence in this information with a trademark symbol ( or TM), these symbols indicate U.S. registered or common law trademarks owned by IBM at the time this information was published. Such trademarks may also be registered or common law trademarks in other countries. A current list of IBM trademarks is available on the Web at Copyright and trademark information at www.ibm.com/legal/copytrade.shtml Other company, product, or service names may be trademarks or service marks of others. 2
Agenda Importance of Tooling Selection Criteria Integrated Development Environments Eclipse, IntelliJ IDEA Source Control Systems RTC, git Static Analysis/Metrics FindBugs, CodePro Analytix, EclEmma Unit Testing JUnit, TestNG Runtime Analysis IBM Java Health Center, IBM Heap Analyzer, SLF4J 3
Importance of Tooling Tooling can make order of magnitude difference in product build time and quality Tooling that works together can greatly speed up productivity Good tools encourage better processes, styles, best practice use, and streamlined development 4
Selection Criteria Tools must be mature/popular Nothing that s not backed by documentation, examples, groups and stackoverflow topics Nothing obscure that nobody has heard of and may not be around next month Tools that are actually used These are selected as they are actually used in development of software Applicable for projects both small and large You won t need million line enterprise projects to find value in the tooling shown in this session 5
Integrated Development Environments Foundation for other tooling Highly extensible IDE s allow easy integration and execution of other tools Two popular IDE s Eclipse IntelliJ IDEA Eclipse used more at IBM Many IBM tools built as extensions to Eclipse Eclipse marketplace has made finding extensions and tools much easier IntelliJ Has more analysis tools built in (analyzer, code coverage) Analysis caught different (minor) issues than other tools 6
Source Control Systems From small personal tooling to large million line projects, storing work in source control systems is essential Peace of mind work is backed up Small/pet projects can suddenly become products or important components of your dev process People come and go, where is their work saved? 7
Source Control Systems (cont) GIT and RTC are commonly used SCM s GIT used for open source projects Strong support for non-linear development Easy to do distributed development RTC used for internal projects New projects, products, tooling Both have strong Eclipse extensibility support Covers up some complexity of GIT Makes both SCM s easy to use Branching, merging, history all integrated into the IDE 8
Static Analysis/Metrics Static analysis is the quickest way (after unit testing and code reviews) to find problems in code of all sizes Built into IDE s it s made easy to run Running static analysis can be made and easy addition to any development process Boxed into IDE s Plugin to Jenkins or other build management systems 9
FindBugs Found in Eclipse Marketplace http://findbugs.sourceforge.net/ Performs static code analysis on Java ByteCode, not source code Categorizes errors into 4 ranks and 2 levels of confidence 10
FindBugs (cont) 11
EclEmma Code coverage tool for Eclipse Based on/uses JaCoCo which is a code coverage tool for Java Found in the Eclipse marketplace Has updated support for Java 8 Lambda expression support Has coverage counters for: Instructions Branches Lines (with debug information) 12
EclEmma 13
CodePro AnalytiX Suite of Static analysis tools and test case generation Static analysis from source code auditing rather than bytecode (different from FindBugs) 14
CodePro AnalytiX Auditing checks code against audit rules Can catch critical issues Finds lots of performance related or poor code styling issues 15
CodePro AnalytiX Metrics gives you a view into several aspects of your code Size, size per file/class/method Abstractness Various levels of complexity 16
CodePro AnalytiX 17
CodePro AnalytiX Dependency Graphs Shows dependences for external libraries and between packages Can get really messy Really good way to visualize circular dependencies among packages and to see dependencies 18
CodePro AnalytiX What can you make of this? 19
CodePro AnalytiX Drilling down allows you to start to view the issues with your architecture 20
Unit Testing Key process for test driven development TDD not just for large projects or products Useful for validating core functionality The higher the dependence on a library or class, the greater amount of Unit testing you want protecting it Code speaks louder than words Descriptions of a function can become outdated, a test that is always ran, is always up to date! 21
JUnit Defacto unit test framework for Java Comes with Eclipse IDE Simple to get started Allows calls to setup/cleanup methods to run before/after tests and testcases 22
JUnit (cont) package com.ibm.utm.core.tests; import org.junit.after; import org.junit.before; import org.junit.test; import static org.junit.assert.*; public class IIUGTtest { @Before public void startup() { System.out.println("code to run before running a tests goes here!"); } @After public void cleanup() { System.out.println("code to run after running a tests goes here!"); } @Test public void test1() { System.out.println("running test1"); assertequals(1, 2); } @Test public void test2() { System.out.println("running test2"); assertequals(1, 1); } } 23
TestNG Increasingly popular unit test framework for Java Just as easy as JUnit to get started Has more features and flexibility for managing larger number of unit tests Method grouping Test dependency Parameterized tests Source: http://www.mkyong.com/unittest/junit-4-vs-testng-comparison/ 24
TestNG (cont) package com.ibm.utm.core.tests; import org.testng.annotations.aftertest; import org.testng.annotations.beforetest; import org.testng.annotations.test; public class IIUGTestNG { @BeforeTest public void startup() { System.out.println("code to run before running a tests goes here!"); } @AfterTest public void cleanup() { System.out.println("code to run after running a tests goes here!"); } @Test public void test1() { System.out.println("running test1"); assertequals(1, 2); } @Test public void test2() { System.out.println("running test2"); assertequals(1, 1); } } 25
Runtime Analysis Once application is deployed, how do you track/resolve problems Services vs applications Services run 24x4 while applications run for a shorter time Require different approaches to tracking down issues that arise 26
Runtime Analysis - Logging Logging is an art Too much logging slows down systems, fills up disks, harder to search for problems Too little logging leaves you wondering what actually happened Logging can be as a simple as prints to the console or as complex as logging frameworks reporting to databases 27
Runtime Analysis SLF4J SLF4J Simple Logging Facade for Java commonly used logging framework for Java Very easy to setup and use Requires 2 JAR files Can swap out binded loggers Just put one in your CLASSPATH Easy to switch between loggers or create your own! Using SLF4J with Logback allows deep customization of logging using XML with zero code changes 28
Runtime Analysis SLF4J 29
Runtime Analysis IBM Health Center for Java Helpful diagnosing issues running the IBM version of Java Oracle Java has its own set of analysis tooling (JConsole) Easily installable Eclipse plugin from the Eclipse Marketplace Uses a health center agent to monitor the application and communicate with the Health Center Uses ~ 1% CPU overhead for monitoring Can collect memory, CPU, I/O, locking, loaded classes, threads Provides profiling capabilities 30
Runtime Analysis IBM Health Center for Java 31
Runtime Analysis IBM Health Center for Java 32
Runtime Analysis IBM Health Center for Java 33
Thread Analysis Useful when you wonder how many threads are running, is my program deadlocked, what is it doing right now? You can trigger thread dumps in Java by issuing a kill QUIT (kill -3) command against the Java PID Sometimes it s just as easy to manually view the javacore files 34
Thread Analysis IBM Thread and Monitor Dump Analyzer Free tool from IBM to analyze thread dumps Reads in javacore files Parses files to produce some information aggregation for you Useful when you need aggregation or summary of many threads 35
Heap Analysis Typically triggered by out of memory issues, heap dumps and subsequent analysis can be both crucial and complicated in finding and fixing problems with your application Memory leaks in java are typically collections not being emptied, or resources not being closed Sometimes you need to trigger you own heap dump Adding Xdump options to JVM can allow auto-trigger of heap dumps when a SIGQUIT is issued http://www- 01.ibm.com/support/docview.wss?uid=swg21242497 36
IBM HeapAnlayzer Handy tool for processing and analyzing Java heap dumps Attempts to automatically detect memory leaks by looking for the largest objects in the heap Displays breakdown of objects and what they contain Makes it easy to track down memory leaks due to collections holding too many objects and finding very large objects 37
IBM HeapAnalyzer Easy to read tree breakdown of the objects found in the Java heap Does not get you all the way In this example, which list from the map went awry? 38
Links CodePro Analytix https://developers.google.com/java-dev-tools/codepro/doc/ FindBugs http://findbugs.sourceforge.net/ EclEmma code coverage http://www.eclemma.org/ http://www.sonarqube.org/ SLF4J http://www.slf4j.org LogBack http://logback.qos.ch/ IBM Thread and Monitor Dump Analyzer https://www.ibm.com/developerworks/community/groups/service/html/communityview?communityuuid= 2245aa39-fa5c-4475-b891-14c205f7333c IBM HeapAnalyzer https://www.ibm.com/developerworks/community/groups/service/html/communityview?communityuuid= 4544bafe-c7a2-455f-9d43-eb866ea60091 IBM Health Center http://www.ibm.com/developerworks/java/jdk/tools/healthcenter/ 39
Questions? 40