Skip to content

Commit bef0b32

Browse files
authored
Merge ff6eb3a into 7d386ae
2 parents 7d386ae + ff6eb3a commit bef0b32

File tree

3 files changed

+181
-0
lines changed

3 files changed

+181
-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 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+
#>
10+
11+
$tests = Get-ChildItem .\tests\ -File # search for test files with specific pattern.
12+
$totalAgents = [int]$Env:SYSTEM_TOTALJOBSINPHASE # standard VSTS variables available using parallel execution; total number of parallel jobs running
13+
$agentNumber = [int]$Env:SYSTEM_JOBPOSITIONINPHASE # current job position
14+
$testCount = $tests.Count
15+
16+
# below conditions are used if parallel pipeline is not used. i.e. pipeline is running with single agent (no parallel configuration)
17+
if ($totalAgents -eq 0) {
18+
$totalAgents = 1
19+
}
20+
if (!$agentNumber -or $agentNumber -eq 0) {
21+
$agentNumber = 1
22+
}
23+
24+
Write-Host "Total agents: $totalAgents"
25+
Write-Host "Agent number: $agentNumber"
26+
Write-Host "Total tests: $testCount"
27+
28+
$testsToRun= @()
29+
30+
# slice test files to make sure each agent gets unique test file to execute
31+
For ($i=$agentNumber; $i -le $testCount;) {
32+
$file = $tests[$i-1]
33+
34+
$fileName = [System.IO.Path]::GetFileNameWithoutExtension($file.Name)
35+
$testsToRun += "$fileName/*"
36+
Write-Host "Added $file"
37+
$i = $i + $totalAgents
38+
}
39+
40+
# join all test files seperated by space.
41+
$testFiles = $testsToRun -Join " "
42+
Write-Host "Test files $testFiles"
43+
# write these files into variable so that we can run them using pytest in subsequent task.
44+
Write-Host "##vso[task.setvariable variable=MATLABTestFiles;]$testFiles"

AzureDevOps/DistributeTests.sh

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#!/bin/bash
2+
#===============================================================================
3+
#
4+
# FILE: distribute_tests.sh
5+
#
6+
# USAGE: ./distribute_tests.sh
7+
#
8+
# DESCRIPTION: This script slices tests files across multiple agents for faster execution.
9+
# We search for specific type of file structure (in this example test*), and slice them according to agent number
10+
# 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
11+
#
12+
#===============================================================================
13+
14+
tests=()
15+
while IFS= read -r file; do
16+
tests+=("$file")
17+
done < <(find ./tests -type f -name "*.m" | sort)
18+
19+
# Use Azure DevOps variables
20+
totalAgents=${SYSTEM_TOTALJOBSINPHASE}
21+
agentNumber=${SYSTEM_JOBPOSITIONINPHASE}
22+
testCount=${#tests[@]}
23+
24+
if [ $totalAgents -eq 0 ]; then
25+
totalAgents=1
26+
fi
27+
# below conditions are used if parallel pipeline is not used. i.e. pipeline is running with single agent (no parallel configuration)
28+
if [ -z $agentNumber ]; then
29+
agentNumber=1
30+
fi
31+
32+
echo "Total agents: $totalAgents"
33+
echo "Agent number: $agentNumber"
34+
echo "Total tests: $testCount"
35+
36+
testsToRun=()
37+
38+
# Slice test files so each agent gets unique files (1-based index)
39+
for (( i=agentNumber; i<=testCount; i+=totalAgents )); do
40+
file="${tests[i-1]}"
41+
42+
fileName=$(basename "$file" .m)
43+
testsToRun+=("${fileName}/*")
44+
echo "Added $fileName"
45+
done
46+
47+
# Join all test files separated by space
48+
testFiles="${testsToRun[*]}"
49+
echo "Test files $testFiles"
50+
51+
# Set as Azure Pipelines variable
52+
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)