Skip to content

[core] Incremental analysis - #125

Merged
adangel merged 9 commits into
pmd:masterfrom
jsotuyod:incremental-analysis
Nov 25, 2016
Merged

adangel merged 9 commits into
pmd:masterfrom
jsotuyod:incremental-analysis

Conversation

@jsotuyod

@jsotuyod jsotuyod commented Nov 11, 2016 •

Copy link
Copy Markdown
Member

This turned out to be much simpler to implement than expected, however, I'd love to have some in-depth peer review. There are actually a couple points on which I'm not completely sure and would love some feedback.

The results are really promising (always analyzing the complete PMD source code):

Mono thread - java-braces - noop cache

-d ./ -b -f text -r no-cache.report -R java-braces -threads 0

-----------------------------<<< Final Summary >>>-----------------------------
Total                                           Time (secs)

Measured total:                                      25.381
Non-measured total:                                   0.729
Total PMD:                                           26.111

Mono thread - java-braces - empty file cache

-d ./ -b -f text -r empty-cache.report -R java-braces -threads 0 -cache pmd.cache

-----------------------------<<< Final Summary >>>-----------------------------
Total                                           Time (secs)

Measured total:                                      25.892
Non-measured total:                                   0.867
Total PMD:                                           26.759

~0.65 overhead for writing the file cache.

Mono thread - java-braces - primed file cache

-d ./ -b -f text -r primed-cache.report -R java-braces -threads 0 -cache pmd.cache

-----------------------------<<< Final Summary >>>-----------------------------
Total                                           Time (secs)

Measured total:                                      13.451
Non-measured total:                                   0.680
Total PMD:                                           14.131

Huge gains since we skipped analyzing several files altogether.

Of course, all reports are exactly the same:

$ md5sum *.report
37c884377d83fc07a57775acbb7b9679  empty-cache.report
37c884377d83fc07a57775acbb7b9679  no-cache.report
37c884377d83fc07a57775acbb7b9679  primed-cache.report

I repeated the experiment with a somewhat more complex ruleset: http://static.monits.com/pmd.xml. More rules raises the chances of having violations, and therefore, the misses on the cache (wihch right now only saves files with no violations).

Mono thread - http://static.monits.com/pmd.xml - no cache

-----------------------------<<< Final Summary >>>-----------------------------
Total                                           Time (secs)

Measured total:                                      67.455
Non-measured total:                                   1.184
Total PMD:                                           68.639

Mono thread - http://static.monits.com/pmd.xml - outdated file cache

We are reusing the cache from the previous run, but the ruleset changed, invalidating it. So we have the extra cost of loading, and no benefits. This is the worst case scenario.

INFO: Analysis cache invalidated, rulesets changed.

-----------------------------<<< Final Summary >>>-----------------------------
Total                                           Time (secs)

Measured total:                                      69.944
Non-measured total:                                   1.477
Total PMD:                                           71.421

~2.8 overhead for reading & writing the file cache.

Mono thread - http://static.monits.com/pmd.xml - primed file cache

-----------------------------<<< Final Summary >>>-----------------------------
Total                                           Time (secs)

Measured total:                                      44.166
Non-measured total:                                   0.982
Total PMD:                                           45.148

Once again, significant gains, even with a much lower hit ratio.

$ md5sum *.report
91bfc759db88d53abe5dfb7ba0ddf47d  no-cache.report
91bfc759db88d53abe5dfb7ba0ddf47d  old-cache.report
91bfc759db88d53abe5dfb7ba0ddf47d  primed-cache.report

Multi thread (4 threads) - http://static.monits.com/pmd.xml - no cache

-----------------------------<<< Final Summary >>>-----------------------------
Total                                           Time (secs)

Measured total:                                     151.734
Non-measured total:                                -104.830
Total PMD:                                           46.904

Multi thread (4 threads) - http://static.monits.com/pmd.xml - empty file cache

-----------------------------<<< Final Summary >>>-----------------------------
Total                                           Time (secs)

Measured total:                                     163.697
Non-measured total:                                -113.610
Total PMD:                                           50.087

~3.1 secs overhead

Multi thread (4 threads) - http://static.monits.com/pmd.xml - primed file cache

-----------------------------<<< Final Summary >>>-----------------------------
Total                                           Time (secs)

Measured total:                                     116.428
Non-measured total:                                 -81.296
Total PMD:                                           35.131

Once again, significant gains, even with a much lower hit ratio.

$ md5sum *.report
91bfc759db88d53abe5dfb7ba0ddf47d  empty-cache.report
91bfc759db88d53abe5dfb7ba0ddf47d  no-cache.report
91bfc759db88d53abe5dfb7ba0ddf47d  primed-cache.report

I still need to run this through a profiler. The overhead, specially for reading, seems to be a little excesive, 'though I see nothing obvious in the code (I'm using a BufferedInputStream), and it could also be a meassuring error (the problem with single run measurements when difference is smaller than the noise).

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Report listeners are synchronized, even 'though the cache is thread-safe... I've not profiled it, but it may be a cause of contingency. Any better way around this?

Also, do listeners really need to be synchronized? Can't we just make them thread-safe?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The listeners might be provided by external libraries/programs, when they call PMD - so they could potentially be not-thread-safe...
I think, we had this problem, once we switched on multi-threading by default....

The underlying problem is of course the API the PMD provides for integration: How to execute PMD and how to gather the results (violations including processing errors and suppressions). If we find a better way than these listeners, we can mark this as deprecated. Maven-pmd-plugin uses a custom renderer by now to collect the results and doesn't use the listener anymore....

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I don't like this at all, but can't think of a better way other than making it nullable and checking against null all the time.... ideas?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This checksum reminded me of hashCode - especially in RuleSets.
What about a "ChecksumAware" interface (name not good)?

