Copilot commented on code in PR #915:
URL: https://github.com/apache/dubbo-go-pixiu/pull/915#discussion_r3106158949


##########
pkg/model/cluster.go:
##########
@@ -60,7 +60,7 @@ type (
                ConsistentHash       ConsistentHash      `yaml:"consistent" 
json:"consistent"` // Consistent hash config info
                HealthChecks         []HealthCheckConfig `yaml:"health_checks" 
json:"health_checks"`
                Endpoints            []*Endpoint         `yaml:"endpoints" 
json:"endpoints"`
-               PrePickEndpointIndex int
+               PrePickEndpointIndex uint32

Review Comment:
   `PrePickEndpointIndex` is runtime-only cursor state for round-robin, but as 
an exported field without `yaml:"-"/json:"-"` tags it will be serialized/cloned 
(e.g., via `yaml.Marshal` in `ClusterManager.CloneStore`). Consider adding 
`yaml:"-" json:"-"` (and ideally a short comment) to prevent this internal 
state from leaking into config/XDS snapshots and to keep cloning purely 
configuration-driven.
   ```suggestion
                PrePickEndpointIndex uint32              `yaml:"-" json:"-"` // 
runtime-only round-robin cursor state
   ```



##########
pkg/server/cluster_manager_bench_test.go:
##########
@@ -0,0 +1,221 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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 server
+
+import (
+       "fmt"
+       "sync/atomic"
+       "testing"
+
+       "github.com/stretchr/testify/require"
+
+       _ "github.com/apache/dubbo-go-pixiu/pkg/cluster/loadbalancer/maglev"
+       _ "github.com/apache/dubbo-go-pixiu/pkg/cluster/loadbalancer/rand"
+       _ "github.com/apache/dubbo-go-pixiu/pkg/cluster/loadbalancer/ringhash"
+       _ "github.com/apache/dubbo-go-pixiu/pkg/cluster/loadbalancer/roundrobin"
+       "github.com/apache/dubbo-go-pixiu/pkg/model"
+)
+
+var (
+       // Keep benchmark results live so the compiler cannot optimize the hot 
path away.
+       benchmarkEndpointSink  *model.Endpoint
+       benchmarkEndpointsSink []*model.Endpoint
+       benchmarkClusterSink   *model.ClusterConfig
+)
+
+type benchmarkHashPolicy string
+
+func (p benchmarkHashPolicy) GenerateHash() string {
+       return string(p)
+}
+
+func BenchmarkClusterPickEndpointSerial(b *testing.B) {
+       for _, lbType := range []model.LbPolicyType{model.LoadBalancerRand, 
model.LoadBalancerRoundRobin} {
+               for _, clusterCount := range []int{1, 32, 256, 1024} {
+                       b.Run(fmt.Sprintf("%s/clusters=%d", lbType, 
clusterCount), func(b *testing.B) {
+                               cm, names := 
benchmarkClusterManager(clusterCount, 4, lbType)
+
+                               b.ReportAllocs()
+                               b.ResetTimer()
+                               for i := 0; i < b.N; i++ {
+                                       benchmarkEndpointSink = 
cm.PickEndpoint(names[i%len(names)], nil)
+                               }
+                       })
+               }
+       }
+}
+
+func BenchmarkClusterLookupSerial(b *testing.B) {
+       for _, clusterCount := range []int{1, 32, 256, 1024} {
+               b.Run(fmt.Sprintf("clusters=%d", clusterCount), func(b 
*testing.B) {
+                       cm, names := benchmarkClusterManager(clusterCount, 4, 
model.LoadBalancerRoundRobin)
+
+                       b.ReportAllocs()
+                       b.ResetTimer()
+                       for i := 0; i < b.N; i++ {
+                               cm.rw.RLock()
+                               benchmarkClusterSink = 
cm.getCluster(names[i%len(names)])
+                               cm.rw.RUnlock()
+                       }
+               })
+       }
+}
+
+func BenchmarkClusterPickEndpointParallel(b *testing.B) {
+       for _, lbType := range []model.LbPolicyType{model.LoadBalancerRand, 
model.LoadBalancerRoundRobin} {
+               b.Run(string(lbType), func(b *testing.B) {
+                       cm, names := benchmarkClusterManager(256, 4, lbType)
+                       var workerCounter uint64
+
+                       b.ReportAllocs()
+                       b.ResetTimer()
+                       b.RunParallel(func(pb *testing.PB) {
+                               // Give each worker a stable starting offset 
once so bookkeeping stays off the hot path.
+                               idx := int(atomic.AddUint64(&workerCounter, 
1)-1) % len(names)
+                               var endpoint *model.Endpoint
+                               for pb.Next() {
+                                       endpoint = cm.PickEndpoint(names[idx], 
nil)
+                                       idx++
+                                       if idx == len(names) {
+                                               idx = 0
+                                       }
+                               }
+                               benchmarkEndpointSink = endpoint
+                       })
+               })
+       }
+}
+
+func BenchmarkClusterLoadBalancerHotPathSerial(b *testing.B) {
+       for _, lbType := range []model.LbPolicyType{model.LoadBalancerRand, 
model.LoadBalancerRoundRobin} {
+               for _, endpointCount := range []int{4, 64, 512} {
+                       b.Run(fmt.Sprintf("%s/endpoints=%d", lbType, 
endpointCount), func(b *testing.B) {
+                               cm := &ClusterManager{}
+                               cluster := 
benchmarkClusterConfig("lb-hot-path", lbType, endpointCount, 0)
+
+                               b.ReportAllocs()
+                               b.ResetTimer()
+                               for i := 0; i < b.N; i++ {
+                                       benchmarkEndpointSink = 
cm.pickOneEndpoint(cluster, nil)
+                               }
+                       })
+               }
+       }
+}
+
+func BenchmarkClusterHealthyFilterCost(b *testing.B) {
+       for _, endpointCount := range []int{8, 64, 512} {
+               for _, healthyRatio := range []int{100, 50, 0} {
+                       b.Run(fmt.Sprintf("endpoints=%d/healthy=%d", 
endpointCount, healthyRatio), func(b *testing.B) {
+                               cluster := 
benchmarkClusterConfig("healthy-filter", model.LoadBalancerRoundRobin, 
endpointCount, 0)
+                               healthyCount := endpointCount * healthyRatio / 
100
+                               for i := healthyCount; i < 
len(cluster.Endpoints); i++ {
+                                       cluster.Endpoints[i].UnHealthy = true
+                               }
+
+                               b.ReportAllocs()
+                               b.ResetTimer()
+                               for i := 0; i < b.N; i++ {
+                                       benchmarkEndpointsSink = 
cluster.GetEndpoint(true)
+                               }
+                       })
+               }
+       }
+}
+
+func BenchmarkClusterCompareAndSetStoreMixed(b *testing.B) {
+       cm, names := benchmarkClusterManager(128, 4, 
model.LoadBalancerRoundRobin)
+       readIndex := 0
+
+       b.ReportAllocs()
+       b.ResetTimer()
+       for i := 0; i < b.N; i++ {
+               if i%20 == 19 {
+                       newStore, err := benchmarkFreshStore(cm, names, 
model.LoadBalancerRoundRobin, 4)
+                       require.NoError(b, err)
+                       require.True(b, cm.CompareAndSetStore(newStore))
+                       continue

Review Comment:
   This benchmark uses `require.NoError` / `require.True` inside the timed loop 
(after `b.ResetTimer()`), which adds non-trivial overhead (formatting, 
allocations) and can skew the reported numbers. Prefer replacing these with 
minimal `if err != nil { b.Fatal(...) }` checks or moving validation outside 
the measured loop where possible.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to