java performance & profiling
TRANSCRIPT
![Page 1: Java Performance & Profiling](https://reader034.vdocuments.mx/reader034/viewer/2022052312/587ac39d1a28abc0478b75af/html5/thumbnails/1.jpg)
Java Performance & Profiling
M. Isuru Tharanga Chrishantha PereraAssociate Technical Lead at WSO2Co-organizer of Java Colombo Meetup
![Page 2: Java Performance & Profiling](https://reader034.vdocuments.mx/reader034/viewer/2022052312/587ac39d1a28abc0478b75af/html5/thumbnails/2.jpg)
What is Profiling?
Here is what wikipedia says:In software engineering, profiling ("program profiling", "software profiling") is a form of dynamic program analysis that measures, for example, the space (memory) or time complexity of a program, the usage of particular instructions, or the frequency and duration of function calls. Most commonly, profiling information serves to aid program optimization.
https://en.wikipedia.org/wiki/Profiling_(computer_programming)
![Page 3: Java Performance & Profiling](https://reader034.vdocuments.mx/reader034/viewer/2022052312/587ac39d1a28abc0478b75af/html5/thumbnails/3.jpg)
What is Profiling?
Here is what wikipedia says:Profiling is achieved by instrumenting either the program source code or its binary executable form using a tool called a profiler (or code profiler). Profilers may use a number of different techniques, such as event-based, statistical, instrumented, and simulation methods.
https://en.wikipedia.org/wiki/Profiling_(computer_programming)
![Page 4: Java Performance & Profiling](https://reader034.vdocuments.mx/reader034/viewer/2022052312/587ac39d1a28abc0478b75af/html5/thumbnails/4.jpg)
Measuring Performance
We need a way to measure the performance:o To understand how the system behaveso To see performance improvements after doing
any optimizations
There are two key performance metrics.o Latencyo Throughput
![Page 5: Java Performance & Profiling](https://reader034.vdocuments.mx/reader034/viewer/2022052312/587ac39d1a28abc0478b75af/html5/thumbnails/5.jpg)
What is Throughput?
Throughput measures the number of messages that a server processes during a specific time interval (e.g. per second).
Throughput is calculated using the equation:
Throughput = number of requests / time to complete the requests
![Page 6: Java Performance & Profiling](https://reader034.vdocuments.mx/reader034/viewer/2022052312/587ac39d1a28abc0478b75af/html5/thumbnails/6.jpg)
What is Latency?
Latency measures the end-to-end processing time for an operation.
![Page 7: Java Performance & Profiling](https://reader034.vdocuments.mx/reader034/viewer/2022052312/587ac39d1a28abc0478b75af/html5/thumbnails/7.jpg)
Tuning Java Applications
We need to have a very high throughput and very low latency values.
There is a tradeoff between throughput and latency. With more concurrent users, the throughput increases, but the average latency will also increase.
![Page 8: Java Performance & Profiling](https://reader034.vdocuments.mx/reader034/viewer/2022052312/587ac39d1a28abc0478b75af/html5/thumbnails/8.jpg)
Throughput and Latency Graphs
Source: https://www.infoq.com/articles/Tuning-Java-Servers
![Page 9: Java Performance & Profiling](https://reader034.vdocuments.mx/reader034/viewer/2022052312/587ac39d1a28abc0478b75af/html5/thumbnails/9.jpg)
Latency Distribution
When measuring latency, it’s important to look at the latency distribution: min, max, avg, median, 75th percentile, 98th percentile, 99th percentile etc.
![Page 10: Java Performance & Profiling](https://reader034.vdocuments.mx/reader034/viewer/2022052312/587ac39d1a28abc0478b75af/html5/thumbnails/10.jpg)
Longtail latencies
When high percentiles have values much greater than the average latency
Source: https://engineering.linkedin.com/performance/who-moved-my-99th-percentile-latency
![Page 11: Java Performance & Profiling](https://reader034.vdocuments.mx/reader034/viewer/2022052312/587ac39d1a28abc0478b75af/html5/thumbnails/11.jpg)
Latency Numbers Every Programmer Should Know
L1 cache reference 0.5 ns
Branch mispredict 5 ns
L2 cache reference 7 ns 14x L1 cache
Mutex lock/unlock 25 ns
Main memory reference 100 ns 20x L2 cache, 200x L1 cache
Compress 1K bytes with Zippy 3,000 ns 3 us
Send 1K bytes over 1 Gbps network 10,000 ns 10 us
Read 4K randomly from SSD* 150,000 ns 150 us ~1GB/sec SSD
Read 1 MB sequentially from memory 250,000 ns 250 us
Round trip within same datacenter 500,000 ns 500 us
Read 1 MB sequentially from SSD* 1,000,000 ns 1,000 us 1 ms ~1GB/sec SSD, 4X memory
Disk seek 10,000,000 ns 10,000 us 10 ms 20x datacenter roundtrip
Read 1 MB sequentially from disk 20,000,000 ns 20,000 us 20 ms 80x memory, 20X SSD
Send packet CA->Netherlands->CA 150,000,000 ns 150,000 us 150 ms
![Page 12: Java Performance & Profiling](https://reader034.vdocuments.mx/reader034/viewer/2022052312/587ac39d1a28abc0478b75af/html5/thumbnails/12.jpg)
Why do we need Profiling?
o Improve throughput (Maximizing the transactions processed per second)
o Improve latency (Minimizing the time taken to for each operation)
o Find performance bottlenecks
![Page 13: Java Performance & Profiling](https://reader034.vdocuments.mx/reader034/viewer/2022052312/587ac39d1a28abc0478b75af/html5/thumbnails/13.jpg)
Java Just-In-Time (JIT) compiler
Java code is usually compiled into platform independent bytecode (class files)
The JVM is able to load the class files and execute the Java bytecode via the Java interpreter.
Even though this bytecode is usually interpreted, it might also be compiled into native machine code using the JVM's Just-In-Time (JIT) compiler.
![Page 14: Java Performance & Profiling](https://reader034.vdocuments.mx/reader034/viewer/2022052312/587ac39d1a28abc0478b75af/html5/thumbnails/14.jpg)
Java Just-In-Time (JIT) compiler
Unlike the normal compiler, the JIT compiler compiles the code (bytecode) only when required. With JIT compiler, the JVM monitors the methods executed by the interpreter and identifies the “hot methods” for compilation. After identifying the Java method calls, the JVM compiles the bytecode into a more efficient native code.
![Page 15: Java Performance & Profiling](https://reader034.vdocuments.mx/reader034/viewer/2022052312/587ac39d1a28abc0478b75af/html5/thumbnails/15.jpg)
JITWatch
The JITWatch tool can analyze the compilation logs generated with the “-XX:+LogCompilation” flag.
The logs generated by LogCompilation are XML-based and has lot of information related to JIT compilation. Hence these files are very large.
https://github.com/AdoptOpenJDK/jitwatch
![Page 16: Java Performance & Profiling](https://reader034.vdocuments.mx/reader034/viewer/2022052312/587ac39d1a28abc0478b75af/html5/thumbnails/16.jpg)
Java Profiling Tools
Survey by RebelLabs in 2015: http://pages.zeroturnaround.com/RebelLabs---All-Report-Landers_Developer-Productivity-Report-2015.html
![Page 17: Java Performance & Profiling](https://reader034.vdocuments.mx/reader034/viewer/2022052312/587ac39d1a28abc0478b75af/html5/thumbnails/17.jpg)
Java Profiling Tools
Java VisualVM - Available in JDKJava Mission Control - Available in JDKJProfiler - A commercially licensed Java profiling tool developed by ej-technologies
![Page 18: Java Performance & Profiling](https://reader034.vdocuments.mx/reader034/viewer/2022052312/587ac39d1a28abc0478b75af/html5/thumbnails/18.jpg)
Profiling Applications with Java VisualVM
CPU Profiling: Profile the performance of the application.
Memory Profiling: Analyze the memory usage of the application.
![Page 19: Java Performance & Profiling](https://reader034.vdocuments.mx/reader034/viewer/2022052312/587ac39d1a28abc0478b75af/html5/thumbnails/19.jpg)
Java Mission Control
o A set of powerful tools running on the Oracle JDK to monitor and manage Java applications
o Free for development use (Oracle Binary Code License)
o Available in JDK since Java 7 update 40o Supports Pluginso Two main tools
o JMX Consoleo Java Flight Recorder
![Page 20: Java Performance & Profiling](https://reader034.vdocuments.mx/reader034/viewer/2022052312/587ac39d1a28abc0478b75af/html5/thumbnails/20.jpg)
Measuring Methods for CPU Profiling
Sampling: Monitor running code externally and check which code is executed
Instrumentation: Include measurement code into the real code
![Page 21: Java Performance & Profiling](https://reader034.vdocuments.mx/reader034/viewer/2022052312/587ac39d1a28abc0478b75af/html5/thumbnails/21.jpg)
Sampling vs. Instrumentation
Sampling:o Overhead depends on the sampling intervalo Can see execution hotspotso Can miss methods, which returns faster than
the sampling interval.Instrumentation:o Precise measurement for execution timeso More data to process
![Page 22: Java Performance & Profiling](https://reader034.vdocuments.mx/reader034/viewer/2022052312/587ac39d1a28abc0478b75af/html5/thumbnails/22.jpg)
Sampling vs. Instrumentation
o Java VisualVM uses both sampling and instrumentation
o Java Flight Recorder uses samplingo JProfiler supports both sampling and
instrumentation
![Page 23: Java Performance & Profiling](https://reader034.vdocuments.mx/reader034/viewer/2022052312/587ac39d1a28abc0478b75af/html5/thumbnails/23.jpg)
Problems with Profiling
o Runtime Overheado Interpretation of the results can be difficult
o Identifying the "crucial“ parts of the softwareo Identifying potential performance improvements
![Page 24: Java Performance & Profiling](https://reader034.vdocuments.mx/reader034/viewer/2022052312/587ac39d1a28abc0478b75af/html5/thumbnails/24.jpg)
Flame Graphs
Flame graphs are a visualization of profiled software, allowing the most frequent code-paths to be identified quickly and accurately.
Brendan Gregg created the open source program to generate flame graphs: https://github.com/brendangregg/FlameGraph
![Page 25: Java Performance & Profiling](https://reader034.vdocuments.mx/reader034/viewer/2022052312/587ac39d1a28abc0478b75af/html5/thumbnails/25.jpg)
Java CPU Flame Graphs
Helps to understand Java CPU Usage
With Flame Graphs, we can see both java and system profiles
Can profile GC as well
![Page 26: Java Performance & Profiling](https://reader034.vdocuments.mx/reader034/viewer/2022052312/587ac39d1a28abc0478b75af/html5/thumbnails/26.jpg)
Flame Graphs with Java Flight Recordings
We can generate CPU Flame Graphs from a Java Flight Recording
Program is available at GitHub: https://github.com/chrishantha/jfr-flame-graph
The program uses the (unsupported) JMC Parser
![Page 27: Java Performance & Profiling](https://reader034.vdocuments.mx/reader034/viewer/2022052312/587ac39d1a28abc0478b75af/html5/thumbnails/27.jpg)
Benchmarking Tools
Apache JMeterApache Benchmarkwrk - a HTTP benchmarking tool
![Page 28: Java Performance & Profiling](https://reader034.vdocuments.mx/reader034/viewer/2022052312/587ac39d1a28abc0478b75af/html5/thumbnails/28.jpg)
Does profiling matter?
Yes!Most of the performance issues are in the application code.
Early performance testing is key. Fix problems while developing.
![Page 29: Java Performance & Profiling](https://reader034.vdocuments.mx/reader034/viewer/2022052312/587ac39d1a28abc0478b75af/html5/thumbnails/29.jpg)
Thank you!