Skip to content

Instantly share code, notes, and snippets.

@absolutejam
Last active April 20, 2018 12:17
Show Gist options
  • Save absolutejam/380f6739826b627480d299bf174e47ba to your computer and use it in GitHub Desktop.
Save absolutejam/380f6739826b627480d299bf174e47ba to your computer and use it in GitHub Desktop.
Quick and dirty speed test
Function Start-SpeedTest {
param(
# How long (in minutes) to run the speedtest for
[Parameter(
Mandatory = $False
)]
[int]$Duration = 30,
# The interval (in minutes) to run the test
[Parameter(
Mandatory = $False
)]
[int]$Interval = 60,
# The log file location
[Parameter(
Mandatory = $False
)]
[string]$LogFile
)
# Must be 10MB
$TestFile = 'http://client.akamai.com/install/test-objects/10MB.bin'
$StartTime = Get-Date
$EndTime = (Get-Date).AddMinutes($Duration)
$TestFile = 'http://client.akamai.com/install/test-objects/10MB.bin'
$TempFile = Join-Path -Path $env:TEMP -ChildPath 'testfile.tmp'
$WebClient = New-Object Net.WebClient
While ((Get-Date) -lt $EndTime) {
try {
$TimeTaken = Measure-Command { $WebClient.DownloadFile($TestFile,$TempFile) } |
Select-Object -ExpandProperty TotalSeconds
$SpeedMbps = (10 / $TimeTaken) * 8
} catch {
Write-Error ("Failed to download file: {0}" -f $_.Exception.Message) -ErrorAction Continue
}
# Write & log message
$Message = "[{0}] {1:N2} Mbit/sec" -f (Get-Date -Format 'hh:mm'),
($SpeedMbps)
if ($LogFile) {
$Message | Tee-Object -FilePath $LogFile -Append
} else {
$Message | Write-Host
}
# Delete the temporary file
Remove-Item -Path $TempFile
# Wait for the length of the interval
Start-Sleep -Seconds (60 * $Interval)
}
}
Start-SpeedTest -Duration 30 -Interval 1 -LogFile "$($ENV:UserProfile)\SpeedTest.log"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment