github.com/loov/hrtime@v1.0.3/now_test.go (about) 1 // +build !race 2 3 package hrtime_test 4 5 import ( 6 "testing" 7 "time" 8 9 "github.com/loov/hrtime" 10 ) 11 12 func TestNowCalibration(t *testing.T) { 13 start := hrtime.Now() 14 empty() 15 stop := hrtime.Now() 16 if stop-start < hrtime.Overhead() { 17 t.Errorf("measurement: %v %v", stop-start, hrtime.Overhead()) 18 } 19 } 20 21 func TestNowPrecision(t *testing.T) { 22 const N = 8 << 10 23 24 start := hrtime.Now() 25 for i := 0; i < N; i++ { 26 empty() 27 } 28 stop := hrtime.Now() 29 loopTime := stop - start - 2*hrtime.Overhead() 30 31 // we expect each call to take at least 1 nanosecond 32 if loopTime.Nanoseconds() < N { 33 t.Errorf("slow: loop time took %d", loopTime) 34 } 35 // we expect no call to take more than 10 nanoseconds 36 if loopTime.Nanoseconds() > 10*N { 37 t.Errorf("fast: loop time took %d", loopTime) 38 } 39 } 40 41 //go:noinline 42 func empty() {} 43 44 func BenchmarkTimeNow(b *testing.B) { 45 for i := 0; i < b.N; i++ { 46 time.Now() 47 } 48 } 49 50 func BenchmarkNow(b *testing.B) { 51 for i := 0; i < b.N; i++ { 52 hrtime.Now() 53 } 54 }