Why scripted pipelines offer flexibility in Jenkins - Performance Analysis
We want to understand how the time it takes to run a scripted Jenkins pipeline changes as the pipeline grows.
Specifically, how does adding more steps or loops affect execution time?
Analyze the time complexity of the following scripted pipeline snippet.
node {
for (int i = 0; i < stages.size(); i++) {
stage(stages[i]) {
echo "Running stage: ${stages[i]}"
// some build steps here
}
}
}
This code runs a list of stages one by one, executing build steps inside each stage.
Look for loops or repeated actions.
- Primary operation: The for-loop that runs each stage.
- How many times: Once for each stage in the stages list.
As the number of stages increases, the total steps grow proportionally.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 stage executions |
| 100 | 100 stage executions |
| 1000 | 1000 stage executions |
Pattern observation: The time grows directly with the number of stages.
Time Complexity: O(n)
This means the total time increases in a straight line as you add more stages.
[X] Wrong: "Adding more stages won't affect the total time much because they run fast."
[OK] Correct: Each stage adds its own work, so more stages mean more total time.
Understanding how pipeline steps add up helps you design efficient builds and explain your choices clearly.
"What if we nested another loop inside each stage? How would the time complexity change?"