PowerShell/Community-Blog

Request: Check download speed

pawanadubey opened this issue ยท 4 comments

Check download speed of connected internet

Many times we are needed to run a speed test on machines to verify that machines have the acceptable speed to use certain applications effectively.

The goal is to

  • Download a zip file
  • Calculate the time take to download the file
  • Repeat the task for 3 times
  • Get the average time to guess the download speed

PowerShell command to explain: Invoke-WebRequest

Great idea-some thoughts.

  1. Do we need to use Invoke-WebRequest? Personally, I kind of like using Start-BitsTransfer as it's simpler to explain and uses a few less lines of code to do the gransfer.
  2. How big a download will do? Do we want to have a 1mb file, a 100 mb file, etc? How about timing the download of a song or two? How about timing the download of this live concert file. That is the opening number of a 1974 Grateful Dead show.
  3. How do we handle CDNs and the global web. If I kick thi soff in the UK, do I test a download FROM the UK, or from EUrope, or the US, or???

Here is a simple suggestion - comments?

# 1. Defining the file to be tested
$FILE = 'https://archive.org/download/gd74-07-21.' +
        'fob-patched.miller.31436.sbeok.flacf/gd74-07-21d1t01.flac'
$DEST = 'C:\Foo\TPL.Flac'        
"Testing a download of [$FILE]"        
"Storing at [$DEST]"

# 2. Timing the download
$Start = Get-Date
Start-BitsTransfer -Source $FILE -Destination c:\foo\TPL.FLAC
$End = Get-Date

# 3. Calculating the time taken
$TimeTaken = $End-$Start

# 4. Displaying time taken
$Seconds = $TimeTaken.TotalSeconds
"Total seconds taken to perform download [$($TimeTaken.TotalSeconds)"

# 5. Calculate bytes/sec
$Size = (Get-ChildItem -Path $DEST).Length
$Speed = ($Size/$Seconds).ToString('N2')

# 6. Display the speed achieved
"Download speed (bytes/second) [$Speed]"

@doctordns Your code snippet is a good starting point. ๐Ÿ‘

I took it and tried to use the desired Invoke-WebRequest instead of Start-BitsTransfer, as I'm more familiar with Invoke-WebRequest.

Actually, Invoke-WebRequest is not that difficult to explain.
It works when URI and Outfile are specified.

# 1. Defining the file to be tested
$FILE = 'https://archive.org/download/gd74-07-21.' +
        'fob-patched.miller.31436.sbeok.flacf/gd74-07-21d1t01.flac'
$DEST = 'C:\Foo\TPL.Flac'        
"Testing a download of [$FILE]"        
"Storing at [$DEST]"

# 2. Timing the download
$Start = Get-Date
Invoke-WebRequest -Uri $FILE -OutFile $DEST
$End = Get-Date

# 3. Calculating the time taken
$TimeTaken = $End-$Start

# 4. Displaying time taken
$Seconds = $TimeTaken.TotalSeconds
"Total seconds taken to perform download [$($TimeTaken.TotalSeconds)]"

# 5. Calculate bytes/sec
$Size = (Get-ChildItem -Path $DEST).Length
$Speed = ($Size/$Seconds).ToString('N2')

# 6. Display the speed achieved
"Download speed (bytes/second) [$Speed]"

I think it's a matter of choice.

Another point: The user should be informed that the output directory ("C:\foo" in this case) is not automatically created if it doesn't exist. The error message from PowerShell could be a little bit misleading. ๐Ÿค”

Mayabe the post should cover both methods and see if there is a time difference? BITS works in the background, and IIRC, Invoke-WebRequest works in the foreground so the latter would probably show a faster speed. Might be useful to show both and explain.

@termdew Are you still interested in writing this post?