0

I've gone into creating Autohotkey Script GUIs out of interest. I made a diagnostic tool of sorts, and one function of this tool is to give the user a constant, live update on their download speed in Kbps and Mbps using the netstat command.

I decided to use netstat because based on the individual machine setup, the adapter/connection interface can change, causing the tool to not work on all computers.

The only problem is when using Netstat, there's a delay in the update causing the progress bar data to hop back and fourth between "0" and the actual speed. (It kind of resets itself due to the delay with netstat data update)

The question I have is if there's a way to keep the data constant and accurate using the Netstat command, or if there's a more efficient way to get the result desired?

Here's part of the code:

#SingleInstance, Force
nstFile:="C:\bytes.txt"
Process Priority,,High
OnExit, exit
Gui, Show, h450 w500, Diagnostics

Gui, Font,
Gui, Add, Text, x200 y240, Live Network Activity:
Gui, Add, Progress, x131 y260 h100 w15 Vertical clime vbytesbar backgroundgray Range0-25
Gui, Add, Text, x105 y385 h15 vbytes, Loading...
Gui, Add, Text, x80 y370, Download Speed

Gui, Add, GroupBox, x320 y231 w115 h190 cblack
Gui, Font, s6
Gui, Add, Text, x92 y353, 0  Mbps  -
Gui, Add, Text, x92 y334, 5  Mbps  -
Gui, Add, Text, x90 y314, 10 Mbps  -
Gui, Add, Text, x90 y295, 15 Mbps  -
Gui, Add, Text, x90 y276, 20 Mbps  -
Gui, Add, Text, x90 y255, 25 Mbps  -

setTimer, updateBytes, 1000

updateBytes:
    runWait %comspec% /c Netstat -e >"%nstFile%",,hide
    fileReadLine,bytesLine,% nstFile,5
    regExMatch(bytesLine,"Bytes\s+\K\d+",bytesData)
    bandwidth:=bytesData-prevBytesData
    prevBytesData:=bytesData
    guiControl,text,bytes,% bandwidth//1000 " Kbps"
    guiControl 1: , bytesbar,% bandwidth//1000000
Return

guiclose:
guiescape:
exit:
    {
    ifexist, C:\bytes.txt
    FileDelete, C:\bytes.txt
    }
    {
    exitapp
    }
return

Thanks for your time and assistance.

  • It's better to query the internal performance counters directly using [pdh.dll and DllCall](http://autohotkey.com/board/topic/16387-using-dllcall-pdhdll-to-query-performance-counters/) – wOxxOm Nov 13 '15 at 09:50

1 Answers1

0

Use A_TickCount to save the point of time when you read your data and always compare it to the last time.. Also, it appears you are looking for the download speed in Kilobits per second (as used for advertising by your provider) and nor for Kilobytes per second (as used to display download speeds in most computer applications). 1 Kilobyte = 8 Kilobit, so just multiply it by 8. It should look somewhat like this:
Round(((currentSize/1024-lastSize/1024)/((currentSizeTick-lastSizeTick)/1000))*8)

Here is the updated code:

#SingleInstance, Force
nstFile:="C:\bytes.txt"
Process Priority,,High
OnExit, exit
Gui, Show, h450 w500, Diagnostics

Gui, Font,
Gui, Add, Text, x200 y240, Live Network Activity:
Gui, Add, Progress, x131 y260 h100 w15 Vertical clime vbytesbar backgroundgray Range0-25000
Gui, Add, Text, x105 y385 h15 vbytes, Loading...
Gui, Add, Text, x80 y370, Download Speed

Gui, Add, GroupBox, x320 y231 w115 h190 cblack
Gui, Font, s6
Gui, Add, Text, x92 y353, 0  Mbps  -
Gui, Add, Text, x92 y334, 5  Mbps  -
Gui, Add, Text, x90 y314, 10 Mbps  -
Gui, Add, Text, x90 y295, 15 Mbps  -
Gui, Add, Text, x90 y276, 20 Mbps  -
Gui, Add, Text, x90 y255, 25 Mbps  -

setTimer, updateBytes, 1000

updateBytes:
    runWait %comspec% /c Netstat -e >"%nstFile%",,hide
    fileReadLine,bytesLine,% nstFile,5
    regExMatch(bytesLine,"Bytes\s+\K\d+",currentSize)
    currentSizeTick := A_TickCount
    speedKBs := Round((currentSize/1024-lastSize/1024)/((currentSizeTick-lastSizeTick)/1000))
    speedKBits := Round(((currentSize/1024-lastSize/1024)/((currentSizeTick-lastSizeTick)/1000))*8)
    lastSizeTick := currentSizeTick
    lastSize := currentSize
    guiControl,text,bytes,% speedKBits " Kbps"
    guiControl 1: , bytesbar,% speedKBits
Return

guiclose:
guiescape:
exit:
    {
    ifexist, C:\bytes.txt
    FileDelete, C:\bytes.txt
    }
    {
    exitapp
    }
return

I also changed the range of your progress bar to Range0-25000, so we can spare the dividing part.

And be aware, that you need to run this script as admin on most computers.

Forivin
  • 14,780
  • 27
  • 106
  • 199