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
21 changes: 21 additions & 0 deletions mongo/description/selector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,27 @@ func BenchmarkSelector_Sharded(b *testing.B) {
}
}

func Benchmark_SelectServer_SelectServer(b *testing.B) {
topology := Topology{Kind: ReplicaSet} // You can change the topology as needed
candidates := []Server{
{Kind: Mongos},
{Kind: RSPrimary},
{Kind: Standalone},
}

selector := writeServerSelector{} // Assuming this is the receiver type

b.ReportAllocs()
b.ResetTimer()

for i := 0; i < b.N; i++ {
_, err := selector.SelectServer(topology, candidates)
if err != nil {
b.Fatalf("Error selecting server: %v", err)
}
}
}

func TestSelector_Single(t *testing.T) {
t.Parallel()

Expand Down
12 changes: 11 additions & 1 deletion mongo/description/server_selector.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,17 @@ func (writeServerSelector) SelectServer(t Topology, candidates []Server) ([]Serv
case Single, LoadBalanced:
return candidates, nil
default:
result := []Server{}
// Determine the capacity of the results slice.
selected := 0
for _, candidate := range candidates {
switch candidate.Kind {
case Mongos, RSPrimary, Standalone:
selected++
}
}

// Append candidates to the results slice.
result := make([]Server, 0, selected)
for _, candidate := range candidates {
switch candidate.Kind {
case Mongos, RSPrimary, Standalone:
Expand Down