-
Notifications
You must be signed in to change notification settings - Fork 10
/
time_test.go
68 lines (57 loc) · 1.49 KB
/
time_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
package utils
import (
"testing"
"time"
"github.com/stretchr/testify/require"
)
func checkTimeStamp(tb testing.TB, expectedCurrent, actualCurrent uint32) {
tb.Helper()
// test with some buffer in front and back of the expectedCurrent time -> because of the timing on the work machine
require.True(tb, true, actualCurrent >= expectedCurrent-1 || actualCurrent <= expectedCurrent+1)
}
func Test_TimeStampUpdater(t *testing.T) {
t.Parallel()
StartTimeStampUpdater()
now := uint32(time.Now().Unix())
checkTimeStamp(t, now, Timestamp())
// one second later
time.Sleep(1 * time.Second)
checkTimeStamp(t, now+1, Timestamp())
// two seconds later
time.Sleep(1 * time.Second)
checkTimeStamp(t, now+2, Timestamp())
}
func Benchmark_CalculateTimestamp(b *testing.B) {
var res uint32
StartTimeStampUpdater()
b.Run("fiber", func(bb *testing.B) {
bb.ReportAllocs()
bb.ResetTimer()
for n := 0; n < bb.N; n++ {
_ = Timestamp()
}
})
b.Run("default", func(bb *testing.B) {
bb.ReportAllocs()
bb.ResetTimer()
for n := 0; n < bb.N; n++ {
_ = uint32(time.Now().Unix())
}
})
b.Run("fiber_asserted", func(bb *testing.B) {
bb.ReportAllocs()
bb.ResetTimer()
for n := 0; n < bb.N; n++ {
res = Timestamp()
checkTimeStamp(bb, uint32(time.Now().Unix()), res)
}
})
b.Run("default_asserted", func(bb *testing.B) {
bb.ReportAllocs()
bb.ResetTimer()
for n := 0; n < bb.N; n++ {
res = uint32(time.Now().Unix())
checkTimeStamp(bb, uint32(time.Now().Unix()), res)
}
})
}