-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfastnano_timing_test.go
64 lines (56 loc) · 1.25 KB
/
fastnano_timing_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
package fastnano
import (
"sync/atomic"
"testing"
"time"
)
func BenchmarkUnixNano(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = time.Now().UnixNano()
}
}
func BenchmarkFastnano(b *testing.B) {
t := NewFastNano()
for i := 0; i < b.N; i++ {
_ = t.UnixNanoTimestamp()
}
}
// This idea was taken from https://github.com/VictoriaMetrics/VictoriaMetrics/blob/master/lib/fasttime/fasttime_timing_test.go
// Sink should prevent from code elimination by optimizing compiler
var Sink atomic.Int64
func BenchmarkUnixNano_Parallel(b *testing.B) {
b.ReportAllocs()
b.RunParallel(func(pb *testing.PB) {
var ts int64
for pb.Next() {
ts += time.Now().UnixNano()
}
Sink.Store(ts)
})
}
func BenchmarkFastnano_Parallel(b *testing.B) {
var nt = NewFastNano()
b.ReportAllocs()
b.RunParallel(func(pb *testing.PB) {
var ts int64
for pb.Next() {
ts += nt.UnixNanoTimestamp()
}
Sink.Store(ts)
})
}
func oldFastNano() *FastNano {
t := time.Now().Add(time.Hour * 24 * 365 * (-1))
return &FastNano{time: t, nano: t.UnixNano()}
}
func BenchmarkFastnanoOld_Parallel(b *testing.B) {
var nt = oldFastNano()
b.ReportAllocs()
b.RunParallel(func(pb *testing.PB) {
var ts int64
for pb.Next() {
ts += nt.UnixNanoTimestamp()
}
Sink.Store(ts)
})
}