-
Notifications
You must be signed in to change notification settings - Fork 0
/
powernap_test.go
89 lines (76 loc) · 1.83 KB
/
powernap_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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package powernap
import (
"fmt"
"math"
"testing"
"time"
)
func TestSleep(t *testing.T) {
// Run some test sleeps at different durations
d := time.Nanosecond
for i := 6; i < 10; i++ {
d = time.Duration(math.Pow10(i))
reps := int(math.Pow10(9 - i))
// Iterate a bunch of times, to rule out most of the time keeping
start := time.Now()
for n := 0; n < reps; n++ {
Sleep(d)
}
total := time.Since(start)
avg := total / time.Duration(reps)
if math.Abs(float64(avg-d)) > float64(d)/100 {
t.Fatalf("expected: %v, got: %v, diff: %v\n", d, avg, math.Abs(float64(avg-d)))
}
}
}
func TestSleepTight(t *testing.T) {
// Run some test sleeps at different durations
d := time.Nanosecond
for i := 6; i < 10; i++ {
d = time.Duration(math.Pow10(i))
reps := int(math.Pow10(9 - i))
// Iterate a bunch of times, to rule out most of the time keeping
start := time.Now()
for n := 0; n < reps; n++ {
SleepTight(d)
}
total := time.Since(start)
avg := total / time.Duration(reps)
if math.Abs(float64(avg-d)) > float64(d)/100 {
t.Fatalf("expected: %v, got: %v, diff: %v\n", d, avg, math.Abs(float64(avg-d)))
}
}
}
func BenchmarkNativeSleep(b *testing.B) {
d := time.Nanosecond
for i := 0; i < 10; i++ {
d = time.Duration(math.Pow10(i))
b.Run(fmt.Sprintf("%v", d), func(b *testing.B) {
for i := 0; i < b.N; i++ {
time.Sleep(d)
}
})
}
}
func BenchmarkSleep(b *testing.B) {
d := time.Nanosecond
for i := 0; i < 10; i++ {
d = time.Duration(math.Pow10(i))
b.Run(fmt.Sprintf("%v", d), func(b *testing.B) {
for i := 0; i < b.N; i++ {
Sleep(d)
}
})
}
}
func BenchmarkSleepTight(b *testing.B) {
d := time.Nanosecond
for i := 0; i < 10; i++ {
d = time.Duration(math.Pow10(i))
b.Run(fmt.Sprintf("%v", d), func(b *testing.B) {
for i := 0; i < b.N; i++ {
SleepTight(d)
}
})
}
}