Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,11 @@ func (p *MaxScorePicker) TypedName() fwkplugin.TypedName {

// Pick selects the endpoint(s) with the highest score calculated during the scoring phase.
func (p *MaxScorePicker) Pick(ctx context.Context, scoredEndpoints []*fwksched.ScoredEndpoint) *fwksched.ProfileRunResult {
log.FromContext(ctx).V(logutil.DEBUG).Info("Selecting endpoints from candidates sorted by max score", "max-num-of-endpoints", p.maxNumOfEndpoints,
"num-of-candidates", len(scoredEndpoints), "scored-endpoints", scoredEndpoints)
logger := log.FromContext(ctx)
if logger.V(logutil.DEBUG).Enabled() {
logger.V(logutil.DEBUG).Info("Selecting endpoints from candidates sorted by max score", "max-num-of-endpoints", p.maxNumOfEndpoints,
"num-of-candidates", len(scoredEndpoints), "scored-endpoints", scoredEndpoints)
}

slices.SortStableFunc(scoredEndpoints, func(i, j *fwksched.ScoredEndpoint) int { // highest score first
if i.Score > j.Score {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -312,3 +312,26 @@ func TestPickMaxScorePicker(t *testing.T) {
})
}
}

func BenchmarkMaxScorePicker_Pick(b *testing.B) {
numPods := 8
endpoints := make([]fwksched.Endpoint, numPods)
for i := 0; i < numPods; i++ {
endpoints[i] = fwksched.NewEndpoint(&fwkdl.EndpointMetadata{
ID: k8stypes.NamespacedName{Name: fmt.Sprintf("pod%d", i)},
}, nil, nil)
}

picker := NewMaxScorePicker(1)
ctx := context.Background()

b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
scored := make([]*fwksched.ScoredEndpoint, numPods)
for j := 0; j < numPods; j++ {
scored[j] = &fwksched.ScoredEndpoint{Endpoint: endpoints[j], Score: float64(j * 10)}
}
_ = picker.Pick(ctx, scored)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,11 @@ func (p *RandomPicker) TypedName() fwkplugin.TypedName {

// Pick selects random endpoint(s) from the list of candidates.
func (p *RandomPicker) Pick(ctx context.Context, scoredEndpoints []*fwksched.ScoredEndpoint) *fwksched.ProfileRunResult {
log.FromContext(ctx).V(logutil.DEBUG).Info("Selecting endpoints from candidates randomly", "max-num-of-endpoints", p.maxNumOfEndpoints,
"num-of-candidates", len(scoredEndpoints), "scored-endpoints", scoredEndpoints)
logger := log.FromContext(ctx)
if logger.V(logutil.DEBUG).Enabled() {
logger.V(logutil.DEBUG).Info("Selecting endpoints from candidates randomly", "max-num-of-endpoints", p.maxNumOfEndpoints,
"num-of-candidates", len(scoredEndpoints), "scored-endpoints", scoredEndpoints)
}

// Shuffle to ensure uniform random selection.
picker.ShuffleScoredEndpoints(scoredEndpoints)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,14 +109,19 @@ func (p *WeightedRandomPicker) TypedName() fwkplugin.TypedName {
// Pick selects the endpoint(s) randomly from the list of candidates, where the probability of the endpoint to get picked is derived
// from its weighted score.
func (p *WeightedRandomPicker) Pick(ctx context.Context, scoredEndpoints []*fwksched.ScoredEndpoint) *fwksched.ProfileRunResult {
logger := log.FromContext(ctx)
// Check if there is at least one endpoint with Score > 0, if not let random picker run
if slices.IndexFunc(scoredEndpoints, func(scoredEndpoint *fwksched.ScoredEndpoint) bool { return scoredEndpoint.Score > 0 }) == -1 {
log.FromContext(ctx).V(logutil.DEBUG).Info("All scores are zero, delegating to RandomPicker for uniform selection")
if logger.V(logutil.DEBUG).Enabled() {
logger.V(logutil.DEBUG).Info("All scores are zero, delegating to RandomPicker for uniform selection")
}
return p.randomPicker.Pick(ctx, scoredEndpoints)
}

log.FromContext(ctx).V(logutil.DEBUG).Info("Selecting endpoints from candidates by random weighted picker", "max-num-of-endpoints", p.maxNumOfEndpoints,
"num-of-candidates", len(scoredEndpoints), "scored-endpoints", scoredEndpoints)
if logger.V(logutil.DEBUG).Enabled() {
logger.V(logutil.DEBUG).Info("Selecting endpoints from candidates by random weighted picker", "max-num-of-endpoints", p.maxNumOfEndpoints,
"num-of-candidates", len(scoredEndpoints), "scored-endpoints", scoredEndpoints)
}

// A-Res algorithm: keyᵢ = Uᵢ^(1/wᵢ)
weightedEndpoints := make([]weightedScoredEndpoint, len(scoredEndpoints))
Expand Down
Loading