File: store_benchmarks.go

package info (click to toggle)
golang-k8s-apiserver 0.32.7-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 11,356 kB
  • sloc: sh: 236; makefile: 5
file content (261 lines) | stat: -rw-r--r-- 8,258 bytes parent folder | download
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
/*
Copyright 2024 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 testing

import (
	"context"
	"fmt"
	"sync"
	"sync/atomic"
	"testing"

	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	"k8s.io/apimachinery/pkg/fields"
	"k8s.io/apimachinery/pkg/labels"
	"k8s.io/apimachinery/pkg/runtime"
	"k8s.io/apimachinery/pkg/util/rand"
	"k8s.io/apiserver/pkg/apis/example"
	"k8s.io/apiserver/pkg/storage"
)

func RunBenchmarkStoreListCreate(ctx context.Context, b *testing.B, store storage.Interface, match metav1.ResourceVersionMatch) {
	objectCount := atomic.Uint64{}
	pods := []*example.Pod{}
	for i := 0; i < b.N; i++ {
		name := rand.String(100)
		pods = append(pods, &example.Pod{ObjectMeta: metav1.ObjectMeta{Namespace: "ns", Name: name}})
	}
	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		pod := pods[i]
		podOut := &example.Pod{}
		err := store.Create(ctx, computePodKey(pod), pod, podOut, 0)
		if err != nil {
			panic(fmt.Sprintf("Unexpected error %s", err))
		}
		listOut := &example.PodList{}
		err = store.GetList(ctx, "/pods", storage.ListOptions{
			Recursive:            true,
			ResourceVersion:      podOut.ResourceVersion,
			ResourceVersionMatch: match,
			Predicate: storage.SelectionPredicate{
				Label: labels.Everything(),
				Field: fields.Everything(),
				Limit: 1,
			},
		}, listOut)
		if err != nil {
			panic(fmt.Sprintf("Unexpected error %s", err))
		}
		if len(listOut.Items) != 1 {
			b.Errorf("Expected to get 1 element, got %d", len(listOut.Items))
		}
		objectCount.Add(uint64(len(listOut.Items)))
	}
	b.ReportMetric(float64(objectCount.Load())/float64(b.N), "objects/op")
}

func RunBenchmarkStoreList(ctx context.Context, b *testing.B, store storage.Interface) {
	namespaceCount := 100
	podPerNamespaceCount := 100
	var paginateLimit int64 = 100
	nodeCount := 100
	namespacedNames, nodeNames := prepareBenchchmarkData(ctx, store, namespaceCount, podPerNamespaceCount, nodeCount)
	b.ResetTimer()
	maxRevision := 1 + namespaceCount*podPerNamespaceCount
	cases := []struct {
		name  string
		match metav1.ResourceVersionMatch
	}{
		{
			name:  "RV=Empty",
			match: "",
		},
		{
			name:  "RV=NotOlderThan",
			match: metav1.ResourceVersionMatchNotOlderThan,
		},
		{
			name:  "RV=MatchExact",
			match: metav1.ResourceVersionMatchExact,
		},
	}

	for _, c := range cases {
		b.Run(c.name, func(b *testing.B) {
			runBenchmarkStoreList(ctx, b, store, 0, maxRevision, c.match, false, nodeNames)
		})
	}
	b.Run("Paginate", func(b *testing.B) {
		for _, c := range cases {
			b.Run(c.name, func(b *testing.B) {
				runBenchmarkStoreList(ctx, b, store, paginateLimit, maxRevision, c.match, false, nodeNames)
			})
		}
	})
	b.Run("NodeIndexed", func(b *testing.B) {
		for _, c := range cases {
			b.Run(c.name, func(b *testing.B) {
				runBenchmarkStoreList(ctx, b, store, 0, maxRevision, c.match, true, nodeNames)
			})
		}
		b.Run("Paginate", func(b *testing.B) {
			for _, c := range cases {
				b.Run(c.name, func(b *testing.B) {
					runBenchmarkStoreList(ctx, b, store, paginateLimit, maxRevision, c.match, true, nodeNames)
				})
			}
		})
	})
	b.Run("Namespace", func(b *testing.B) {
		for _, c := range cases {
			b.Run(c.name, func(b *testing.B) {
				runBenchmarkStoreListNamespace(ctx, b, store, maxRevision, c.match, namespacedNames)
			})
		}
	})
}

func runBenchmarkStoreListNamespace(ctx context.Context, b *testing.B, store storage.Interface, maxRV int, match metav1.ResourceVersionMatch, namespaceNames []string) {
	wg := sync.WaitGroup{}
	objectCount := atomic.Uint64{}
	pageCount := atomic.Uint64{}
	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		wg.Add(1)
		resourceVersion := ""
		switch match {
		case metav1.ResourceVersionMatchExact, metav1.ResourceVersionMatchNotOlderThan:
			resourceVersion = fmt.Sprintf("%d", maxRV-99+i%100)
		}
		go func(resourceVersion string) {
			defer wg.Done()
			opts := storage.ListOptions{
				Recursive:            true,
				ResourceVersion:      resourceVersion,
				ResourceVersionMatch: match,
				Predicate:            storage.Everything,
			}
			for j := 0; j < len(namespaceNames); j++ {
				objects, pages := paginate(ctx, store, "/pods/"+namespaceNames[j], opts)
				objectCount.Add(uint64(objects))
				pageCount.Add(uint64(pages))
			}
		}(resourceVersion)
	}
	wg.Wait()
	b.ReportMetric(float64(objectCount.Load())/float64(b.N), "objects/op")
	b.ReportMetric(float64(pageCount.Load())/float64(b.N), "pages/op")
}

func runBenchmarkStoreList(ctx context.Context, b *testing.B, store storage.Interface, limit int64, maxRV int, match metav1.ResourceVersionMatch, perNode bool, nodeNames []string) {
	wg := sync.WaitGroup{}
	objectCount := atomic.Uint64{}
	pageCount := atomic.Uint64{}
	for i := 0; i < b.N; i++ {
		wg.Add(1)
		resourceVersion := ""
		switch match {
		case metav1.ResourceVersionMatchExact, metav1.ResourceVersionMatchNotOlderThan:
			resourceVersion = fmt.Sprintf("%d", maxRV-99+i%100)
		}
		go func(resourceVersion string) {
			defer wg.Done()
			opts := storage.ListOptions{
				Recursive:            true,
				ResourceVersion:      resourceVersion,
				ResourceVersionMatch: match,
				Predicate: storage.SelectionPredicate{
					GetAttrs: podAttr,
					Label:    labels.Everything(),
					Field:    fields.Everything(),
					Limit:    limit,
				},
			}
			if perNode {
				for _, nodeName := range nodeNames {
					opts.Predicate.GetAttrs = podAttr
					opts.Predicate.IndexFields = []string{"spec.nodeName"}
					opts.Predicate.Field = fields.SelectorFromSet(fields.Set{"spec.nodeName": nodeName})
					objects, pages := paginate(ctx, store, "/pods/", opts)
					objectCount.Add(uint64(objects))
					pageCount.Add(uint64(pages))
				}
			} else {
				objects, pages := paginate(ctx, store, "/pods/", opts)
				objectCount.Add(uint64(objects))
				pageCount.Add(uint64(pages))
			}
		}(resourceVersion)
	}
	wg.Wait()
	b.ReportMetric(float64(objectCount.Load())/float64(b.N), "objects/op")
	b.ReportMetric(float64(pageCount.Load())/float64(b.N), "pages/op")
}

func paginate(ctx context.Context, store storage.Interface, key string, opts storage.ListOptions) (objectCount int, pageCount int) {
	listOut := &example.PodList{}
	err := store.GetList(ctx, key, opts, listOut)
	if err != nil {
		panic(fmt.Sprintf("Unexpected error %s", err))
	}
	opts.Predicate.Continue = listOut.Continue
	opts.ResourceVersion = ""
	opts.ResourceVersionMatch = ""
	pageCount += 1
	objectCount += len(listOut.Items)
	for opts.Predicate.Continue != "" {
		listOut := &example.PodList{}
		err := store.GetList(ctx, key, opts, listOut)
		if err != nil {
			panic(fmt.Sprintf("Unexpected error %s", err))
		}
		opts.Predicate.Continue = listOut.Continue
		pageCount += 1
		objectCount += len(listOut.Items)
	}
	return objectCount, pageCount
}

func podAttr(obj runtime.Object) (labels.Set, fields.Set, error) {
	pod := obj.(*example.Pod)
	return nil, fields.Set{
		"spec.nodeName": pod.Spec.NodeName,
	}, nil
}

func prepareBenchchmarkData(ctx context.Context, store storage.Interface, namespaceCount, podPerNamespaceCount, nodeCount int) (namespaceNames, nodeNames []string) {
	nodeNames = make([]string, nodeCount)
	for i := 0; i < nodeCount; i++ {
		nodeNames[i] = rand.String(100)
	}
	namespaceNames = make([]string, nodeCount)
	out := &example.Pod{}
	for i := 0; i < namespaceCount; i++ {
		namespace := rand.String(100)
		namespaceNames[i] = namespace
		for j := 0; j < podPerNamespaceCount; j++ {
			name := rand.String(100)
			pod := &example.Pod{ObjectMeta: metav1.ObjectMeta{Namespace: namespace, Name: name}, Spec: example.PodSpec{NodeName: nodeNames[rand.Intn(nodeCount)]}}
			err := store.Create(ctx, computePodKey(pod), pod, out, 0)
			if err != nil {
				panic(fmt.Sprintf("Unexpected error %s", err))
			}
		}
	}
	return namespaceNames, nodeNames
}