Last active
September 4, 2019 21:26
-
-
Save leandrosilva/e2f9777e221f9788e6af4243d91e37ea to your computer and use it in GitHub Desktop.
PowerShell script to fire some URLs to get them warmed up
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
param ( | |
[string] $NamePattern = "Dummy-web-app-*", | |
[int] $Iterations = 2 | |
) | |
function Get-WebInstances () { | |
$res = $(aws ec2 describe-instances --region us-east-1 --filters "Name=tag:Name,Values=$NamePattern") | |
$json = $res | ConvertFrom-Json | |
$instances = $json.Reservations.Instances ` | |
| Where-Object { $_.State.Name -eq "Running" } ` | |
| Select-Object PublicDnsName, ` | |
@{Label="Tags"; Expression={$_.Tags | Where-Object { $_.Key -eq "Name"}}} | |
return $instances | |
} | |
function Get-WebInstanceUris($WebDnss) { | |
$uris = @() | |
foreach ($web_dns in $WebDnss) { | |
$uris += "http://${web_dns}:8000" | |
} | |
return $uris | |
} | |
function Request-PrinterPage($Uri) { | |
$targets = @("/some/damn/eager/resource", | |
"/other/damn/eager/resource") | |
foreach ($target in $targets) { | |
$targetUri = $uri + $target | |
$res = Invoke-WebRequest -Uri $targetUri | |
$status = $res.StatusCode | |
"- ${targetUri} (${status})" | |
} | |
"" | |
} | |
function Request-NPrinterPage($Uris) { | |
foreach ($uri in $Uris) { | |
Request-PrinterPage -Uri $uri | |
} | |
} | |
### | |
"Warming up web server intances..." | |
"- NamePattern: $NamePattern" | |
$web_instances = Get-WebInstances | |
"" | |
$web_dnss = $web_instances.PublicDnsName | |
$web_uris = Get-WebInstanceUris -WebDnss $web_dnss | |
if (!$web_uris) { | |
"Failed to infer Web instances URI from pattern" | |
exit | |
} | |
for ($i = 1; $i -lt ($Iterations + 1); $i++) { | |
"Iteration ${i}:" | |
Request-NPrinterPage -Uris $web_uris | |
} | |
"Fairly warm!" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment