I created two separate functions to test if SonarQube can detect performance issues. First one is an infinite loop and second function takes lot of time to execute.
## Function 1
def infinite_loop_with_issue():
while True:
pass
infinite_loop_with_issue()
## Function 2
def concatenate_strings_performance_issue():
result = ""
import time
time.sleep(100)
for i in range(1000):
time.sleep(0.1)
result += str(i)
return result
# Call the function
result_string = concatenate_strings_performance_issue()
print(result_string)
It is surprising that SonarQube didn’t detect any performance issues with both of these functions. Any idea why?
I really appreciate the value that issues around infinite loops should be detected if they’re unintended, however, that intent is difficult to ascertain. Do you have some real-world code examples of unintended infinite loops? Where we see them in the wild, they are intended (eg event listening) and artificial examples do not really make it clear how this kind of issue could happen unintentionally. We do have a rule in a similar spirit, but focused on recursion, but it is not free of false-positive reports.
Similarly, the performance example does have the same challenge - a real-world example where the unintended performance issue is clear would help. Our goal would be to ensure that the rule is not noisy. Having said this, we are looking into performance rules for Python generally, and you can follow the progress here.