QuickPerf Documentation

repository·master·Indexed 19 days ago

https://github.com/quick-perf/quickperf

A Java testing library for evaluating and improving performance properties using annotations. It enables monitoring and asserting JVM heap allocation and SQL query counts within JUnit 4, JUnit 5, and TestNG test suites, with specific support for detecting N+1 select issues in Hibernate and Spring Data JPA.

Tokens
1K
Snippets
5
Records
6
Agent score
69%

What's inside QuickPerf

  1. Overview of QuickPerf

    master
    QuickPerf is a Java testing library designed to quickly evaluate and improve performance-related properties of your application. It allows developers to use annotations to monitor and assert performance metrics such as JVM heap allocation and SQL query counts directly within their test suites.
  2. Use SpringRunnerWithQuickPerfFeatures for Spring 5 JUnit 4 tests

    master

    When writing JUnit 4 tests for Spring 5 applications, use SpringRunnerWithQuickPerfFeatures as your test runner to enable QuickPerf performance monitoring features alongside the Spring TestContext Framework. This runner combines the capabilities of SpringJUnit4ClassRunner with QuickPerfJUnitRunner.

    @RunWith(SpringRunnerWithQuickPerfFeatures.class)
    @ContextConfiguration(classes = MyConfig.class)
    public class MySpringTest {
        // Your performance-monitored Spring tests
    }
  3. Use SQL annotations to monitor database queries

    master

    QuickPerf provides annotations to assert the number of SQL statements executed during a test. This is particularly useful for detecting N+1 select issues in Hibernate or Spring Data JPA.

    Example: Use @ExpectSelect(n) to assert that exactly n select statements are executed.

    QuickPerf automatically detects Hibernate and Spring Data JPA and provides actionable suggestions (e.g., using JOIN FETCH or @EntityGraph) when assertions fail.

        @ExpectSelect(1)
        @Test
        public void should_find_all_players() {
         ...
        }
  4. Use JVM annotations to measure heap allocation

    master

    You can monitor JVM performance metrics by applying annotations to your test methods. For example, to measure heap allocation and assert a specific heap size limit, use @MeasureHeapAllocation and @HeapSize.

    Supported test frameworks include JUnit 4, JUnit 5, and TestNG.

        @MeasureHeapAllocation
        @HeapSize(value = 1, unit = AllocationUnit.GIGA_BYTE)
        @Test
        public void execute_batch() {
            ...
        }
  5. View QuickPerf debug information in the console

    master

    When troubleshooting QuickPerf configuration or execution order, you can view debug information in the standard output. The displayQuickPerfDebugInfos method outputs:

    1. JVM Options: A list of the JVM options used during execution.
    2. Recorder Execution Order: The priority and class names of recorders executed before and after test methods, determined by the QuickPerfConfigLoader implementations found via ServiceLoader.

    This is useful for verifying if your custom recorders are being loaded in the expected priority order.

    // Note: ConsoleReporter is package-private in the source, 
    // but its methods are used by the framework to output debug info.
    consoleReporter.displayQuickPerfDebugInfos(jvmOptions);
  6. View applied QuickPerf annotations in the console

    master

    The displayQuickPerfAnnotations method outputs the specific QuickPerf annotations applied to a test or class to the console.

    It provides:

    • A comma-separated list of the applied annotations (formatted via AnnotationFormatter).
    • The canonical name of the class that specifies global annotations (if any are configured via SpecifiableGlobalAnnotations).

    Note that the DisplayAppliedAnnotations annotation itself is filtered out of the output to avoid redundancy.

    // This method is called by the framework to report which 
    // annotations are currently active in the test context.
    consoleReporter.displayQuickPerfAnnotations(perfAnnotations);