Jump to content

download & upload bandwitdh meter (speed test)


Neutro
 Share

Recommended Posts

Hey guys,

After someone requested help about this in the support forum i thought i would share it here as well :)

The following script uses WMI to detect the active network card on a computer, gets its current download & upload speed and computes the average speeds.

You can also setup a minimum threshold to avoid getting false results.

#cs ----------------------------------------------------------------------------

 AutoIt Version: 3.3.14.2
 Author:         myName

 Script Function:
    Template AutoIt script.

#ce ----------------------------------------------------------------------------

; Script Start - Add your code below here

#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#Region ### START Koda GUI section ### Form=
$Form1_1 = GUICreate("Network speed info", 467, 331, 192, 124)
$Title = GUICtrlCreateLabel("Network speed info", 152, 16, 201, 28)
GUICtrlSetFont(-1, 14, 800, 0, "MS Sans Serif")
$Label1 = GUICtrlCreateLabel("Real time download speed:", 16, 72, 386, 24)
GUICtrlSetFont(-1, 12, 800, 0, "MS Sans Serif")
$Label2 = GUICtrlCreateLabel("Average download speed:", 16, 240, 392, 24)
GUICtrlSetFont(-1, 12, 800, 0, "MS Sans Serif")
$Label3 = GUICtrlCreateLabel("Dont record if download <", 16, 152, 208, 24)
GUICtrlSetFont(-1, 12, 800, 0, "MS Sans Serif")
$Label4 = GUICtrlCreateLabel("kB/s", 296, 152, 39, 24)
GUICtrlSetFont(-1, 12, 800, 0, "MS Sans Serif")
$Input1 = GUICtrlCreateInput("450", 224, 152, 65, 24)
GUICtrlSetFont(-1, 10, 800, 0, "MS Sans Serif")
$Label5 = GUICtrlCreateLabel("Real time upload speed:", 16, 112, 394, 24)
GUICtrlSetFont(-1, 12, 800, 0, "MS Sans Serif")
$Label6 = GUICtrlCreateLabel("Dont record if upload    <", 16, 192, 208, 24)
GUICtrlSetFont(-1, 12, 800, 0, "MS Sans Serif")
$Input2 = GUICtrlCreateInput("30", 224, 192, 65, 24)
GUICtrlSetFont(-1, 10, 800, 0, "MS Sans Serif")
$Label7 = GUICtrlCreateLabel("kB/s", 296, 192, 39, 24)
GUICtrlSetFont(-1, 12, 800, 0, "MS Sans Serif")
$update = GUICtrlCreateButton("Update", 360, 168, 81, 33)
GUICtrlSetFont(-1, 10, 800, 0, "MS Sans Serif")
$Label8 = GUICtrlCreateLabel("Average upload speed:", 16, 280, 416, 24)
GUICtrlSetFont(-1, 12, 800, 0, "MS Sans Serif")
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

$objWMIService = ObjGet("winmgmts:\\localhost\root\CIMV2")

;detect the active network card

$oSelect_active_network_cards = $objWMIService.ExecQuery('SELECT ProductName FROM Win32_NetworkAdapter WHERE NetConnectionStatus = 2 OR NetConnectionStatus = 9', "WQL")

for $oSelect_active_network_card in $oSelect_active_network_cards

    $active_network_card = stringright($oSelect_active_network_card.ProductName, 10) ;get part of the name of the active network card

Next

$threshhold_download = 450

$threshhold_upload = 30

$average_download_speed = 0

$average_upload_speed = 0

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $update
            $threshhold_download = Guictrlread($Input1)
            $threshhold_upload = Guictrlread($Input2)
    EndSwitch
    update_speeds()
    sleep(1)
WEnd


func update_speeds()

    ;get the download and upload speed

    $oNetwork_cards = $objWMIService.ExecQuery("SELECT BytesReceivedPerSec, BytesSentPerSec FROM Win32_PerfFormattedData_Tcpip_NetworkInterface WHERE name LIKE '%" & $active_network_card & "%'", "WQL")

    For $oNetwork_card In $oNetwork_cards

        $current_download_speed = Round($oNetwork_card.BytesReceivedPerSec / 1024)

        $current_upload_speed = Round($oNetwork_card.BytesSentPerSec / 1024)

        GUICtrlSetData($Label1, "Real time download speed:  " & $current_download_speed & " kB/s")

        GUICtrlSetData($Label5, "Real time upload speed:  " & $current_upload_speed & " kB/s")

        ;calculate the average download speed

        if $current_download_speed > $threshhold_download then

            $average_download_speed = Round(($current_download_speed + $average_download_speed) / 2)

            GUICtrlSetData($Label2, "Average download speed:  " & $average_download_speed & " kB/s")

        EndIf

        ;calculate the average upload speed

        if $current_upload_speed > $threshhold_upload Then

            $average_upload_speed = Round(($current_upload_speed + $average_upload_speed) / 2)

            GUICtrlSetData($Label8, "Average upload speed:  " & $average_upload_speed & " kB/s")

        EndIf

    Next

EndFunc

There are others topics in the example forum that show how to get network cards performance from WMI but afaik none showed how to match different WMI providers to determine the average speed so here is one way to do it :)

 

Edited by Neutro
Link to comment
Share on other sites

  • 4 weeks later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...