Is it correct, that only RuleSet instances, that would be created programmatically, could end up without a checksum? For any other case, we would load the ruleset from file and use the file checksum, right?
So, maybe it's enough to mark this constructor as deprecated and leave it and remove it in a later version....

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Sounds good. The name reminds me a bit of Spring Framework's Aware interface and subinterfaces, it probably works. I'll refactor it as soon as I get the chance. Yes, it's implemented as a hashCode on RuleSets. As a checksum I just want something that can be generated fast, and will change if anything changes.

Right now this second constructor is used only in unit tests, the designer, the benchmarker and XPathCLI. None of these should use the analysis cache, except maybe XPathCLI?

In future versions we may have Rule be ChecksumAware and have RuleSet compute their checksums off the Rules, just like RuleSets does from RuleSet maybe?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I tend more towards just defining a RuleSet must always be ChecksumAware and we do not allow a way around it. This means, we do not need to mark RuleSets explicitly as ChecksumAware.
Maybe we can make the constructor of RuleSet private and allow to create rulesets only through RuleSetFactory, which

  • either loads a ruleset from an XML file - then we have the checksum
  • or creates a ruleset with a single xpath rule - we could create the checksum based on the xpath expression. That would be the XPath CLI use case.

RuleSets can stay the way it is - always calculating the checksum based on the checksums of the contained RuleSet instances.

The XPathCLI is btw. a very old client - it's purpose seems to be to execute a XPath expression against a source file and display the matches. This is exactly what the Designer is able to do within its GUI. It is not even exposed in the start script, so we might ditch this XPathCLI altogether.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Should we split this into the AnalysisCache and a separate persistence strategy, using composition rather than inheritance?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I would say, it's not needed yet - or do you have another persistence strategy in mind already?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Nothing in particular, that' why I did it this way. But still wanted someone else's opinion.

@jsotuyod jsotuyod changed the title Incremental analysis [core] Incremental analysis Nov 11, 2016
 - Only invalidated by PMD version change.
 - Hooked to CLI and ANT.
 - A NOOP and a File based implementations available.
 - Unit tested where possible.
 - Still several TODOs to handle.
 - Not completely convinced by this implementation, but is simple enough
    and it's working.
 - Split the generic cache implementation from the file-backing persistence
    strategy. This could probably work better as a composition rather than
    inheritance, but we will take it one step at a time.
 - Took the chance to improve error messages produces by FileAnalysisCache
 - Make sure the `ruleSet` collection has a consistent access order.
 - A simple constructor will work just fine.
 - Plus, this makes it even more obvious that no exceptions / null values
    are to be expected when loading a cache from file, even when it's invalid
    or badly configured.
 - indentation fixes, missing licenses, some javadoc, complete some TODOs
@jsotuyod
jsotuyod force-pushed the incremental-analysis branch from 4b635a7 to 0d684d2 Compare November 11, 2016 23:42
@adangel adangel added this to the 5.6.0 milestone Nov 14, 2016
@adangel adangel added an:enhancement An improvement on existing features / rules for:performance The goal of this change is to improve PMD's performance labels Nov 14, 2016
@adangel

adangel commented Nov 25, 2016

Copy link
Copy Markdown
Member

PR looks good! Thanks!

Once the feature is ready for use, we should update the command line / ant documentation: https://github.com/pmd/pmd/blob/master/src/site/markdown/usage/ant-task.md and https://github.com/pmd/pmd/blob/master/src/site/markdown/usage/running.md.vm

For the cache file, I have one question: How do we handle compatibility? E.g. we change the file format, etc... Ok, it starts with the PMD version string. Is this enough?

@adangel
adangel merged commit 0d684d2 into pmd:master Nov 25, 2016
@jsotuyod
jsotuyod deleted the incremental-analysis branch November 25, 2016 17:47
@jsotuyod

Copy link
Copy Markdown
Member Author

@adangel I think so, yes. I don't see how a same version of PMD would produce two incompatible versions of the cache, and I'd rather invalidate by version, since new versions may fix false positives / false negatives meaning we want to re-analyze in spite of what the cache may say.

Of course this will have to be reviewed if we change the release process and stop releasing all jars at the same time with the same version numbers, but until then...

@adangel

adangel commented Nov 26, 2016

Copy link
Copy Markdown
Member

Btw. It seems we have a benchmark measuring problem when running multithreaded:

Measured total: 116.428
Non-measured total: -81.296
Total PMD: 35.131

The "Total PMD" time is too small, leading to a negative "Non-measured total"... The Total PMD time should be the total time, PMD was running...
The start time is gathered on line 494 and the elapsed time is calculated in the finally block on line 520. Any ideas why this would be executed before PMD is finished?

@jsotuyod

Copy link
Copy Markdown
Member Author

I believe the issue raises because we are measuring different things.

Total PMD is wall clock time, from start to finish.

Measured total however is total CPU time. Each thread runs for x time on a given task, and that time is stored and added for "Measured Total". Since threads run in parallel, we are adding up more "processing seconds" than actual seconds of execution.

jsotuyod added a commit to jsotuyod/pmd that referenced this pull request Jan 17, 2017
 - This follows discussion on pmd#125 (comment)
 - We add a marker interface for thread-safe listeners, and implement it were appropriate.
 - Breaking change: `getSynchronizedListeners` and `addSynchronizedListeners` now handle
    `List<ThreadSafeReportListener>`.
 - We may mark the non-thread-safe methods and interfaces as deprecated and force everyone
    to handle synchronization on their own for future releases.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

an:enhancement An improvement on existing features / rules for:performance The goal of this change is to improve PMD's performance

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants