Skip to content

Instantly share code, notes, and snippets.

@fdcastel
Created September 27, 2023 16:19
Show Gist options
  • Save fdcastel/b5250cb47b86a53f394443cdaf44b940 to your computer and use it in GitHub Desktop.
Save fdcastel/b5250cb47b86a53f394443cdaf44b940 to your computer and use it in GitHub Desktop.
Test response times of public "what is my ip" services.
[CmdletBinding(SupportsShouldProcess)]
Param()
#
# Settings
#
# Services to test
$urls = 'https://ifconfig.me/ip',
'https://1.1.1.1/cdn-cgi/trace',
'https://checkip.amazonaws.com/',
'https://domains.google.com/checkip'
# Number of iterations
$MAX_STEPS = 10
# Number of values to discard from lower/higher results
$OUTLIERS = 1
#
# Functions
#
function Get-ResponseTimes {
$urls | Foreach-Object {
$m = Measure-Command { Invoke-RestMethod $_ -Verbose:$false }
@{ $_ = $m.TotalMilliseconds }
}
}
#
# Main
#
# Initialize allResponses with an empty array for each service
$allResponses = @{}
$urls | ForEach-Object {
$allResponses[$_] = @() # empty array
}
# Cold run: Ignore
Write-Verbose 'Warming up...'
Get-ResponseTimes | Out-Null
# Warm run
$step = 1
while ($step -le $MAX_STEPS) {
Write-Verbose "Running step $step of $MAX_STEPS..."
$response = Get-ResponseTimes
# For each service, append response time of this iteration to array
$response.Keys | ForEach-Object {
$service = $_
$serviceResponseTime = $response.$_
$allResponses[$service] += ,$serviceResponseTime
}
$step++
}
# Final output
$result = @{}
$allResponses.Keys | ForEach-Object {
$service = $_
$serviceResponseTimes = $allResponses[$_]
# Sort response times, discard outliers and get the average
$serviceAverage = $serviceResponseTimes |
Sort-Object |
Select-Object -Skip $OUTLIERS |
Select-Object -SkipLast $OUTLIERS |
Measure-Object -Average |
Select-Object -ExpandProperty 'Average'
$result[$service] = $serviceAverage
}
# Final output
$result
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment