Skip to content

Commit 3cb3ae5

Browse files
authored
Merge 9bf7953 into 7d386ae
2 parents 7d386ae + 9bf7953 commit 3cb3ae5

File tree

3 files changed

+183
-0
lines changed

3 files changed

+183
-0
lines changed

AzureDevOps/DistributeTests.ps1

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<#
2+
.SYNOPSIS
3+
Distribute tests across multiple agents in VSTS pipeline
4+
.DESCRIPTION
5+
This script divides test files among multiple agents for faster execution. It searches for files matching a specific pattern (for example, `test*`) and assigns them based on the agent number.
6+
For example, if there are multiple files [test1..test10] and 2 agents:
7+
- Agent 1 runs tests from odd-numbered files.
8+
- Agent 2 runs tests from even-numbered files.
9+
For detailed slicing information, see https://docs.microsoft.com/en-us/vsts/pipelines/test/parallel-testing-any-test-runner
10+
#>
11+
12+
$tests = Get-ChildItem .\tests\ -Filter *.m -File # Search for test files matching the specified pattern
13+
$totalAgents = [int]$Env:SYSTEM_TOTALJOBSINPHASE # Standard VSTS variable containing the number of parallel jobs
14+
$agentNumber = [int]$Env:SYSTEM_JOBPOSITIONINPHASE # Current job position
15+
$testCount = $tests.Count
16+
17+
# Handle cases where the pipeline runs without parallel configuration (single agent)
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 so each agent gets a unique set of files to execute
32+
For ($i=$agentNumber; $i -le $testCount;) {
33+
$file = $tests[$i-1]
34+
35+
$fileName = [System.IO.Path]::GetFileNameWithoutExtension($file.Name)
36+
$testsToRun += "$fileName/*"
37+
$i = $i + $totalAgents
38+
}
39+
40+
# Join all test files into a space-separated string
41+
$testFiles = $testsToRun -Join " "
42+
Write-Host "Tests to run $testFiles"
43+
# Write files into a variable for execution in a subsequent task
44+
Write-Host "##vso[task.setvariable variable=MATLABTestFiles;]$testFiles"

AzureDevOps/DistributeTests.sh

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#!/bin/bash
2+
#===============================================================================
3+
#
4+
# FILE: distribute_tests.sh
5+
#
6+
# USAGE: ./distribute_tests.sh
7+
#
8+
# DESCRIPTION: This script divides test files among multiple agents for faster execution. It searches for files matching a specific pattern (for example, `test*`) and assigns them based on the agent number.
9+
# For example, if there are multiple files [test1..test10] and 2 agents:
10+
# - Agent 1 runs tests from odd-numbered files.
11+
# - Agent 2 runs tests from even-numbered files.
12+
# For detailed slicing information, see https://docs.microsoft.com/en-us/vsts/pipelines/test/parallel-testing-any-test-runner
13+
#
14+
#===============================================================================
15+
16+
tests=()
17+
# Search for test files matching the specified pattern
18+
while IFS= read -r file; do
19+
tests+=("$file")
20+
done < <(find ./tests -type f -name "*.m" | sort)
21+
22+
# Use Azure DevOps variables
23+
totalAgents=${SYSTEM_TOTALJOBSINPHASE} # Standard VSTS variable containing the number of parallel jobs
24+
agentNumber=${SYSTEM_JOBPOSITIONINPHASE} # Current job positioN
25+
testCount=${#tests[@]}
26+
27+
# Handle cases where the pipeline runs without parallel configuration (single agent)
28+
if [ $totalAgents -eq 0 ]; then
29+
totalAgents=1
30+
fi
31+
if [ -z $agentNumber ]; then
32+
agentNumber=1
33+
fi
34+
35+
echo "Total agents: $totalAgents"
36+
echo "Agent number: $agentNumber"
37+
echo "Total tests: $testCount"
38+
39+
testsToRun=()
40+
41+
# Slice test files so each agent gets a unique set of files
42+
for (( i=agentNumber; i<=testCount; i+=totalAgents )); do
43+
file="${tests[i-1]}"
44+
45+
fileName=$(basename "$file" .m)
46+
testsToRun+=("${fileName}/*")
47+
done
48+
49+
# Join all test files into a space-separated string
50+
testFiles="${testsToRun[*]}"
51+
echo "Tests to run $testFiles"
52+
53+
# Write files into a variable for execution in a subsequent task
54+
echo "##vso[task.setvariable variable=MATLABTestFiles;]$testFiles"

AzureDevOps/ParallelStrategy.yml

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
jobs:
2+
- job: ParallelWindows
3+
# Parallel strategy to run tests in parallel
4+
strategy:
5+
parallel: 2
6+
pool:
7+
vmImage: windows-latest
8+
steps:
9+
# Install MATLAB and required products
10+
- task: InstallMATLAB@1
11+
inputs:
12+
products: MATLAB_Compiler_SDK MATLAB_Test
13+
14+
- task: RunMATLABBuild@1
15+
inputs:
16+
tasks: equivalenceTest
17+
env:
18+
MLM_LICENSE_TOKEN: $(MLM_LICENSE_TOKEN)
19+
20+
- powershell: .\AzureDevOps\DistributeTests.ps1
21+
displayName: 'PowerShell Script to distribute tests'
22+
23+
- task: RunMATLABTests@1
24+
inputs:
25+
selectByName: $(MATLABTestFiles)
26+
sourceFolder: src
27+
env:
28+
MLM_LICENSE_TOKEN: $(MLM_LICENSE_TOKEN)
29+
30+
- job: ParallelLinux
31+
# Parallel strategy to run tests in parallel
32+
strategy:
33+
parallel: 2
34+
pool:
35+
vmImage: ubuntu-latest
36+
steps:
37+
# Install MATLAB and required products
38+
- task: InstallMATLAB@1
39+
inputs:
40+
products: MATLAB_Compiler_SDK MATLAB_Test
41+
42+
# Builds Python package from MATLAB function and verifies functionality through equivalence tests
43+
- task: RunMATLABBuild@1
44+
inputs:
45+
tasks: equivalenceTest
46+
env:
47+
MLM_LICENSE_TOKEN: $(MLM_LICENSE_TOKEN)
48+
49+
- bash: chmod +x ./AzureDevOps/DistributeTests.sh && ./AzureDevOps/DistributeTests.sh
50+
displayName: 'Bash Script to distribute tests'
51+
52+
- task: RunMATLABTests@1
53+
inputs:
54+
selectByName: $(MATLABTestFiles)
55+
sourceFolder: src
56+
env:
57+
MLM_LICENSE_TOKEN: $(MLM_LICENSE_TOKEN)
58+
59+
- job: ParallelMac
60+
# Parallel strategy to run tests in parallel
61+
strategy:
62+
parallel: 2
63+
pool:
64+
vmImage: macOS-latest
65+
steps:
66+
# Install MATLAB and required products
67+
- task: InstallMATLAB@1
68+
inputs:
69+
products: MATLAB_Compiler_SDK MATLAB_Test
70+
71+
- task: RunMATLABBuild@1
72+
inputs:
73+
tasks: equivalenceTest
74+
env:
75+
MLM_LICENSE_TOKEN: $(MLM_LICENSE_TOKEN)
76+
77+
- bash: chmod +x ./AzureDevOps/DistributeTests.sh && ./AzureDevOps/DistributeTests.sh
78+
displayName: 'Bash Script to distribute tests'
79+
80+
- task: RunMATLABTests@1
81+
inputs:
82+
selectByName: $(MATLABTestFiles)
83+
sourceFolder: src
84+
env:
85+
MLM_LICENSE_TOKEN: $(MLM_LICENSE_TOKEN)

0 commit comments

Comments
 (0)