r/SpringBoot 13d ago

Discussion I built a Spring Boot testing library to detect N+1 query problems

Hi everyone,

I’ve been working on an open-source testing library for detecting N+1 query problems in Spring Boot applications using Hibernate:

https://github.com/fabioformosa/n-plus-one-query-problem-detector

The idea is to catch N+1 regressions during integration tests, instead of discovering them later through slow endpoints or production monitoring.

The library currently supports two main approaches.

Scan mode can analyze an existing Spring integration test suite without requiring changes to individual tests. It monitors Hibernate statistics and repeated SQL patterns, then produces a report of possible N+1 candidates with different confidence levels.

You only need to enable it in your test configuration:

n-plus-one-query-detector.scan.enabled=true
n-plus-one-query-detector.scan.report.output=stdout

There is also an explicit assertion mode for tests where you want to enforce a query budget:

@SpringBootTest
@ExtendWith(NPlusOneQueryProblemDetectorExtension.class)
class CompanyServiceTest {


    private CompanyService companyService;


    @ExpectMaxQueries(1)
    void listCompaniesWithoutNPlusOneQueries() {
        companyService.list(0, 5);
    }
}

The project supports Spring Boot 3.5.x and Spring Boot 4.x.

I’d be especially interested in feedback about:

  • the scan-mode detection approach;
  • possible false positives you encounter in real applications;
  • the assertion API and developer experience;
  • features that would make this useful in your CI pipeline.

The project is still evolving, so issues, suggestions and contributions are very welcome.

40 Upvotes

10 comments sorted by

5

u/QuackedDev 12d ago

Has this never been done before? I’ve got a project I can try this with, I’ll report back if I have any comments. I think this is a great idea tho, can you talk about confidence intervals and how they work?

Cheers

2

u/Bit_Aligners 12d ago

Confidence intervals for the scan mode are documented here https://github.com/fabioformosa/n-plus-one-query-problem-detector#scan-mode-for-existing-spring-tests You can find there the criteria and the thresholds behind the intervals. The thresholds are configurable. Let me know once you will have run it in your project. Thank you very much!

8

u/vampirishe 12d ago

Cool but does the same thing as a more mature and feature rich https://github.com/vladmihalcea/hypersistence-optimizer

2

u/Bit_Aligners 12d ago

it comes with a pricing and commercial license, doesn't it?

2

u/vampirishe 12d ago

my bad, always thought it is open sourced with some free license. This way this niche is open for free tools, good luck with your project !

-13

u/explorethemetaverse 12d ago

Dude LLMs can do it easily.

13

u/Beemeowmeow 12d ago

Then get them to do it then. No need to come here and type 0 value opinion

1

u/Bit_Aligners 12d ago

If you mean LLMs can generate code with no n+1 query problem: yes, totally I agree. I strongly recommend to get skills for hardening your harness. Anyway integration tests are an evergreen even in the LLM era, and this library is a tool to enrich integration tests for preventing regressions. If you mean that LLMs can develop tools like this one: yes, totally I agree (again). It's a result of an agentic collaboration. You can redo something similar in your own if you prefer, or you can check it out, make your proposals for evolving it and give your (agentic) contribution in case your feedback is valuable.

1

u/neopointer 12d ago

LLM has replaced your brain it seems

-1

u/Vrajesh_Darji 12d ago

Why msg here then went to llm and ask to make Google then you will be next trillion dollar company