core/Resources/Scripts/test_stress.ps1
Param( [Parameter(Mandatory)] [string]$EnvironmentName, [Parameter(Mandatory)] [string]$CacheName, [string]$Server, [int]$DataSize, [int]$ItemsCount = 5000, [int]$SlidingExpiration, [int]$TestCaseIterationDelay, [int]$TestCaseIterations, [int]$ThreadCount, [int]$UpdatesPerIteration, [pscredential]$Credentials, [switch]$NoLogo ) function GetUploadedVmNames { param( [Parameter(Mandatory)][string]$ResourceGroup ) return Get-AzVM -ResourceGroupName $ResourceGroup -ErrorAction Stop | Where-Object { $_.Tags.ContainsKey("Caches") } | Select-Object -ExpandProperty Name } function InvokeCommandOnServerWindows { param( [string]$ResourceGroupName, [string[]]$VmNames ) $command = "Test-Stress -CacheName $CacheName" if (![string]::IsNullOrWhiteSpace($Server)) { $command += " -Server `"$Server`"" } if ($DataSize) { $command += " -DataSize $DataSize" } if ($ItemsCount) { $command += " -ItemsCount $ItemsCount" } if ($SlidingExpiration) { $command += " -SlidingExpiration $SlidingExpiration" } if ($TestCaseIterationDelay) { $command += " -TestCaseIterationDelay $TestCaseIterationDelay" } if ($TestCaseIterations) { $command += " -TestCaseIterations $TestCaseIterations" } if ($ThreadCount) { $command += " -ThreadCount $ThreadCount" } if ($UpdatesPerIteration) { $command += " -UpdatesPerIteration $UpdatesPerIteration" } if ($Credentials) { $command += " -Credentials `"$($Credentials.GetNetworkCredential().UserName):$($Credentials.GetNetworkCredential().Password)`"" } if ($NoLogo) { $command += " -NoLogo" } $script = @" $command > output.txt Write-Output (Get-Content output.txt) Remove-Item output.txt "@ $jobs = @() foreach ($vm in $VmNames) { $jobs += Start-Job -ScriptBlock { param($ResourceGroupName, $vm, $script) $result = Invoke-AzVMRunCommand -ResourceGroupName $ResourceGroupName -VMName $vm -CommandId 'RunPowerShellScript' -ScriptString $script $messages = $result.Value | ForEach-Object { $_.Message } $joined = ($messages -join "`n").Trim() $joined } -ArgumentList $ResourceGroupName, $vm, $script # if server param is provided, exute on only one server if (![string]::IsNullOrWhiteSpace($Server)) { break } } $jobs | Wait-Job | Out-Null $jobs | Receive-Job } function InvokeCommandOnServerLinux { param( [string]$ResourceGroupName, [string[]]$VmNames ) $command = "/opt/ncache/bin/tools/test-stress -cachename $CacheName" if (![string]::IsNullOrWhiteSpace($Server)) { $command += " -server `"$Server`"" } if ($DataSize) { $command += " -datasize $DataSize" } if ($ItemsCount) { $command += " -itemscount $ItemsCount" } if ($SlidingExpiration) { $command += " -slidingexpiration $SlidingExpiration" } if ($TestCaseIterationDelay) { $command += " -testcaseiterationdelay $TestCaseIterationDelay" } if ($TestCaseIterations) { $command += " -testcaseiterations $TestCaseIterations" } if ($ThreadCount) { $command += " -threadcount $ThreadCount" } if ($UpdatesPerIteration) { $command += " -updatesperiteration $UpdatesPerIteration" } if ($Credentials) { $command += " -credentials `"$($Credentials.GetNetworkCredential().UserName):$($Credentials.GetNetworkCredential().Password)`"" } if ($NoLogo) { $command += " -nologo" } $script = @" $command > output.txt cat output.txt rm -f output.txt "@ $jobs = @() foreach ($vm in $VmNames) { $jobs += Start-Job -ScriptBlock { param($ResourceGroupName, $vm, $script) $result = Invoke-AzVMRunCommand -ResourceGroupName $ResourceGroupName -VMName $vm -CommandId 'RunShellScript' -ScriptString $script $messages = $result.Value | ForEach-Object { $_.Message } $joined = ($messages -join "`n").Trim() $joined } -ArgumentList $ResourceGroupName, $vm, $script # if server param is provided, exute on only one server if (![string]::IsNullOrWhiteSpace($Server)) { break } } $jobs | Wait-Job | Out-Null $jobs | Receive-Job } function InvokeCommandOnVms { param( [object]$Resource, [string]$ResourceGroupName, [string[]]$VmNames ) $os = $Resource.Tags['OsType'] if ($os -eq "Windows") { InvokeCommandOnServerWindows -ResourceGroupName $ResourceGroupName -VmNames $VmNames } else { InvokeCommandOnServerLinux -ResourceGroupName $ResourceGroupName -VmNames $VmNames } } function ExecuteCommands { $resource = Get-AzResourceGroup -ErrorAction Stop | Where-Object { $_.Tags -and $_.Tags.Contains("EnvironmentName") -and $_.Tags["EnvironmentName"] -eq $EnvironmentName } if (-not $resource) { throw "No such environment exists" } $resourceGroupName = $resource.ResourceGroupName $vmNames = GetUploadedVmNames -ResourceGroup $resourceGroupName InvokeCommandOnVms -Resource $resource -ResourceGroupName $resourceGroupName -VmNames $vmNames } try { if (-not (Get-AzContext)) { Connect-AzAccount if (Get-AzContext) { ExecuteCommands } } else { ExecuteCommands } } catch { Write-Error $($_.Exception.Message) } |