Download speed function reports impossible speed in Mbps

rplazzotta

New Member
Joined
Oct 28, 2021
Messages
41
Office Version
  1. 2019
  2. 2010
Platform
  1. Windows
I've written a function (Downloadspeed()) that's supposed to report the download speed for a PDF file stored on my FTP server.
But it's reporting an impossible speed, i.e. roughly 3 times greater than my maximum LAN speed (which is 1 Gbps, = 1000 Mbps) on my Ethernet-wired PC. WiFi is disabled.
Can anyone help please? Full code here
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
People tend to be reluctant to download files from unknown sources, especially if they're binary files. Would it be possible to post the code here?

What are you using to time/measure the download speed?
 
Upvote 0
I'm using API functions GetTickCount64() (before and after download) and URLDownloadToFile().
My code below downloads (then optionally deletes) a 77.7 MB PDF file from my FTP server.
See comments in my function DownloadSpeed()

VBA Code:
Option Explicit

Private Declare PtrSafe Function URLDownloadToFile Lib "urlmon" Alias "URLDownloadToFileA" _
        (ByVal pCaller As LongPtr, ByVal szURL As String, ByVal szFileName As String, _
        ByVal dwReserved As LongPtr, ByVal lpfnCB As LongPtr) As Long
        
#If Win64 Then 'to calculate download time in milliseconds
    Declare PtrSafe Function GetTickCount64 Lib "kernel32" () As LongLong
#Else
    Declare PtrSafe Function GetTickCount Lib "kernel32" () As Long
#End If

#If Win64 Then  'for my Win10 64-bit, Office 2019 *** 64-bit *** PC
    Dim ST As LongLong      'start time point as returned by GetTickCount64
    Dim ET As LongLong      'end time point as returned by GetTickCount64
    Dim TT As LongLong      'time taken (ET - ST) in milliseconds
    Dim FSize As LongLong   'in bits, not bytes
    Dim MultiplyBy As LongLong
    Dim DivideBy As LongLong
#Else           'for my Win10 64-bit, Office 2019 *** 32-bit *** PC
    Dim ST As Long          'start time point as returned by GetTickCount
    Dim ET As Long          'end time point as returned by GetTickCount
    Dim TT As Long          'time taken (ET - ST) in milliseconds
    Dim FSize As Currency   'in bits, not bytes
    Dim MultiplyBy As Currency
    Dim DivideBy As Currency
#End If
     
Private Const ERROR_SUCCESS As Long = 0
Private Const BINDF_GETNEWESTVERSION As Long = &H10
Private Const INTERNET_FLAG_RELOAD As Long = &H80000000

Public Function DownloadSingleFile(sSourceUrl As String, sLocalFile As String, Optional ByVal AlertIfError As Boolean = True) As Boolean
'Download the file. BINDF_GETNEWESTVERSION forces the API to download from the specified source.
'Passing 0& as dwReserved causes the locally-cached copy to be downloaded, if available.
'If the API returns ERROR_SUCCESS (0), DownloadSingleFile returns True.
Dim RV As Long
'RV = URLDownloadToFile(0&, sSourceUrl, sLocalFile, BINDF_GETNEWESTVERSION, 0&)
RV = URLDownloadToFile(0&, sSourceUrl, sLocalFile, 0&, 0&)
If RV <> 0 Then 'ERROR_SUCCESS Then
    If AlertIfError Then
        MsgBox "Download failed, errror " & RV
    End If
Else
    DownloadSingleFile = True
End If
End Function

Public Function DownloadSpeed(Optional ByVal SourceURL As String = "", _
    Optional ByVal DeleteAfterTest As Boolean = True) As String 'return value = Mbps

'**********************************************************************************************************************************
'My router is an Orange Livebox 6, fibre broadband, theoretically 2 Gbps
'My LAN: Ethernet, my PC network adapter's Speed & Duplex = 1.0 Gbps Full Duplex, as shown in Device Manager, Advanced Tab
'Therefore, in my home this function should return a value <= 1 Gbps (<= 1000 Mbps)
'==================================================================================
'But on average it's returning 3200 Mbps! As demonstrated above, that's impossible,
'because it's more than my max. LAN speed (1 Gbps) and more than my router's max. speed (2 Gbps)
'
'                               WHAT'S WRONG IN MY CODE BELOW?
'
'**********************************************************************************************************************************

