Linux – Network latency measurement (Linux)

latencylinuxnetworkingtime

I'd like to measure network latency for SNMP GET request.
There is a free command line tool time which can be used to find timing statistics for various commands. For example it can be used with snmpget in the following way:

$ time snmpget -v 2c -c public 192.168.1.3 .1.3.6.1.2.1.2.2.1.10.2
IF-MIB::ifInOctets.2 = Counter32: 112857973

real    0m0.162s
user    0m0.069s
sys 0m0.005s

According to the manual, statistics conists of:

  • the elapsed real time between
    invocation and termination,

  • the user CPU time (the sum of the
    tms_utime and tms_cutime values in a
    struct tms as returned by
    times(2)),

  • the system CPU time (the sum of the
    tms_stime and tms_cstime values in a
    struct tms as returned by
    times(2)).

As you see, none of these options allows to measure real network latency time (with excluded other program execution time stats). Is there any way to do so? Maybe not using time tool, but rather some kernel hacks?

I wanted to ask, before I'll start to write my own program.

Thanks,
Piotr

Best Answer

The tcpdump(8) program's -ttt flag may be just what you need:

00:00:00.000031 IP haig.mdns > 224.0.0.251.mdns: 0 PTR (QM)? 1.0.168.192.in-addr.arpa. (42)
00:00:01.897031 IP haig.45240 > stackoverflow.com.www: Flags [F.], seq 866615166, ack 62506321, win 123, options [nop,nop,TS val 6026371 ecr 419296939], length 0
00:00:00.000030 IP haig.45242 > stackoverflow.com.www: Flags [F.], seq 853537650, ack 61102072, win 123, options [nop,nop,TS val 6026371 ecr 419296939], length 0
00:00:00.000019 IP haig.45243 > stackoverflow.com.www: Flags [F.], seq 863535366, ack 62086489, win 123, options [nop,nop,TS val 6026371 ecr 419296939], length 0

The timestamp at the beginning of the lines show the time in microseconds since the previous packet. By giving a different number of -t on the command line, you can get either absolute times, relative times since the first packet, or relative times between packets.

I've just shown random mdns and web traffic because that's what is easy to find on my system; you could easily filter for SNMP traffic with tcpdump(8) to get only the packets you're interested in. (Which would be a good idea, as dumping all traffic from busy systems can generate a huge load.)

Related Question