I am using go-ping ( https://github.com/sparrc/go-ping )library of golang for unprivileged ICMP ping.
timeout := time.Second*1000 interval := time.Second count := 5 host := p.ipAddr pinger, cmdErr := ping.NewPinger(host) pinger.Count = count pinger.Interval = interval pinger.Timeout = timeout pinger.SetPrivileged(false) pinger.Run() stats := pinger.Statistics() latency = stats.AvgRtt // stats.AvgRtt is time.Duration type jitter = stats.StdDevRtt// stats.StdDevRtt is time.Duration type
From running this, I am getting latency in milliseconds and jitter in microseconds. I want same unit for both let's say millisecond so when I am doing jitter = stats.StdDevRtt/1000
or jitter = jitter/1000
(to convert microseconds to milliseconds), what I am getting is jitter in nanoseconds :(. Is there any way to get same unit milliseconds for both latency and jitter.
A microsecond is equal to 1000 nanoseconds or 1⁄1,000 of a millisecond.
To convert a microsecond measurement to a millisecond measurement, divide the time by the conversion ratio. The time in milliseconds is equal to the microseconds divided by 1,000.
To convert a second measurement to a millisecond measurement, multiply the time by the conversion ratio. The time in milliseconds is equal to the seconds multiplied by 1,000.
To convert a second measurement to a microsecond measurement, multiply the time by the conversion ratio. The time in microseconds is equal to the seconds multiplied by 1,000,000.
time.Duration
time.Duration
is a type having int64
as its underlying type, which stores the duration in nanoseconds.
If you know the value but you want other than nanoseconds, simply multiply the unit you want, e.g.:
d := 100 * time.Microsecond fmt.Println(d) // Output: 100µs
The above works because 100
is an untyped constant, and it can be converted automatically to time.Duration
which has int64
underlying type.
Note that if you have the value as a typed value, you have to use explicit type conversion:
value := 100 // value is of type int d2 := time.Duration(value) * time.Millisecond fmt.Println(d2) // Output: 100ms
time.Duration
to numberSo time.Duration
is always the nanoseconds. If you need it in milliseconds for example, all you need to do is divide the time.Duration
value with the number of nanoseconds in a millisecond:
ms := int64(d2 / time.Millisecond) fmt.Println("ms:", ms) // Output: ms: 100
Other examples:
fmt.Println("ns:", int64(d2/time.Nanosecond)) // ns: 100000000 fmt.Println("µs:", int64(d2/time.Microsecond)) // µs: 100000 fmt.Println("ms:", int64(d2/time.Millisecond)) // ms: 100
Try the examples on the Go Playground.
If your jitter (duration) is less than the unit you whish to convert it to, you need to use floating point division, else an integer division will be performed which cuts off the fraction part. For details see: Golang Round to Nearest 0.05.
Convert both the jitter and unit to float64
before dividing:
d := 61 * time.Microsecond fmt.Println(d) // Output: 61µs ms := float64(d) / float64(time.Millisecond) fmt.Println("ms:", ms) // Output: ms: 0.061
Output (try it on the Go Playground):
61µs ms: 0.061
As of Go 1.13, you can use new Duration methods Microseconds
and Milliseconds
which return the duration as an integer count of their respectively named units.
https://golang.org/doc/go1.13#time
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With