benchmark test to evaluate the overhead of podMatchesScopeFunc
This commit is contained in:
		@@ -328,8 +328,9 @@ func podMatchesScopeFunc(selector corev1.ScopedResourceSelectorRequirement, obje
 | 
				
			|||||||
	case corev1.ResourceQuotaScopeNotBestEffort:
 | 
						case corev1.ResourceQuotaScopeNotBestEffort:
 | 
				
			||||||
		return !isBestEffort(pod), nil
 | 
							return !isBestEffort(pod), nil
 | 
				
			||||||
	case corev1.ResourceQuotaScopePriorityClass:
 | 
						case corev1.ResourceQuotaScopePriorityClass:
 | 
				
			||||||
		if len(selector.Operator) == 0 && selector.Values == nil {
 | 
							if selector.Operator == corev1.ScopeSelectorOpExists {
 | 
				
			||||||
			// this is just checking for existence of a priorityClass on the pod
 | 
								// 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 len(pod.Spec.PriorityClassName) != 0, nil
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		return podMatchesSelector(pod, selector)
 | 
							return podMatchesSelector(pod, selector)
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -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 {
 | 
					func mockListerForResourceFunc(listerForResource map[schema.GroupVersionResource]cache.GenericLister) quota.ListerForResourceFunc {
 | 
				
			||||||
	return func(gvr schema.GroupVersionResource) (cache.GenericLister, error) {
 | 
						return func(gvr schema.GroupVersionResource) (cache.GenericLister, error) {
 | 
				
			||||||
		lister, found := listerForResource[gvr]
 | 
							lister, found := listerForResource[gvr]
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -199,7 +199,7 @@ func CalculateUsageStats(options quota.UsageStatsOptions,
 | 
				
			|||||||
		// need to verify that the item matches the set of scopes
 | 
							// need to verify that the item matches the set of scopes
 | 
				
			||||||
		matchesScopes := true
 | 
							matchesScopes := true
 | 
				
			||||||
		for _, scope := range options.Scopes {
 | 
							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 {
 | 
								if err != nil {
 | 
				
			||||||
				return result, nil
 | 
									return result, nil
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user