Dim FN As String    'save-file name
Dim Success As Boolean
Dim mbps As Double
If Len(SourceURL) = 0 Then
    SourceURL = "http://www.wot.fr/RPTB/RefDocs/Le bon usage Grammaire francaise - Grevisse.pdf"
    'The above file's size is exactly (81,484,673 bytes * 8 = 651,877,384 bits on my FTP server
    FN = ThisWorkbook.Path & "\SpeedTest.pdf"
Else
    FN = ThisWorkbook.Path & "\" & Mid(SourceURL, InStrRev(SourceURL, "/") + 1)
End If
If Dir(FN) <> "" Then
    Kill (FN)
End If
#If Win64 Then
    ST = GetTickCount64() 'start time
#Else
    ST = GetTickCount()   'start time
#End If
'*************************************************
Success = DownloadSingleFile(SourceURL, FN, False) 'see function above this one
'*************************************************
If Not Success Then
    DownloadSpeed = "Download failed"
    Exit Function
End If
#If Win64 Then
    ET = GetTickCount64() ' end time
#Else
    ET = GetTickCount()   ' end time
#End If
If Success And Dir(FN) <> "" Then
    TT = (ET - ST) 'milliseconds taken
    If Len(SourceURL) = 0 Then
        FSize = 4514938880# * 8   'in bits, see size above
    Else
        'FSize = File_Size(FN) * 8   'in bits (function in separate module)
        FSize = FileLen(FN) * 8      'in bits
    End If
    Debug.Print FSize & " bits took " & TT & " milliseconds to download"
    If TT < 1000 Then ' under 1 second
        Debug.Print "TT = " & Round(TT, 3)
        Debug.Print "MultiplyBy = 1000 / Round(TT, 3) = " & Round(1000 / Round(TT, 3), 3)
        MultiplyBy = Round(1000 / Round(TT, 3), 3)
        FSize = FSize * MultiplyBy
        Debug.Print "FSize * MultiplyBy = " & FSize
        Debug.Print Round(FSize, 0) & " bits would take 1 second to download"
        mbps = (Round(FSize, 0) / 1000000) 'divide by 1 million to return Mbps
       ' Debug.Print mbps & " Mbps (megabits per second)"
    Else
        Debug.Print "TT = " & Round(TT, 3)
        Debug.Print "DivideBy = Round(TT, 3) / 1000 = " & Round(Round(TT, 3) / 1000, 3)
        DivideBy = Round(Round(TT, 3) / 1000, 3)
        FSize = FSize / DivideBy
        Debug.Print "FSize / DivideBy = " & FSize
        Debug.Print Round(FSize, 0) & " bits would take 1 second to download"
        mbps = (Round(FSize, 0) / 1000000) 'divide by 1 million to return Mbps
    End If
    DownloadSpeed = Format(CStr(mbps), "####0.00")
    If DeleteAfterTest Then
        Kill (FN)
    End If
End If
End Function
 
Upvote 0
Thank you for posting that, and for providing the detail that you have.

I haven't yet worked my way through the logic of your calculations or API declarations, but the first thing that jumped out at me was your flags for the Download API - you code is sourcing the file from cache and not downloading it from online, no?

'Download the file. BINDF_GETNEWESTVERSION forces the API to download from the specified source.
'Passing 0& as dwReserved causes the locally-cached copy to be downloaded, if available.
Deleting the file from the designated directory doesn't remove it from cache. Do you get a different result if you uncomment the line you had previously, and try that instead of this?
 
Upvote 0
Solution
Yes you're right, thank you. I've just tested it with BINDF_GETNEWESTVERSION instead of 0&, and it returns 47 Mbps, which is very close to what FileZilla reports when I download the PDF "manually".
The problem must have been my interpretation of BINDF_GETNEWESTVERSION, which I thought meant the version on the remote site (FileZilla appears to cache versions of the same document).
 
Upvote 0
Glad you got it working! Thank you for the feedback!
 
Upvote 0

Forum statistics

Threads
1,215,110
Messages
6,123,146
Members
449,098
Latest member
Doanvanhieu

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top