-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtsc_amd64_test.go
106 lines (82 loc) · 1.73 KB
/
tsc_amd64_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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
//go:build amd64
// +build amd64
package tsc
import (
"fmt"
"math/rand"
"testing"
"time"
"github.com/templexxx/tsc/internal/xbytes"
)
func TestStoreOffsetCoeff(t *testing.T) {
rand.Seed(time.Now().UnixNano())
dst := xbytes.MakeAlignedBlock(128, 128)
for i := 0; i < 1024; i++ {
coeff := rand.Float64()
offset := rand.Int63()
storeOffsetCoeff(&dst[0], offset, coeff)
actOffset, actCoeff := LoadOffsetCoeff(&dst[0])
if actOffset != offset {
t.Log(coeff, offset, actCoeff, actOffset)
t.Fatalf("offset not equal, exp: %d, got: %d", offset, actOffset)
}
if actCoeff != coeff {
t.Fatalf("coeff not equal, exp: %.2f, got: %.2f", coeff, actCoeff)
}
}
}
// Out-of-Order test, GetInOrder should be in order as we assume.
func TestGetInOrder(t *testing.T) {
if !Supported() {
t.Skip("tsc is unsupported")
}
n := 4096
ret0 := make([]int64, n)
ret1 := make([]int64, n)
for i := range ret0 {
ret0[i] = GetInOrder()
ret1[i] = GetInOrder()
}
cnt := 0
for i := 0; i < n; i++ {
d := ret1[i] - ret0[i]
if d < 0 {
cnt++
}
}
if cnt > 0 {
t.Fatal(fmt.Sprintf("GetInOrder is not in order: %d aren't in order", cnt))
}
}
func BenchmarkGetInOrder(b *testing.B) {
if !Supported() {
b.Skip("tsc is unsupported")
}
for i := 0; i < b.N; i++ {
_ = GetInOrder()
}
}
func BenchmarkRDTSC(b *testing.B) {
if !Supported() {
b.Skip("tsc is unsupported")
}
for i := 0; i < b.N; i++ {
_ = RDTSC()
}
}
func BenchmarUnixNanoTSCFMA(b *testing.B) {
if !Supported() {
b.Skip("tsc is unsupported")
}
for i := 0; i < b.N; i++ {
_ = unixNanoTSCFMA()
}
}
func BenchmarkUnixNanoTSC16B(b *testing.B) {
if !Supported() {
b.Skip("tsc is unsupported")
}
for i := 0; i < b.N; i++ {
_ = unixNanoTSC16B()
}
}