Skip to content

Commit c0ed56d

Browse files
committed
Implement performance test for large number of violations and suppressions.
This test validates the performance increase the file name cache per suppression yields.
1 parent 3693c19 commit c0ed56d

1 file changed

Lines changed: 39 additions & 0 deletions

File tree

test/cli/performance_test.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -413,3 +413,42 @@ class C {
413413
}
414414
}""")
415415
cppcheck([filename]) # should not take more than ~1 second
416+
417+
418+
@pytest.mark.timeout(5)
419+
def test_large_number_of_violations_and_suppressions(tmpdir):
420+
filename_main = os.path.join(tmpdir, 'main.c')
421+
# This name causes the PathMatch::match() to iterate ~70 times, which is not unrealistic for a header file placed in subdirs.
422+
# The number of iterations also depends on how the suppressions are written.
423+
header_name = 'long_filename_to_simulate_a_more_realistic_header_placed_in_subdirs.h'
424+
header_file = os.path.join(tmpdir, header_name)
425+
suppressions_file = os.path.join(tmpdir, 'suppressions.txt')
426+
427+
# Create a main file that includes a header and returns 0.
428+
with open(filename_main, "w") as f:
429+
f.write(f'#include "{header_name}"\n\n')
430+
f.write('int main() \n')
431+
f.write('{\n')
432+
f.write(' return 0;\n')
433+
f.write('}\n')
434+
435+
# Create a header file with macro definitions that violates misra because they start with an underscore.
436+
with open(header_file, "w") as f:
437+
f.write(f'#ifndef {header_name.upper().replace(".", "_")}\n')
438+
f.write(f'#define {header_name.upper().replace(".", "_")}\n')
439+
f.write('\n')
440+
for i in range(5000):
441+
f.write(f'#define _FOO{i} {i}\n')
442+
f.write('\n')
443+
f.write(f'#endif // {header_name.upper().replace(".", "_")}\n')
444+
445+
# Create a suppressions file that suppresses the misra violation for the macros in the header file
446+
with open(suppressions_file, "w") as f:
447+
f.write(f'misra-c2012-2.5:{header_name}\n')
448+
f.write(f'misra-c2012-21.1:{header_name}\n')
449+
# Create other suppressions that don't match the file name to test that iterating the suppressions are faster with cache
450+
for i in range(300):
451+
f.write(f'misra-c2012-2.5:**/{i}/{header_name}\n')
452+
f.write(f'misra-c2012-21.1:**/{i}/{header_name}\n')
453+
454+
cppcheck([filename_main] + ['--addon=misra.py', '--check-level=exhaustive', '--enable=all', f'--suppressions-list={suppressions_file}'])

0 commit comments

Comments
 (0)