Parallel test execution in Jenkins - Time & Space Complexity
When running tests in Jenkins, we often want to run many tests quickly. Parallel test execution helps by running tests at the same time.
We want to understand how the total time changes as we add more tests and run them in parallel.
Analyze the time complexity of the following Jenkins pipeline snippet that runs tests in parallel.
pipeline {
agent any
stages {
stage('Run Tests') {
steps {
parallel (
"Test 1": {
sh 'run_test_1.sh'
},
"Test 2": {
sh 'run_test_2.sh'
},
"Test 3": {
sh 'run_test_3.sh'
}
)
}
}
}
}
This code runs three test stages at the same time instead of one after another.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Running each test script (Test 1, Test 2, Test 3).
- How many times: Each test runs once, but all run simultaneously.
Imagine we have n tests to run.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | Runs 10 tests in parallel, total time close to longest single test. |
| 100 | Runs 100 tests in parallel, total time close to longest single test. |
| 1000 | Runs 1000 tests in parallel, total time close to longest single test. |
Pattern observation: Total time does not grow much with more tests because they run at the same time.
Time Complexity: O(1)
This means the total time to run all tests stays about the same no matter how many tests run in parallel.
[X] Wrong: "Running more tests in parallel always takes more time because there are more tests."
[OK] Correct: When tests run at the same time, total time depends mostly on the longest test, not the number of tests.
Understanding how parallel execution affects time helps you explain how to speed up processes in real projects. It shows you can think about work done at the same time versus one after another.
"What if the tests cannot run fully in parallel because of limited resources? How would the time complexity change?"
Practice
parallel block in a Jenkins pipeline?Solution
Step 1: Understand the
Theparallelblock functionparallelblock in Jenkins allows running multiple tasks simultaneously.Step 2: Identify the benefit of parallel execution
Running tests in parallel saves time by executing multiple test groups at once instead of sequentially.Final Answer:
To run multiple test groups at the same time to save time -> Option BQuick Check:
Parallel execution = run tests simultaneously [OK]
- Thinking parallel runs tests one by one
- Confusing parallel with deployment steps
- Assuming parallel stops on first failure
Solution
Step 1: Recall Jenkins parallel syntax
In declarative pipelines,paralleltakes a map of closures using scripted syntax with direct steps likeecho.Step 2: Check which option uses correct closure with direct steps
parallel( Test1: { echo 'Run Test1' }, Test2: { echo 'Run Test2' } ) correctly usesparallelwith keys and closures containing directecho.Final Answer:
parallel( Test1: { echo 'Run Test1' }, Test2: { echo 'Run Test2' } ) -> Option CQuick Check:
parallel map with direct echo closures = parallel( Test1: { echo 'Run Test1' }, Test2: { echo 'Run Test2' } ) [OK]
- Using stage inside parallel directly (wrong in scripted)
- Adding steps block inside closures (declarative syntax mismatch)
- Using braces incorrectly for parallel syntax
parallel(
TestA: {
echo 'Running Test A'
},
TestB: {
echo 'Running Test B'
}
)
echo 'All tests done'Solution
Step 1: Understand parallel execution output order
Both TestA and TestB run at the same time, so their echo outputs appear before the next command.Step 2: Identify the sequence of echo commands
After both parallel tasks finish, the final echo 'All tests done' runs.Final Answer:
Running Test A\nRunning Test B\nAll tests done -> Option AQuick Check:
Parallel tests output first, then final echo [OK]
- Assuming output order is strictly sequential
- Thinking 'All tests done' prints before tests
- Confusing parallel output interleaving
parallel(
Test1: {
stage('Test1') {
steps {
echo 'Running Test1'
}
}
},
Test2: {
stage('Test2') {
steps {
echo 'Running Test2'
}
}
}
)
What is the main cause of the error?Solution
Step 1: Identify pipeline type and syntax rules
In scripted pipelines,stageandstepsblocks are not used inside closures passed toparallel.Step 2: Recognize correct scripted pipeline parallel syntax
Scripted pipelines use simple closures with commands likeechodirectly, withoutstageorsteps.Final Answer:
Usingstageandstepsinside scripted pipeline closures -> Option DQuick Check:
Scripted parallel closures exclude stage/steps blocks [OK]
- Mixing declarative syntax inside scripted pipeline
- Forgetting commas between map entries (not the main error here)
- Assuming indentation causes syntax errors
Solution
Step 1: Recall declarative pipeline structure for parallel stages
Declarative pipelines require astagesblock containingstageblocks; parallel tests go inside a single stage with aparallelblock.Step 2: Identify correct syntax for parallel tests with failure propagation
stages { stage('Parallel Tests') { parallel { Test1: { steps { sh 'run_test1.sh' } } Test2: { steps { sh 'run_test2.sh' } } Test3: { steps { sh 'run_test3.sh' } } } } } correctly usesstagesblock with astagenamed 'Parallel Tests' containing aparallelblock where each parallel branch is a closure withstepsandshcommands. This is the proper declarative syntax and ensures the build fails if any test fails.Final Answer:
stages { stage('Parallel Tests') { parallel { Test1: { steps { sh 'run_test1.sh' } } Test2: { steps { sh 'run_test2.sh' } } Test3: { steps { sh 'run_test3.sh' } } } } } -> Option AQuick Check:
Declarative parallel inside a stage within stages block with steps closures [OK]
- Placing parallel outside stages block
- Using parallel without wrapping in a stage
- Mixing scripted and declarative syntax
Ready to prove it?
Pass the test and this pattern moves unknown
