-
Notifications
You must be signed in to change notification settings - Fork 3
/
benchmark_test.go
103 lines (78 loc) · 2.36 KB
/
benchmark_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
package compandauth_test
import (
"math/rand"
"testing"
"time"
"github.com/endiangroup/compandauth"
"github.com/endiangroup/compandauth/clock"
)
var isValidResult bool
var sessionCAAResult compandauth.SessionCAA
func Benchmark_Counter_Issue(b *testing.B) {
b.StopTimer()
entity := CounterEntity{Delta: 10, CAA: compandauth.NewCounter()}
var sessionCAA compandauth.SessionCAA
b.StartTimer()
for i := 0; i < b.N; i++ {
sessionCAA = entity.CAA.Issue()
}
sessionCAAResult = sessionCAA
}
func Benchmark_Counter_IsValid(b *testing.B) {
b.StopTimer()
entity := CounterEntity{Delta: 10, CAA: compandauth.NewCounter()}
numberOfSessions := 100000
sessions := []Session{}
for i := 0; i < numberOfSessions; i++ {
newSession := Session{}
newSession.CAA = compandauth.SessionCAA(rand.Intn(numberOfSessions))
if rand.Intn(numberOfSessions)%2 == 0 {
entity.CAA.Issue()
// Randomly lock ~50% of the sessions
newSession.CAA = compandauth.SessionCAA(-1) * newSession.CAA
}
sessions = append(sessions, newSession)
}
var isValid bool
b.StartTimer()
for i := 0; i < b.N; i++ {
isValid = entity.CAA.IsValid(sessions[i%numberOfSessions].CAA, entity.Delta)
}
isValidResult = isValid
}
func Benchmark_Timeout_IsValid(b *testing.B) {
b.StopTimer()
start := time.Now()
clock.NowForce(start)
defer clock.NowReset()
entity := TimeoutEntity{Timeout: 30 * time.Second, CAA: compandauth.NewTimeout()}
numberOfSessions := 100000
offset := start.Unix() - int64(numberOfSessions/2)
sessions := []Session{}
for i := 0; i < numberOfSessions; i++ {
newSession := Session{}
newSession.CAA = compandauth.SessionCAA(offset + int64(rand.Intn(numberOfSessions)))
if rand.Intn(numberOfSessions)%2 == 0 {
entity.CAA.Issue()
// Randomly lock ~50% of the sessions
newSession.CAA = compandauth.SessionCAA(-1) * newSession.CAA
}
sessions = append(sessions, newSession)
}
var isValid bool
b.StartTimer()
for i := 0; i < b.N; i++ {
isValid = entity.CAA.IsValid(sessions[i%numberOfSessions].CAA, compandauth.ToSeconds(entity.Timeout))
}
isValidResult = isValid
}
func Benchmark_Timeout_Issue(b *testing.B) {
b.StopTimer()
entity := TimeoutEntity{Timeout: 30 * time.Second, CAA: compandauth.NewTimeout()}
var sessionCAA compandauth.SessionCAA
b.StartTimer()
for i := 0; i < b.N; i++ {
sessionCAA = entity.CAA.Issue()
}
sessionCAAResult = sessionCAA
}