Skip to content

Instantly share code, notes, and snippets.

@fdcastel
Last active November 11, 2024 05:03
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://1.1.1.1/cdn-cgi/trace', # 13,86
'https://myip.dnsomatic.com/', # 17,45
'https://icanhazip.com', # 23,96
'https://checkip.amazonaws.com/', # 147,48
'https://ipinfo.io/ip' # 163,43
# Number of iterations
$MAX_STEPS = 5
# 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