Chapter 7: Finding hidden issues using profiling techniques
Introduces profiling as a way to investigate execution in hard-to-read applications and previews the chapter’s techniques.
After this chapter you will be able to
- Profiling Hard-to-Read Code. Explain why a profiler can help identify executing code in a difficult-to-read application.
- Sampling as investigation starting point. Explain what sampling reveals and why it precedes more detailed profiling. Describe how it can also help locate a place to debug.
- VisualVM CPU sampling workflow. Describe how to start CPU sampling in VisualVM and inspect the execution of the example endpoint.
- Execution time and CPU time. Distinguish execution time from CPU time and use the example to explain how sampling reveals waiting as the source of latency.
- Dependency and dynamic proxy visibility. Explain why sampling dependency methods is useful when a framework supplies an implementation dynamically.
- Invocation Counts and Latency. Explain why invocation counts help diagnose latency and interpret the section’s profiling example.
- Sampling-Guided Profiling Scope. Explain why profiling should be restricted and how sampling guides the choice of code to profile.
- Dynamically generated SQL investigation. Explain why SQL queries generated by a framework or library can be difficult to identify and how a profiler can help.
- JDBC query capture. Explain how to retrieve SQL queries with the profiler and what to do with the captured queries.
- Repeated query design. Explain the repeated-query problem in the example and how a join could improve it.
- Framework generated SQL capture. Explain why framework-generated SQL can be difficult to obtain from source code and how a profiler reveals it.
- The N+1 query problem. Describe how an N+1 query problem arises and why it can cause latency.
- Profiling versus query logs. Distinguish profiling from logging when investigating slow or excessively repeated SQL queries.
- Auditing generated SQL. Explain how profiling helps audit SQL generated from programmatically defined Hibernate criteria queries.
- Cross-join diagnosis and correction. Trace the example's unwanted cross join from its discovery and potential effects to its cause and correction.
What it builds on
- Navigating code with the debugger (chapter 2) · Debugging requires choosing where to pause execution.
- Installing and configuring VisualVM (chapter 6) · VisualVM setup and the VM option needed to connect to the process.
Section by section
Introduction
Introduces profiling as a way to investigate execution in hard-to-read applications and previews the chapter’s techniques.
- Profiling Hard-to-Read Code: Explain why a profiler can help identify executing code in a difficult-to-read application.
Why it matters: This gives you a way to investigate behavior even when the code design is difficult to follow.
Key terms: profiler, app execution
7.1 Sampling to observe executing code
This section uses VisualVM sampling to reveal executing code and investigate the source of latency, including work hidden inside dependencies.
- Sampling as investigation starting point: Explain what sampling reveals and why it precedes more detailed profiling. Describe how it can also help locate a place to debug.
Why it matters: Sampling can narrow an unfamiliar investigation before you invest in more detailed tools. - VisualVM CPU sampling workflow: Describe how to start CPU sampling in VisualVM and inspect the execution of the example endpoint.
Why it matters: This workflow turns a slow operation into a view of the methods active during it. - Execution time and CPU time: Distinguish execution time from CPU time and use the example to explain how sampling reveals waiting as the source of latency.
Why it matters: Distinguishing waiting from computation directs you toward the actual source of a slowdown. - Dependency and dynamic proxy visibility: Explain why sampling dependency methods is useful when a framework supplies an implementation dynamically.
Why it matters: The executing implementation may be in a library and invisible from the application’s call site.
Key terms: sampling, profiling, instrumentation, breakpoint, VisualVM, Sampler, CPU, active threads, stack trace, execution time
7.2 Profiling to learn how many times a method executed
This section uses targeted profiling to count method invocations and distinguish a slow individual call from repeated calls.
- Invocation Counts and Latency: Explain why invocation counts help diagnose latency and interpret the section’s profiling example.
Why it matters: Knowing the call count helps you distinguish a slow call from unnecessary repeated work. - Sampling-Guided Profiling Scope: Explain why profiling should be restricted and how sampling guides the choice of code to profile.
Why it matters: Narrowing the profiling scope avoids spending excessive time and resources on unrelated code. - Profiler Filter Syntax: Apply the profiler’s filter rules to select a package, its subpackages, or one class.
Why it matters: Correct filters let you focus profiling on precisely the code you want to investigate.
Key terms: sampling, profiling, method invocations, latency, feign package, intercepted code, Profiler tab, filter, package, subpackages
7.3 Using a profiler to identify SQL queries an app executes
This section introduces the difficulty of identifying dynamically generated SQL queries and the value of a profiler for investigating them.
- Dynamically generated SQL investigation: Explain why SQL queries generated by a framework or library can be difficult to identify and how a profiler can help.
Why it matters: Knowing why generated queries are hard to find helps you recognize when a profiler can help.
Key terms: dynamically generated queries, profiler, persistence layer
7.3.1 Using a profiler to retrieve SQL queries not generated by a framework
This section uses VisualVM to trace an endpoint’s execution, retrieve its JDBC queries, and identify a repeated-query design problem.
- Method call investigation: Describe how profiling the /products endpoint helps locate code to inspect before reading it.
Why it matters: The call profile helps you decide where to begin reading the code. - JDBC query capture: Explain how to retrieve SQL queries with the profiler and what to do with the captured queries.
Why it matters: Capturing the queries lets you investigate database behavior even when finding SQL in the code is difficult. - Query execution stack: Describe how to find the application code that sent a captured query.
Why it matters: The SQL alone does not show where in your application to make a change. - Repeated query design: Explain the repeated-query problem in the example and how a join could improve it.
Why it matters: Repeated database queries can reveal an opportunity to simplify the implementation and reduce resource use.
Key terms: Profiler tab, class names, method names, JDBC, JDBC driver, DBMS, SQL queries, execution stack, codebase, query count
7.3.2 Using the profiler to get the SQL queries generated by a framework
This section shows how profiling reveals framework-generated SQL and helps investigate query-related latency.
- Framework generated SQL capture: Explain why framework-generated SQL can be difficult to obtain from source code and how a profiler reveals it.
Why it matters: This lets you inspect and test SQL that is not readily available in the application code. - Repository calls and executions: Explain what the example shows about the relationship between repository method calls and SQL executions.
Why it matters: Counting method calls alone may not tell you how often the database actually receives a query. - The N+1 query problem: Describe how an N+1 query problem arises and why it can cause latency.
Why it matters: Recognizing this pattern helps explain why apparently simple data access can generate excessive database work. - Profiling versus query logs: Distinguish profiling from logging when investigating slow or excessively repeated SQL queries.
Why it matters: The profiler helps narrow down SQL problems before you spend time searching and reconstructing logged queries. - Latency investigation workflow: Describe the recommended order of tools when investigating latency that may involve SQL.
Why it matters: Starting broadly and then focusing on JDBC helps direct the investigation before confirming a suspected cause.
Key terms: Spring Data JPA, JPQL, JDBC driver, DBMS, repository method, Hibernate, query execution, N+1 query problem, query executions, latency
7.3.3 Using the profiler to get programmatically generated SQL queries
This section uses JDBC profiling to diagnose and correct an inefficient SQL query generated from a Hibernate criteria query.
- Auditing generated SQL: Explain how profiling helps audit SQL generated from programmatically defined Hibernate criteria queries.
Why it matters: Auditing generated SQL can expose costly mistakes that are not obvious from the programmatic query definition. - Cross-join diagnosis and correction: Trace the example's unwanted cross join from its discovery and potential effects to its cause and correction.
Why it matters: A small criteria-query mistake can generate an expensive query whose impact grows with the data. - Repeated query inefficiency: Describe the remaining inefficiency after the cross join is removed and the suggested direction for improvement.
Why it matters: Fixing one SQL problem does not necessarily remove every source of inefficiency.
Key terms: criteria queries, Hibernate, JDBC profiling, VisualVM, cross join, product table, CriteriaQuery, from(), product instance, query
7.4 Summary
Where this is used later
- (chapter intro) (chapter 8) · Sampling and visualizing an app’s execution as discussed in the preceding chapter.
- Detecting problems with JDBC connections (chapter 8) · Sampling to obtain an overview of executing code.
- Investigating communication between services (chapter 12) · Prior investigation of communication between an application and a database.
Concepts explained in this chapter
- profilingThe practice of gathering information about a program's execution to understand its performance or behavior.
- samplingSampling is a profiling technique that observes selected parts of a program’s execution to reveal its behavior.
- profilerA software analysis tool that measures program execution, such as time spent in code, to help identify performance problems.
- latencyDelay in an application's operations or responses, often caused by waiting for input or output.
- overall execution timeThe elapsed time for which a thread has been alive.
- N + 1 query problemA performance problem in which retrieving a collection triggers one initial query followed by a separate query for each retrieved record.
- method invocation countThe number of times a method is called during program execution.
- jdbc profilingProfiling that intercepts database queries sent through the Java Database Connectivity API.
- CPU timeThe time a program spends executing instructions on the CPU.
These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.
