-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbench_test.go
More file actions
179 lines (154 loc) · 3.89 KB
/
bench_test.go
File metadata and controls
179 lines (154 loc) · 3.89 KB
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package multielo
import (
"testing"
"time"
)
func setupLeagueForBenchmark(playerCount int) (*League, []*MatchResult) {
cfg := DefaultConfig()
cfg.MaxMatches = 1 << 30 // effectively unbounded for benchmark runs
l := NewLeagueWithConfig(cfg)
players := make([]*Player, playerCount)
for i := 0; i < playerCount; i++ {
name := string(rune('A'+i%26)) + string(rune('a'+(i/26)))
_ = l.AddPlayer(name)
p, _ := l.GetPlayer(name)
players[i] = p
}
results := make([]*MatchResult, playerCount)
for i := 0; i < playerCount; i++ {
results[i] = &MatchResult{Position: i + 1, Player: players[i]}
}
return l, results
}
// Benchmark: Small match (8 players)
func BenchmarkAddMatchSmall(b *testing.B) {
l, results := setupLeagueForBenchmark(8)
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
if err := l.AddMatch(results, time.Now()); err != nil {
b.Fatalf("add match failed: %v", err)
}
if i%1000 == 0 {
l.ResetMatches()
}
}
}
// Benchmark: Medium match (20 players)
func BenchmarkAddMatchMedium(b *testing.B) {
l, results := setupLeagueForBenchmark(20)
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
if err := l.AddMatch(results, time.Now()); err != nil {
b.Fatalf("add match failed: %v", err)
}
if i%1000 == 0 {
l.ResetMatches()
}
}
}
// Benchmark: Large match (50 players)
func BenchmarkAddMatchLarge(b *testing.B) {
l, results := setupLeagueForBenchmark(50)
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
if err := l.AddMatch(results, time.Now()); err != nil {
b.Fatalf("add match failed: %v", err)
}
if i%100 == 0 {
l.ResetMatches()
}
}
}
// Benchmark: GetPlayer lookups (should be O(1))
func BenchmarkGetPlayer(b *testing.B) {
l, _ := setupLeagueForBenchmark(100)
testName := "Aa" // Known player from setup
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, err := l.GetPlayer(testName)
if err != nil {
b.Fatalf("get player failed: %v", err)
}
}
}
// Benchmark: GetLeaderboard with leaderboard caching
func BenchmarkGetLeaderboard(b *testing.B) {
l, results := setupLeagueForBenchmark(30)
// Add some matches to populate the league
for i := 0; i < 10; i++ {
_ = l.AddMatch(results, time.Now())
}
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = l.GetLeaderboard()
}
}
// Benchmark: GetPlayerStats
func BenchmarkGetPlayerStats(b *testing.B) {
l, results := setupLeagueForBenchmark(20)
// Add some matches to build up stats
for i := 0; i < 50; i++ {
_ = l.AddMatch(results, time.Now())
}
player, _ := l.GetPlayer("Aa")
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = player.GetStats()
}
}
// Benchmark: League initialization with many players
func BenchmarkLeagueInitialization(b *testing.B) {
const playerCount = 100
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
cfg := DefaultConfig()
cfg.MaxMatches = 1 << 30
l := NewLeagueWithConfig(cfg)
for j := 0; j < playerCount; j++ {
name := string(rune('A'+(j%26))) + string(rune('a'+(j/26)))
_ = l.AddPlayer(name)
}
}
}
// Benchmark: Service layer AddPlayer
func BenchmarkServiceAddPlayer(b *testing.B) {
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
cfg := DefaultConfig()
cfg.MaxMatches = 1 << 30
cfg.MaxPlayers = 10 // Just need a few players per iteration
l := NewLeagueWithConfig(cfg)
service := NewLeagueService(l)
for j := 0; j < 10; j++ {
name := "Player" + string(rune('A'+(j%26)))
if err := service.AddPlayer(name); err != nil {
b.Fatalf("add player failed: %v", err)
}
}
}
}
// Benchmark: Match filtering
func BenchmarkMatchFiltering(b *testing.B) {
l, results := setupLeagueForBenchmark(10)
// Add many matches
for i := 0; i < 100; i++ {
_ = l.AddMatch(results, time.Now())
}
filter := MatchFilter{
Limit: 10,
Offset: 0,
}
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = l.GetMatchesFiltered(filter)
}
}