What's new

Whats the most accurate way to get current in/outgoing speed (eth0) ?

  • SNBForums Code of Conduct

    SNBForums is a community for everyone, no matter what their level of experience.

    Please be tolerant and patient of others, especially newcomers. We are all here to share and learn!

    The rules are simple: Be patient, be nice, be helpful or be gone!

heliosone

Regular Contributor
Im trying to read/calculate the current overall bandwidth/speed for down&up stream ( eth0 ) on my AC56U router.
The values are send via python/socket to a raspberryPi that draws a running speed chart on a ws2812b led matrix display.

right now I use /sys/class/net/eth0/statistics/rx_bytes & /sys/class/net/eth0/statistics/tx_bytes
calculating the speed through the difference over a certain time.

This works quite well for the downstream,but when I try to do this also for the upstream (tx),I kinda hit a wall.
Quite often the calculated values are a far cry from the real ones currently happening.
I.e. when I use a speedtest (@google),the upstream speed goes up all the way to match the downstream speed,which isnt possible,as my upstream speed is capped @~1/6 of the download speed.
On other occasions,i..e when downloading a torrent from a lot of sources,again,the upstream speed reaches unrealistic values.(even though throttled within the torrent program)
Or when 100% upstream speed uploading ,also the downspeed goes way(100%) up,even if there are no downloads running at all.

What other ways are there to calculate the overall speeds that yield (more) realistic values ?


PS: part of the script I use right now :

rx=open('/sys/class/net/eth0/statistics/rx_bytes', 'r')
rx_old=int(rx.read())
time.sleep(ts)
rx=open('/sys/class/net/eth0/statistics/rx_bytes', 'r')
rx_new=int(rx.read())
speed=int(((rx_new-rx_old)/1024)/ts)
 
Merlin uses /proc/net/dev
 
the values are identical

rx=$(cat /proc/net/dev|grep eth0|awk '{print $2}');rx2=$(cat /sys/class/net/eth0/statistics/rx_bytes)
echo $rx"/"$rx2

any ideas why the spike when using a speedtest or downloading a well seeded torrent ?
 
Is your WAN interface actually eth0? If you have hardware acceleration enabled it's probably vlan2 instead.
 
using nload I checked,but vlan2 is dead here.
eth0 (with my wan IP) shows only correct speed values for incoming,but the outgoing part is completly wrong.. it shows the same traffic amount as incoming,and the same speed max/mins,which isnt possible.

during the google speedtest, vlan1 shows correct values for BOTH.
but its mirrored
incoming shows the outgoing speed (~5 mbit)
and outgoing shows the incoming speed (~30mbit)

one other thing I once discovered.. when checking with nload... one of the vlans,Im not sure which one,showed FULL bandwidth allocation(and traffic counting up),even though no download OR wifi datatransfer was running at the time.

also why does show /proc/net/dev different traffic amounts
eth0 : 1295404127
vlan1: 683371649

im a bit clueless here to be honest :)
 
vlan1 is the router's LAN ports, so it doesn't include the wifi interfaces. br0 is more relevant.
 
br0 doesnt show any activity when browsing the web/speedtest/...
vlan1 does show the accurate speed (in nload & script with cat /sys/class/net/vlan1/statistics/rx_bytes)

if vlan1 is the lan ports.. shouldnt that also samba traffic between my pc<=>rpiNAS ?
because I just watch a movie via samba@vlc,and there is NO speed increase@vlan1 (nload)
actually... no interface available in nload shows any traffic while watching the movie ...

crap... Im feeling a headache coming :(

so what interface/or rx/tx sum do I have to use for speed calculation,so that I get an accurate speed thats currently going up/down the cable to my cablemodem ?(in other words :the overall speed to/from the net)
 
another example,on how wrong the values calculated from statistics are :

streaming youtube (1080p):

using the script below I get : (@Eth0)

TX eth0: 2280 kB/s RX eth0: 2207 kB/s
TX eth0: 4052 kB/s RX eth0: 4009 kB/s
TX eth0: 3836 kB/s RX eth0: 3791 kB/s
TX eth0: 3060 kB/s RX eth0: 3017 kB/s
TX eth0: 4060 kB/s RX eth0: 4010 kB/s
TX eth0: 2761 kB/s RX eth0: 2728 kB/s

why are the tx values so wrong and almost identical with the rx values ?
Wrong,as in they CANT be that high,as I only have 5mbit upstream speed(and 30 down)


Script I used :
Code:
#!/bin/sh

INTERVAL="1"  # update interval in seconds

if [ -z "$1" ]; then
        echo
        echo usage: $0 [network-interface]
        echo
        echo e.g. $0 eth0
        echo
        exit
fi

IF=$1

while true
do
        R1=`cat /sys/class/net/$1/statistics/rx_bytes`
        T1=`cat /sys/class/net/$1/statistics/tx_bytes`
        sleep $INTERVAL
        R2=`cat /sys/class/net/$1/statistics/rx_bytes`
        T2=`cat /sys/class/net/$1/statistics/tx_bytes`
        TBPS=`expr $T2 - $T1`
        RBPS=`expr $R2 - $R1`
        TKBPS=`expr $TBPS / 1024`
        RKBPS=`expr $RBPS / 1024`
        echo "TX $1: $TKBPS kB/s RX $1: $RKBPS kB/s"
        sleep $INTERVAL
done





is it possible there is no accurate way to determine the actual current speeds ?
 
is it possible there is no accurate way to determine the actual current speeds ?
I'm coming to the same conclusion. I've never had a problem like this on my routers. It's possible it's simply a bug/limitation of that model, or the firmware which is years old now.
 

Similar threads

Sign Up For SNBForums Daily Digest

Get an update of what's new every day delivered to your mailbox. Sign up here!
Top