-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathtsc_test.go
74 lines (59 loc) · 1.68 KB
/
tsc_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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// +build !race
package hrtime_test
import (
"testing"
"time"
"github.com/loov/hrtime"
)
func TestCountCalibration(t *testing.T) {
if !hrtime.TSCSupported() {
t.Skip("Cycle counting not supported")
}
start := hrtime.TSC()
for i := 0; i < 64; i++ {
empty()
}
stop := hrtime.TSC()
if stop-start < hrtime.TSCOverhead() {
t.Errorf("overhead is larger than delta: %v-%v=%v overhead:%v", stop, start, stop-start, hrtime.TSCOverhead())
}
}
func TestCountPrecision(t *testing.T) {
if !hrtime.TSCSupported() {
t.Skip("Cycle counting not supported")
}
t.Logf("Conversion 1000000 count = %v", hrtime.Count(1000000).ApproxDuration())
const N = 8 << 10
startnano := hrtime.Now()
start := hrtime.TSC()
for i := 0; i < N; i++ {
empty()
}
stop := hrtime.TSC()
stopnano := hrtime.Now()
loopTime := stop - start - 2*hrtime.TSCOverhead()
wallTime := stopnano - startnano - 2*hrtime.Overhead() - 2*hrtime.TSCOverhead().ApproxDuration()
approxConversionDrift := wallTime - loopTime.ApproxDuration()
if approxConversionDrift < 0 {
approxConversionDrift *= -1
}
if approxConversionDrift > 2*hrtime.Overhead()+500*time.Nanosecond {
t.Logf("drift: too large %v (loopTime:%v, wallTime:%v)", approxConversionDrift, loopTime.ApproxDuration(), wallTime)
}
// we expect each call to take at least 1 nanos
if loopTime.ApproxDuration() < N {
t.Errorf("slow: loop time took %v", loopTime)
}
// we expect no call to take more than 20 nanos
if loopTime.ApproxDuration() > 20*N {
t.Errorf("fast: loop time took %v", loopTime)
}
}
func BenchmarkTSC(b *testing.B) {
if !hrtime.TSCSupported() {
b.Skip("Cycle counting not supported")
}
for i := 0; i < b.N; i++ {
hrtime.TSC()
}
}