-
Notifications
You must be signed in to change notification settings - Fork 155
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add MethodInvocationCounter Metric to Track Method Invocations within Classes #120
base: master
Are you sure you want to change the base?
Add MethodInvocationCounter Metric to Track Method Invocations within Classes #120
Conversation
… to handle the new MethodInvocationCount Metric
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This MR is great, thanks for the feature!
However, this one makes the output way larger. Therefore, this feature should be off by default, and enabled only optionally!
0b485ba
to
b791b4a
Compare
… Runner Java Files
…s according to the verbose execution modifier
…put of this class metric as verbose/large
…unter Metric and defining a standard isVerbose method to the ClassLevelMetric Interface File
Hi again, @mauricioaniche. I acknowledge that this PR is massive. If you prefer, i can split it into two PRs: One for the verbose execution argument and another for the new class metric itself ( |
Hi @mauricioaniche. The readme's conflict is solved :). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
All unnecessary changes were undone
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The unnecessary changes were undone.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the PR!
My main concern is about the new metric being a very long string. I think we should:
- Make this feature disabled by default, so we don't produce such large extra data for people that are already using CK
- Output it to a different file, so the CSV file stays short and sweet
@@ -208,10 +209,10 @@ in the directory and use them to better resolve types. `Max files per partition` | |||
of the batch to process. Let us decide that for you and start with 0; if problems happen (i.e., | |||
out of memory) you think of tuning it. `Variables and field metrics` indicates to CK whether | |||
you want metrics at variable- and field-levels too. They are highly fine-grained and produce a lot of output; | |||
you should skip it if you only need metrics at class or method level. Finally, `output dir` refer to the | |||
you should skip it if you only need metrics at class or method level. Aditionally, `output dir` refer to the |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's a typo here, Additionally
directory where CK will export the csv file with metrics from the analyzed project. | ||
Optionally, you can specify any number ignored directories, separated by spaces (for example, `build/`). | ||
By default, `.git` and all other hidden folders are ignored. | ||
By default, `.git` and all other hidden folders are ignored. Finally, the `verbose flag` tells CK if it must process metrics tagged as large outputs. If you are not interested in the detailed output of the metrics, you can set it to false. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
verbose
flag, flag shouldn't be highlighted
import java.util.*; | ||
import java.util.stream.Collectors; | ||
|
||
public class MethodCounter { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The string produced by this class can be really long. I'm not sure I'd like to mix the CSV, which is mostly numbers with this super large string. Also, given that this string can be quite long, just concatenating it to the CSV might make the CSV invalid. We'd need to use proper escaping here. An alternative would be to output this to a separate file. And make this feature disabled by default, so we don't start producing large data files to people that have been using CK so far (and do not expect it).
Overview
This PR introduces the
MethodInvocationCounter
metric, a new ClassLevelMetric designed to analyze and report on method invocation patterns within Java classes. The metric captures the frequency of invocations for each method within a class, providing insights into method dependencies and utilization.Key Changes
CKClassResult.java (REFACTOR):
methodInvocations
to store the formatted string representing method invocation statistics for each class.setMethodInvocation(String methodInvocations)
andgetMethodInvocations()
to handle this new data. This allows the stored invocation data to be set during metric calculation and later retrieved for output.MethodCounter.java (FEAT):
MethodInformation
class).formatResult
andtoFormattedString
methods).MethodInvocationCounter.java (FEAT, FIX, REFACTOR):
MethodInvocationCounter
class which implementsCKASTVisitor
andClassLevelMetric
. It contains the logic to visit method declarations and invocations, and it populates theMethodCounter.MethodInformation
data structure with this information.visit(MethodDeclaration node)
to set the current method context andvisit(MethodInvocation node)
to record invocations within the context of the current method.setResult(CKClassResult result)
to format and set the method invocation data on theCKClassResult
.ResultWriter.java (REFACTOR):
methodInvocations
in the output of the CK tool. This ensures that the collected method invocation data is written out alongside other metrics.Implementation Details
The
MethodInvocationCounter
leverages the AST (Abstract Syntax Tree) to visit and analyzeMethodDeclaration
andMethodInvocation
nodes within Java source files. For each class, it maintains a mapping of methods and the methods they invoke, along with the invocation counts. This data is then formatted into a human-readable string that summarizes the invocation patterns, making it easy to identify which methods are central to the functionality of the class.Output
Here is an example of the Output generated when using CK to generate the class.csv for the ZooKeeper repository:
Usefulness
The
MethodInvocationCounter
metric is particularly useful for:Conclusion
The addition of the
MethodInvocationCounter
provides valuable insights into method usage patterns, making it a useful tool for developers looking to maintain and improve the quality of their Java applications.