-
-
Notifications
You must be signed in to change notification settings - Fork 122
GCI35 : Check an existing rule in Credengo Python that had been implemented but not previously validated. #456
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
Open
aaudric
wants to merge
2
commits into
green-code-initiative:main
Choose a base branch
from
aaudric:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,22 +7,161 @@ When an exception is thrown, a variable (the exception itself) is created in a c | |
|
|
||
| [source,python] | ||
| ---- | ||
| try: | ||
| f = open(path) | ||
| print(fh.read()) | ||
| except: | ||
| print('No such file '+path | ||
| finally: | ||
| f.close() | ||
| try : | ||
| with open(path, "r") as f: | ||
| _ = f.read() | ||
| except : | ||
| pass | ||
|
dedece35 marked this conversation as resolved.
|
||
| ---- | ||
|
|
||
| == Compliant Solution | ||
| == Compliant Code Example | ||
|
|
||
| [source,python] | ||
| ---- | ||
| if os.path.isfile(path): | ||
| fh = open(path, 'r') | ||
| print(fh.read()) | ||
| fh.close | ||
| with open(path, "r") as f: | ||
| _ = f.read() | ||
| ---- | ||
|
|
||
| == Experimental Verification | ||
|
|
||
| The rule was evaluated using: | ||
|
|
||
| * CodeCarbon to estimate CO₂ emissions | ||
| * execution time measurements | ||
| * multiple iteration scales to simulate real execution contexts | ||
|
|
||
| === Configuration | ||
|
|
||
| [source,python] | ||
| ---- | ||
| from codecarbon import EmissionsTracker | ||
| import matplotlib.pyplot as plt | ||
| import os | ||
| import time | ||
|
|
||
| path = ('text.txt') # the famous file not founded | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @aaudric please, take into account copilot feedback |
||
| ---- | ||
|
|
||
| === Non compliant Implementation | ||
|
|
||
| [source,python] | ||
| ---- | ||
| #function who use the no compliant_solution | ||
| def no_compliant_solution(): | ||
| for i in range(nb_runs): | ||
| try : | ||
| with open(path, "r") as f: | ||
| _ = f.read() | ||
| except : | ||
| pass | ||
|
dedece35 marked this conversation as resolved.
|
||
| ---- | ||
|
|
||
| === Compliant Implementation | ||
|
|
||
| [source,python] | ||
| ---- | ||
| #function who use the compliant solution | ||
| def compliant_solution(): | ||
| for i in range(nb_runs): | ||
| if os.path.isfile(path): | ||
| with open(path, "r") as f: | ||
| _ = f.read() | ||
|
|
||
| ---- | ||
|
|
||
| === Measurement Function | ||
|
|
||
| [source,python] | ||
| ---- | ||
| # function to measure the impact of the both solutions | ||
| def measure(function): | ||
| start_time = time.time() | ||
| tracker = EmissionsTracker() | ||
| tracker.start() | ||
|
|
||
| function() | ||
| end_time = time.time() | ||
|
|
||
| emission = tracker.stop() | ||
| full_time = end_time-start_time | ||
|
|
||
| return emission, full_time | ||
|
|
||
| print(f"Emisssion no compliant solution :{no_emission_compliant_solution:.2e}") | ||
| print(f"Time for no compliant solution :{no_time_compliant_solution:.2f}") | ||
|
|
||
| print(f"Emisssion compliant solution : {emission_compliant_solution:.2e}") | ||
| print(f"Time for compliant solution :{time_compliant_solution:.2f}") | ||
|
dedece35 marked this conversation as resolved.
|
||
| ---- | ||
|
|
||
| === Initial Results | ||
|
|
||
| Emisssion no compliant solution : 2.76e-05 | ||
| Time for no compliant solution : 39.21 | ||
|
|
||
| Emisssion compliant solution : 1.86e-05 | ||
| Time for compliant solution : 26.35 | ||
|
|
||
| The compliant solution shows: | ||
|
|
||
| * lower execution time | ||
| * lower CO₂ emissions | ||
| * reduced unnecessary resource consumption | ||
|
Comment on lines
+108
to
+110
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @aaudric please, take into account copilot feedback |
||
|
|
||
| == Large-Scale Iteration Analysis | ||
|
|
||
| To simulate real-world execution scenarios, the two implementations were tested with increasing numbers of iterations. | ||
|
|
||
| [source,python] | ||
| ---- | ||
| #Code to show the impact with a simulation of a real context | ||
| runs = [100, 500, 1000, 5000, 10000, 100000, 1000000, 10000000, 100000000] | ||
| times_no = [] | ||
| times_yes = [] | ||
| em_no = [] | ||
| em_yes = [] | ||
|
|
||
| for r in runs: | ||
| nb_runs = r | ||
| e1, t1 = measure(no_compliant_solution) | ||
| e2, t2 = measure(compliant_solution) | ||
|
|
||
| times_no.append(t1) | ||
| times_yes.append(t2) | ||
| em_no.append(e1) | ||
| em_yes.append(e2) | ||
|
|
||
| plt.plot(runs, times_no, label="try/except") | ||
| plt.plot(runs, times_yes, label="os.path.isfile") | ||
| plt.xlabel("Iterations") | ||
| plt.ylabel("Time (s)") | ||
| plt.legend() | ||
| plt.show() | ||
|
|
||
| plt.plot(runs, em_no, label="try/except") | ||
| plt.plot(runs, em_yes, label="os.path.isfile") | ||
| plt.xlabel("Iterations") | ||
| plt.ylabel("CO2 emissions en kg") | ||
| plt.legend() | ||
| plt.show() | ||
| ---- | ||
|
|
||
| === Results | ||
| image::../python/comparison_plots.png[Metrics for the both splutions] | ||
|
|
||
| The emissions trend follows the same behavior as execution time: | ||
|
|
||
| * the try/except implementation consistently produces more emissions | ||
| * the gap increases with the number of iterations | ||
| * avoiding unnecessary exceptions improves energy efficiency | ||
|
|
||
| == Conclusion | ||
|
|
||
| Using try/except blocks to manage expected situations such as missing files introduces avoidable overhead in Python applications. | ||
|
|
||
| In repetitive or performance-sensitive code paths: | ||
|
|
||
| * exception handling increases execution time | ||
| * unnecessary object creation consumes additional resources | ||
| * higher CPU usage leads to greater energy consumption and CO₂ emissions | ||
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
please add the number of the rule