|
32 | 32 | public abstract class Scanner { |
33 | 33 | private static final Logger LOG = LoggerFactory.getLogger(Scanner.class); |
34 | 34 | private static final String FAIL_FAST_PROPERTY_NAME = "sonar.internal.analysis.failFast"; |
| 35 | + private static final int MAX_NUMBER_OF_THREADS = 6; |
35 | 36 | public static final String THREADS_PROPERTY_NAME = "sonar.python.analysis.threads"; |
| 37 | + public static final String PARALLEL_PROPERTY_NAME = "sonar.python.analysis.parallel"; |
36 | 38 | protected final SensorContext context; |
37 | 39 |
|
38 | 40 | protected Scanner(SensorContext context) { |
@@ -65,8 +67,7 @@ protected void processFiles(List<PythonInputFile> files, SensorContext context, |
65 | 67 | var allTasks = CompletableFuture.allOf( |
66 | 68 | files.stream() |
67 | 69 | .map(file -> CompletableFuture.runAsync(() -> processFile(context, file, progressReport, numScannedWithoutParsing), executor)) |
68 | | - .toArray(CompletableFuture[]::new) |
69 | | - ); |
| 70 | + .toArray(CompletableFuture[]::new)); |
70 | 71 | allTasks.join(); |
71 | 72 | } catch (CompletionException e) { |
72 | 73 | var cause = e.getCause(); |
@@ -143,15 +144,18 @@ private static boolean isParseErrorOnTestFile(PythonInputFile file, Exception e) |
143 | 144 | } |
144 | 145 |
|
145 | 146 | protected int getNumberOfThreads(SensorContext context) { |
146 | | - int minNumOfThreads = 1; |
147 | | - int maxNumOfThreads = 6; |
148 | | - int availableProcessors = (int) Math.round(Runtime.getRuntime().availableProcessors() * 0.9); |
149 | | - |
150 | | - // Disabling parallelization if threads property is not setup properly |
151 | | - return context.config() |
152 | | - .getInt(THREADS_PROPERTY_NAME) |
153 | | - .map(threads -> threads < 1 ? 1 : threads) |
154 | | - .orElse(Math.max(minNumOfThreads, Math.min(availableProcessors, maxNumOfThreads))); |
| 147 | + boolean isParallelizationEnabled = context.config().getBoolean(PARALLEL_PROPERTY_NAME).orElse(true); |
| 148 | + if (isParallelizationEnabled) { |
| 149 | + int minNumOfThreads = 1; |
| 150 | + int availableProcessors = (int) Math.round(Runtime.getRuntime().availableProcessors() * 0.9); |
| 151 | + |
| 152 | + // Disabling parallelization if threads property is not setup properly |
| 153 | + return context.config() |
| 154 | + .getInt(THREADS_PROPERTY_NAME) |
| 155 | + .map(threads -> threads < 1 ? 1 : threads) |
| 156 | + .orElse(Math.max(minNumOfThreads, Math.min(availableProcessors, MAX_NUMBER_OF_THREADS))); |
| 157 | + } |
| 158 | + return 1; |
155 | 159 | } |
156 | 160 |
|
157 | 161 | } |
0 commit comments