benchmark test to evaluate the overhead of podMatchesScopeFunc

This commit is contained in:
Wei Huang 2023-05-04 16:55:32 -07:00
parent edd032e22b
commit 359bcec0e0
No known key found for this signature in database
GPG Key ID: 17AFE05D01EA77B2
3 changed files with 49 additions and 3 deletions

View File

@ -328,8 +328,9 @@ func podMatchesScopeFunc(selector corev1.ScopedResourceSelectorRequirement, obje
case corev1.ResourceQuotaScopeNotBestEffort:
return !isBestEffort(pod), nil
case corev1.ResourceQuotaScopePriorityClass:
if len(selector.Operator) == 0 && selector.Values == nil {
// this is just checking for existence of a priorityClass on the pod
if selector.Operator == corev1.ScopeSelectorOpExists {
// This is just checking for existence of a priorityClass on the pod,
// no need to take the overhead of selector parsing/evaluation.
return len(pod.Spec.PriorityClassName) != 0, nil
}
return podMatchesSelector(pod, selector)

View File

@ -1105,6 +1105,51 @@ func TestPodEvaluatorUsageResourceResize(t *testing.T) {
}
}
func BenchmarkPodMatchesScopeFunc(b *testing.B) {
pod, _ := toExternalPodOrError(makePod("p1", "high-priority",
api.ResourceList{api.ResourceCPU: resource.MustParse("1")}, api.PodRunning))
tests := []struct {
name string
selector corev1.ScopedResourceSelectorRequirement
}{
{
name: "PriorityClass selector w/o operator",
selector: corev1.ScopedResourceSelectorRequirement{
ScopeName: corev1.ResourceQuotaScopePriorityClass,
},
},
{
name: "PriorityClass selector w/ 'Exists' operator",
selector: corev1.ScopedResourceSelectorRequirement{
ScopeName: corev1.ResourceQuotaScopePriorityClass,
Operator: corev1.ScopeSelectorOpExists,
},
},
{
name: "BestEfforts selector w/o operator",
selector: corev1.ScopedResourceSelectorRequirement{
ScopeName: corev1.ResourceQuotaScopeBestEffort,
},
},
{
name: "BestEfforts selector w/ 'Exists' operator",
selector: corev1.ScopedResourceSelectorRequirement{
ScopeName: corev1.ResourceQuotaScopeBestEffort,
Operator: corev1.ScopeSelectorOpExists,
},
},
}
for _, tt := range tests {
b.Run(tt.name, func(b *testing.B) {
for n := 0; n < b.N; n++ {
_, _ = podMatchesScopeFunc(tt.selector, pod)
}
})
}
}
func mockListerForResourceFunc(listerForResource map[schema.GroupVersionResource]cache.GenericLister) quota.ListerForResourceFunc {
return func(gvr schema.GroupVersionResource) (cache.GenericLister, error) {
lister, found := listerForResource[gvr]

View File

@ -199,7 +199,7 @@ func CalculateUsageStats(options quota.UsageStatsOptions,
// need to verify that the item matches the set of scopes
matchesScopes := true
for _, scope := range options.Scopes {
innerMatch, err := scopeFunc(corev1.ScopedResourceSelectorRequirement{ScopeName: scope}, item)
innerMatch, err := scopeFunc(corev1.ScopedResourceSelectorRequirement{ScopeName: scope, Operator: corev1.ScopeSelectorOpExists}, item)
if err != nil {
return result, nil
}