Skip to content

Commit d2cd614

Browse files
committed
Update file
1 parent 96b217a commit d2cd614

File tree

2 files changed

+48
-44
lines changed

2 files changed

+48
-44
lines changed

AzureDevOps/DistributeTests.ps1

Lines changed: 43 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,43 @@
1-
#!/usr/bin/env bash
2-
# Distribute tests across multiple agents for parallel execution (Bash version)
3-
# Produces MATLAB cell array like:
4-
# {'/path/to/file1.m','/path/to/file2.m'}
5-
6-
# Find all test files (*.m) under ./tests
7-
mapfile -t tests < <(find ./tests -type f -name "*.m" | sort)
8-
9-
totalAgents=${CI_TOTAL:-1}
10-
agentNumber=${CI_INDEX:-1}
11-
testCount=${#tests[@]}
12-
13-
echo "Total agents: $totalAgents"
14-
echo "Agent number: $agentNumber"
15-
echo "Total tests: $testCount"
16-
17-
testsToRun=()
18-
19-
# Slice test files so each agent gets unique tests
20-
for (( i=agentNumber; i<=testCount; i+=totalAgents )); do
21-
file="${tests[i-1]}"
22-
testsToRun+=("$file")
23-
echo "Added $file"
24-
done
25-
26-
# Format as MATLAB cell array: {'file1','file2'}
27-
joined="{"
28-
for idx in "${!testsToRun[@]}"; do
29-
if [[ $idx -gt 0 ]]; then
30-
joined+=","
31-
fi
32-
joined+="'${testsToRun[$idx]}'"
33-
done
34-
joined+="}"
35-
36-
echo "Final test file list (MATLAB cell array): $joined"
37-
38-
# Set as Azure Pipelines variable
39-
echo "##vso[task.setvariable variable=MATLABTestFiles;]$joined"
1+
<#
2+
.SYNOPSIS
3+
Distribute the tests in VSTS pipeline across multiple agents
4+
.DESCRIPTION
5+
This script slices tests files across multiple agents for faster execution.
6+
We search for specific type of file structure (in this example test*), and slice them according to agent number
7+
If we encounter multiple files [file1..file10] and if we have 2 agents, agent1 executes tests odd number of files while agent2 executes even number of files
8+
For detalied slicing info: https://docs.microsoft.com/en-us/vsts/pipelines/test/parallel-testing-any-test-runner
9+
We use JUnit style test results to publish the test reports.
10+
#>
11+
12+
$tests = Get-ChildItem .\ -Filter "test*" # search for test files with specific pattern.
13+
$totalAgents = [int]$Env:SYSTEM_TOTALJOBSINPHASE # standard VSTS variables available using parallel execution; total number of parallel jobs running
14+
$agentNumber = [int]$Env:SYSTEM_JOBPOSITIONINPHASE # current job position
15+
$testCount = $tests.Count
16+
17+
# below conditions are used if parallel pipeline is not used. i.e. pipeline is running with single agent (no parallel configuration)
18+
if ($totalAgents -eq 0) {
19+
$totalAgents = 1
20+
}
21+
if (!$agentNumber -or $agentNumber -eq 0) {
22+
$agentNumber = 1
23+
}
24+
25+
Write-Host "Total agents: $totalAgents"
26+
Write-Host "Agent number: $agentNumber"
27+
Write-Host "Total tests: $testCount"
28+
29+
$testsToRun= @()
30+
31+
# slice test files to make sure each agent gets unique test file to execute
32+
For ($i=$agentNumber; $i -le $testCount;) {
33+
$file = $tests[$i-1]
34+
$testsToRun = $testsToRun + $file
35+
Write-Host "Added $file"
36+
$i = $i + $totalAgents
37+
}
38+
39+
# join all test files seperated by space. pytest runs multiple test files in following format pytest test1.py test2.py test3.py
40+
$testFiles = $testsToRun -Join " "
41+
Write-Host "Test files $testFiles"
42+
# write these files into variable so that we can run them using pytest in subsequent task.
43+
Write-Host "##vso[task.setvariable variable=pytestfiles;]$testFiles"

AzureDevOps/ParallelStrategy.yml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,15 @@ jobs:
1212
release: R2025a
1313
products: MATLAB_Test Simulink_Test
1414

15-
- powershell: .\AzureDevOps\DistributeTests.ps1
15+
- powershell: .\AzureDevOps\DistributeTests.ps1
1616
displayName: 'PowerShell Script to distribute tests'
1717

1818
- task: RunMATLABTests@1
1919
inputs:
2020
selectByName: $(MATLABTestFiles)
21-
sourceFolder:
21+
sourceFolder: src
2222
env:
23-
MLM_LICENSE_TOKEN: $(MLM_LICENSE_TOKEN)
23+
MLM_LICENSE_TOKEN: $(MLM_LICENSE_TOKEN)
2424

2525
- job: ParallelLinux
2626
# Parallel strategy to run tests in parallel
@@ -41,7 +41,7 @@ jobs:
4141
- task: RunMATLABTests@1
4242
inputs:
4343
selectByName: $(MATLABTestFiles)
44-
sourceFolder:
44+
sourceFolder: src
4545
env:
4646
MLM_LICENSE_TOKEN: $(MLM_LICENSE_TOKEN)
4747

@@ -64,6 +64,6 @@ jobs:
6464
- task: RunMATLABTests@1
6565
inputs:
6666
selectByName: $(MATLABTestFiles)
67-
sourceFolder:
67+
sourceFolder: src
6868
env:
6969
MLM_LICENSE_TOKEN: $(MLM_LICENSE_TOKEN)

0 commit comments

Comments
 (0)