forked from llm-d/llm-d-router
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbenchmark_test.go
More file actions
174 lines (147 loc) · 4.92 KB
/
Copy pathbenchmark_test.go
File metadata and controls
174 lines (147 loc) · 4.92 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
/*
Copyright 2025 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package queue
import (
"fmt"
"sync"
"testing"
"github.com/llm-d/llm-d-router/pkg/epp/flowcontrol/contracts"
"github.com/llm-d/llm-d-router/pkg/epp/framework/interface/flowcontrol"
"github.com/llm-d/llm-d-router/pkg/epp/framework/interface/flowcontrol/mocks"
)
var benchmarkFlowKey = flowcontrol.FlowKey{ID: "benchmark-flow"}
// BenchmarkQueues runs a series of benchmarks against all registered queue implementations.
func BenchmarkQueues(b *testing.B) {
for queueName, constructor := range RegisteredQueues {
b.Run(string(queueName), func(b *testing.B) {
// All queue implementations must support the default enqueue time comparator.
q, err := constructor(enqueueTimePolicy)
if err != nil {
b.Fatalf("Failed to construct queue '%s': %v", queueName, err)
}
b.Run("AddRemove", func(b *testing.B) {
benchmarkAddRemove(b, q)
})
b.Run("AddPeekRemove", func(b *testing.B) {
benchmarkAddPeekRemove(b, q)
})
b.Run("BulkAddThenBulkRemove", func(b *testing.B) {
benchmarkBulkAddThenBulkRemove(b, q)
})
b.Run("HighContention", func(b *testing.B) {
benchmarkHighContention(b, q)
})
})
}
}
// benchmarkAddRemove measures the throughput of tightly coupled Add and Remove operations in parallel. This is a good
// measure of the base overhead of the queue's data structure and locking mechanism.
func benchmarkAddRemove(b *testing.B, q contracts.SafeQueue) {
b.ReportAllocs()
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
item := mocks.NewMockQueueItemAccessor(1, "item", benchmarkFlowKey)
q.Add(item)
_, err := q.Remove(item.Handle())
if err != nil {
b.Fatalf("Remove failed: %v", err)
}
}
})
}
// benchmarkAddPeekRemove measures the throughput of a serial Add, Peek, and Remove sequence. This simulates a
// common consumer pattern where a single worker peeks at an item before deciding to process and remove it.
func benchmarkAddPeekRemove(b *testing.B, q contracts.SafeQueue) {
// Pre-add one item so Peek doesn't fail on the first iteration.
initialItem := mocks.NewMockQueueItemAccessor(1, "initial", benchmarkFlowKey)
q.Add(initialItem)
b.ReportAllocs()
for b.Loop() {
item := mocks.NewMockQueueItemAccessor(1, "item", benchmarkFlowKey)
q.Add(item)
peeked := q.Peek()
if peeked == nil {
// In a concurrent benchmark, this could happen if the queue becomes empty.
// In a serial one, it's a fatal error.
b.Fatal("Peek failed")
}
_, err := q.Remove(peeked.Handle())
if err != nil {
b.Fatalf("Remove failed: %v", err)
}
}
}
// benchmarkBulkAddThenBulkRemove measures performance of filling the queue up with a batch of items and then draining
// it. This can reveal performance characteristics related to how the data structure grows and shrinks.
func benchmarkBulkAddThenBulkRemove(b *testing.B, q contracts.SafeQueue) {
b.ReportAllocs()
for i := 0; b.Loop(); i++ {
// Add a batch of items
items := make([]flowcontrol.QueueItemAccessor, 100)
for j := range items {
item := mocks.NewMockQueueItemAccessor(1, fmt.Sprintf("bulk-%d-%d", i, j), benchmarkFlowKey)
items[j] = item
q.Add(item)
}
// Remove the same number of items
for range items {
peeked := q.Peek()
if peeked == nil {
b.Fatal("Peek failed")
}
if _, err := q.Remove(peeked.Handle()); err != nil {
b.Fatalf("Remove failed: %v", err)
}
}
}
}
// benchmarkHighContention simulates a more realistic workload with multiple producers and consumers operating on the
// queue concurrently.
func benchmarkHighContention(b *testing.B, q contracts.SafeQueue) {
// Pre-fill the queue to ensure consumers have work to do immediately.
for i := range 1000 {
item := mocks.NewMockQueueItemAccessor(1, fmt.Sprintf("prefill-%d", i), benchmarkFlowKey)
q.Add(item)
}
stopCh := make(chan struct{})
var wgProducers sync.WaitGroup
// Start producer goroutines to run in the background.
for range 4 {
wgProducers.Go(func() {
for {
select {
case <-stopCh:
return
default:
item := mocks.NewMockQueueItemAccessor(1, "item", benchmarkFlowKey)
q.Add(item)
}
}
})
}
b.ReportAllocs()
b.ResetTimer()
// Consumers drive the benchmark.
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
peeked := q.Peek()
if peeked != nil {
_, _ = q.Remove(peeked.Handle())
}
}
})
b.StopTimer()
close(stopCh) // Signal producers to stop.
wgProducers.Wait()
}