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