timing_test.go 744 B

1234567891011121314151617181920212223242526272829303132
  1. package udptransfer
  2. import (
  3. "math"
  4. "testing"
  5. "time"
  6. )
  7. func Test_sleep(t *testing.T) {
  8. const loops = 10
  9. var intervals = [...]int64{1, 1e3, 1e4, 1e5, 1e6, 1e7}
  10. var ret [len(intervals)][loops]int64
  11. for j := 0; j < len(intervals); j++ {
  12. v := time.Duration(intervals[j])
  13. for i := 0; i < loops; i++ {
  14. t0 := NowNS()
  15. time.Sleep(v)
  16. ret[j][i] = NowNS() - t0
  17. }
  18. }
  19. for j := 0; j < len(intervals); j++ {
  20. var exp, sum, stdev float64
  21. exp = float64(intervals[j])
  22. for _, v := range ret[j] {
  23. sum += float64(v)
  24. stdev += math.Pow(float64(v)-exp, 2)
  25. }
  26. stdev /= float64(loops)
  27. stdev = math.Sqrt(stdev)
  28. t.Logf("interval=%s sleeping=%s stdev/k=%.2f", time.Duration(intervals[j]), time.Duration(sum/loops), stdev/1e3)
  29. }
  30. }