-
Notifications
You must be signed in to change notification settings - Fork 6
Adding example for running tests in parallel in Azure #8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 23 commits
96b217a
d2cd614
e03c52f
982dd65
91fd679
76eeb0c
92bc54f
1f07238
b1c0c8e
02db0b3
12b8bec
956ab1e
2e6d8ca
fc5d5a7
9385b68
085e078
3097975
4758771
45ef02b
69d39c2
8258dbc
034ad30
ff6eb3a
9bf7953
4aeabe0
13e8cb1
8f89dca
d819763
e37a140
66f0350
04de8cf
3cd4074
6d6210e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| <# | ||
| .SYNOPSIS | ||
| Distribute the tests in VSTS pipeline across multiple agents | ||
mw-hrastega marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| .DESCRIPTION | ||
mw-hrastega marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| This script slices tests files across multiple agents for faster execution. | ||
| We search for specific type of file structure (in this example test*), and slice them according to agent number | ||
| 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 | ||
| For detalied slicing info: https://docs.microsoft.com/en-us/vsts/pipelines/test/parallel-testing-any-test-runner | ||
| #> | ||
|
|
||
| $tests = Get-ChildItem .\tests\ -File # search for test files with specific pattern. | ||
mw-hrastega marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| $totalAgents = [int]$Env:SYSTEM_TOTALJOBSINPHASE # standard VSTS variables available using parallel execution; total number of parallel jobs running | ||
mw-hrastega marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| $agentNumber = [int]$Env:SYSTEM_JOBPOSITIONINPHASE # current job position | ||
mw-hrastega marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| $testCount = $tests.Count | ||
|
|
||
| # below conditions are used if parallel pipeline is not used. i.e. pipeline is running with single agent (no parallel configuration) | ||
mw-hrastega marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| if ($totalAgents -eq 0) { | ||
| $totalAgents = 1 | ||
| } | ||
| if (!$agentNumber -or $agentNumber -eq 0) { | ||
| $agentNumber = 1 | ||
| } | ||
|
|
||
| Write-Host "Total agents: $totalAgents" | ||
| Write-Host "Agent number: $agentNumber" | ||
| Write-Host "Total tests: $testCount" | ||
|
|
||
| $testsToRun= @() | ||
|
|
||
| # slice test files to make sure each agent gets unique test file to execute | ||
mw-hrastega marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| For ($i=$agentNumber; $i -le $testCount;) { | ||
| $file = $tests[$i-1] | ||
|
|
||
| $fileName = [System.IO.Path]::GetFileNameWithoutExtension($file.Name) | ||
| $testsToRun += "$fileName/*" | ||
| Write-Host "Added $file" | ||
| $i = $i + $totalAgents | ||
| } | ||
|
|
||
| # join all test files seperated by space. | ||
mw-hrastega marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| $testFiles = $testsToRun -Join " " | ||
| Write-Host "Test files $testFiles" | ||
| # write these files into variable so that we can run them using pytest in subsequent task. | ||
mw-hrastega marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| Write-Host "##vso[task.setvariable variable=MATLABTestFiles;]$testFiles" | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| #!/bin/bash | ||
mw-hrastega marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| #=============================================================================== | ||
| # | ||
| # FILE: distribute_tests.sh | ||
| # | ||
| # USAGE: ./distribute_tests.sh | ||
| # | ||
| # DESCRIPTION: This script slices tests files across multiple agents for faster execution. | ||
mw-hrastega marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| # We search for specific type of file structure (in this example test*), and slice them according to agent number | ||
| # 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 | ||
| # | ||
| #=============================================================================== | ||
|
|
||
| tests=() | ||
| while IFS= read -r file; do | ||
| tests+=("$file") | ||
| done < <(find ./tests -type f -name "*.m" | sort) | ||
|
|
||
| # Use Azure DevOps variables | ||
| totalAgents=${SYSTEM_TOTALJOBSINPHASE} | ||
| agentNumber=${SYSTEM_JOBPOSITIONINPHASE} | ||
| testCount=${#tests[@]} | ||
|
|
||
| if [ $totalAgents -eq 0 ]; then | ||
| totalAgents=1 | ||
| fi | ||
| # below conditions are used if parallel pipeline is not used. i.e. pipeline is running with single agent (no parallel configuration) | ||
mw-hrastega marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| if [ -z $agentNumber ]; then | ||
| agentNumber=1 | ||
| fi | ||
|
|
||
| echo "Total agents: $totalAgents" | ||
| echo "Agent number: $agentNumber" | ||
| echo "Total tests: $testCount" | ||
|
|
||
| testsToRun=() | ||
|
|
||
| # Slice test files so each agent gets unique files (1-based index) | ||
mw-hrastega marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| for (( i=agentNumber; i<=testCount; i+=totalAgents )); do | ||
| file="${tests[i-1]}" | ||
|
|
||
| fileName=$(basename "$file" .m) | ||
| testsToRun+=("${fileName}/*") | ||
| echo "Added $fileName" | ||
| done | ||
|
|
||
| # Join all test files separated by space | ||
mw-hrastega marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| testFiles="${testsToRun[*]}" | ||
| echo "Test files $testFiles" | ||
|
|
||
| # Set as Azure Pipelines variable | ||
mw-hrastega marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| echo "##vso[task.setvariable variable=MATLABTestFiles;]$testFiles" | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| jobs: | ||
| - job: ParallelWindows | ||
| # Parallel strategy to run tests in parallel | ||
mw-hrastega marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| strategy: | ||
| parallel: 2 | ||
| pool: | ||
| vmImage: windows-latest | ||
| steps: | ||
| # Install MATLAB and required products | ||
| - task: InstallMATLAB@1 | ||
| inputs: | ||
| products: MATLAB_Compiler_SDK MATLAB_Test | ||
|
|
||
| - task: RunMATLABBuild@1 | ||
| inputs: | ||
| tasks: equivalenceTest | ||
| env: | ||
| MLM_LICENSE_TOKEN: $(MLM_LICENSE_TOKEN) | ||
|
Comment on lines
14
to
18
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Aren't these tests being split and run again below?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The tests needs some artifacts which are generated by a task in buildfile. I updated the task to 'buildPythonPackage' which generates the artifact. |
||
|
|
||
| - powershell: .\AzureDevOps\DistributeTests.ps1 | ||
| displayName: 'PowerShell Script to distribute tests' | ||
mw-hrastega marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| - task: RunMATLABTests@1 | ||
| inputs: | ||
| selectByName: $(MATLABTestFiles) | ||
| sourceFolder: src | ||
| env: | ||
| MLM_LICENSE_TOKEN: $(MLM_LICENSE_TOKEN) | ||
|
|
||
| - job: ParallelLinux | ||
| # Parallel strategy to run tests in parallel | ||
mw-hrastega marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| strategy: | ||
| parallel: 2 | ||
| pool: | ||
| vmImage: ubuntu-latest | ||
| steps: | ||
| # Install MATLAB and required products | ||
| - task: InstallMATLAB@1 | ||
| inputs: | ||
| products: MATLAB_Compiler_SDK MATLAB_Test | ||
|
|
||
| # Builds Python package from MATLAB function and verifies functionality through equivalence tests | ||
mw-hrastega marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| - task: RunMATLABBuild@1 | ||
| inputs: | ||
| tasks: equivalenceTest | ||
| env: | ||
| MLM_LICENSE_TOKEN: $(MLM_LICENSE_TOKEN) | ||
|
|
||
| - bash: chmod +x ./AzureDevOps/DistributeTests.sh && ./AzureDevOps/DistributeTests.sh | ||
| displayName: 'Bash Script to distribute tests' | ||
mw-hrastega marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| - task: RunMATLABTests@1 | ||
| inputs: | ||
| selectByName: $(MATLABTestFiles) | ||
| sourceFolder: src | ||
| env: | ||
| MLM_LICENSE_TOKEN: $(MLM_LICENSE_TOKEN) | ||
|
|
||
| - job: ParallelMac | ||
| # Parallel strategy to run tests in parallel | ||
mw-hrastega marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| strategy: | ||
| parallel: 2 | ||
| pool: | ||
| vmImage: macOS-latest | ||
| steps: | ||
| # Install MATLAB and required products | ||
| - task: InstallMATLAB@1 | ||
| inputs: | ||
| products: MATLAB_Compiler_SDK MATLAB_Test | ||
|
|
||
| - task: RunMATLABBuild@1 | ||
| inputs: | ||
| tasks: equivalenceTest | ||
| env: | ||
| MLM_LICENSE_TOKEN: $(MLM_LICENSE_TOKEN) | ||
|
|
||
| - bash: chmod +x ./AzureDevOps/DistributeTests.sh && ./AzureDevOps/DistributeTests.sh | ||
| displayName: 'Bash Script to distribute tests' | ||
mw-hrastega marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| - task: RunMATLABTests@1 | ||
| inputs: | ||
| selectByName: $(MATLABTestFiles) | ||
| sourceFolder: src | ||
| env: | ||
| MLM_LICENSE_TOKEN: $(MLM_LICENSE_TOKEN) | ||
Uh oh!
There was an error while loading. Please reload this page.