Skip to content

Add CLI and Gradle support for profiling rules#9016

Open
eygraber wants to merge 10 commits intodetekt:mainfrom
eygraber:profiling
Open

Add CLI and Gradle support for profiling rules#9016
eygraber wants to merge 10 commits intodetekt:mainfrom
eygraber:profiling

Conversation

@eygraber
Copy link
Contributor

Fixes #6962

Adds support to the CLI and Gradle plugin for profiling rules (built on top of a new RuleExecutionListener). Each project will generate a CSV containing the time it took to run each rule, and how many times they ran. There is also an aggregation task added to the root project that will write a combined CSV with the values from all of the subproject CSVs. It will also print the N slowest rules to the console.

Sample aggregate:

Rule                                          Total    Calls        Avg Findings
--------------------------------------------------------------------------------
Compose:ContentSlotReused                     1.44s      571      2.5ms        0
Compose:MutableStateAutoboxing                1.44s      571      2.5ms        0
formatting-eygraber:NoWhitespaceAfter...      1.36s      571      2.4ms        0
Compose:ContentTrailingLambda                 1.32s      571      2.3ms        0
style-eygraber:NewlineForMultilineKey...      1.11s      571      1.9ms        0
Compose:ComposableParamOrder                876.4ms      571      1.5ms        0
Compose:ParameterNaming                     712.7ms      571      1.2ms        0
Compose:LambdaParameterInRestartableE...    556.8ms      571     0.98ms        0
complexity:MethodOverloading                504.2ms      571     0.88ms        0
Compose:Material2                           499.6ms      571     0.87ms        0
--------------------------------------------------------------------------------
Total analysis time: 20.27s
Rules measured: 122

The feature itself was ~1k additions; the rest is tests and documentation.

@detekt-ci
Copy link
Collaborator

detekt-ci commented Jan 27, 2026

Warnings
⚠️

It looks like you're editing the un-versioned copy of our website. This affects only users on the 'next' version of detekt, and it's correct only if you intend to document a future change or feature. If you intended to make a change also for the current version of detekt, please make sure you edit the equivalent file inside website/versioned_docs/ as well.

Messages
📖

Thank you very much for making our website better ❤️!

Generated by 🚫 dangerJS against 264252b

@eygraber
Copy link
Contributor Author

If I backport this to v1 would you be able to make a new release for it? I think it would be helpful when it comes to deciding between updating from v1 to v2.

Comment on lines +149 to +156
/*
This is not the best method to create root project aggregation tasks, but it is the only
way to do it without user intervention. If task abbreviations are used (e.g. `dPDSS` instead
of `detektProfileDebugSourceSet`) it will not work.

The "correct" method would be to require the user to register DetektProfilingAggregateTasks in the
root project manually.
*/
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't have a good answer for what to do here. My gut feel is that this stays as a best effort, and if it doesn't work for a specific user for whatever reason they can register their own DetektProfilingAggregateTasks.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another option would be to provide a separate plugin that is applied to the root project, which provides an extension with a nice API that the user can use to register various different types of aggregating tasks.

@codecov
Copy link

codecov bot commented Jan 27, 2026

Codecov Report

❌ Patch coverage is 95.76271% with 5 lines in your changes missing coverage. Please review.
✅ Project coverage is 85.22%. Comparing base (368ec48) to head (264252b).

Files with missing lines Patch % Lines
...ain/kotlin/dev/detekt/api/RuleExecutionListener.kt 25.00% 3 Missing ⚠️
...c/main/kotlin/dev/detekt/core/tooling/Lifecycle.kt 83.33% 1 Missing and 1 partial ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##               main    #9016      +/-   ##
============================================
+ Coverage     85.13%   85.22%   +0.09%     
- Complexity     4380     4412      +32     
============================================
  Files           569      574       +5     
  Lines         12255    12365     +110     
  Branches       2642     2649       +7     
============================================
+ Hits          10433    10538     +105     
- Misses          676      680       +4     
- Partials       1146     1147       +1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@BraisGabin
Copy link
Member

TL;DR: Thanks for this PR, I'll review it soon. If you want an explanation about the late review continue reading.


The only thing that scares me a bit is how this values should be read. The Analysis API is really lazy so the first rules will pay the time of that lazyness so they will "seem" slower than the last ones when that's maybe not the case. And this is not "theorical" in this comment we can see how after we remove the classpath-validation step the rule execution is slower.

This is the reason I didn't review this PR yet, I don't know how to handle this.

But, probably something is better than nothing so we could add this as a first step and then iterate.

In a perfect world we would have perfetto traces of detekt execution. I think that we are far away from it as I couldn't find any easy way to create perfetto traces for JVM. (On Android is SO easy...)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Create an extension point at rule execution to measure performances

3 participants