dougong9987 2017-01-06 10:33
浏览 1932


I am using 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
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.

  • 写回答

2条回答 默认 最新

  • doudun3040 2017-01-06 10:42

    Number to 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 number

    So 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):

    ms: 0.061
    本回答被题主选为最佳回答 , 对您是否有帮助呢?



  • ¥15 学校优化算法sbo和蚁群算法怎么结合
  • ¥20 Apache poi使用问题
  • ¥21 matlab怎么求时域信号的二阶导数
  • ¥15 判断两个表是否完全相同
  • ¥15 java map类型数据格式,如何快速通过前缀匹配元素
  • ¥15 stc12c5a60s2、QMC5883L、LCD1602组合测量磁场所需程序
  • ¥20 Win11测试yolov4,“找不到nvcuda.dll”怎么办?
  • ¥15 simulink绘制bode图
  • ¥15 php_network_getaddresses: getaddrinfo failed: Name or service not known
  • ¥15 用msg发消息出现的问题