Copilot commented on code in PR #3426:
URL: https://github.com/apache/brpc/pull/3426#discussion_r3700954669
##########
test/brpc_load_balancer_unittest.cpp:
##########
@@ -1026,6 +1026,55 @@ TEST_F(LoadBalancerTest, weighted_randomized) {
}
}
+TEST_F(LoadBalancerTest, weighted_randomized_equal_weight) {
+ // With equal weights every server must get the same share of the traffic.
+ // The tolerance of `weighted_randomized` above is +/-2x, which is too
loose
+ // to catch a single misplaced slot, so check the distribution tightly
here.
+ const char* servers[] = {
+ "10.92.115.19:8831",
+ "10.42.108.25:8832",
+ "10.36.150.31:8833",
+ "10.36.150.32:8899"
+ };
+ brpc::policy::WeightedRandomizedLoadBalancer wrlb;
+ for (size_t i = 0; i < ARRAY_SIZE(servers); ++i) {
+ butil::EndPoint dummy;
+ ASSERT_EQ(0, str2endpoint(servers[i], &dummy));
+ brpc::ServerId id(8888);
+ brpc::SocketOptions options;
+ options.remote_side = dummy;
+ options.user = new SaveRecycle;
+ ASSERT_EQ(0, brpc::Socket::Create(options, &id.id));
+ id.tag = "1";
+ ASSERT_TRUE(wrlb.AddServer(id));
+ }
+
+ std::map<butil::EndPoint, size_t> select_result;
+ brpc::SocketUniquePtr ptr;
+ brpc::LoadBalancer::SelectIn in = { 0, false, false, 0u, NULL };
+ brpc::LoadBalancer::SelectOut out(&ptr);
+ const int run_times = 40000;
+ for (int i = 0; i < run_times; ++i) {
+ ASSERT_EQ(0, wrlb.SelectServer(in, &out));
+ ++select_result[ptr->remote_side()];
+ }
+
+ // Every server must be selected at least once, in particular the one added
+ // last, which owns the largest prefix sum.
+ ASSERT_EQ(ARRAY_SIZE(servers), select_result.size());
+ const double expect_rate = 1.0 / ARRAY_SIZE(servers);
+ for (const auto& result : select_result) {
+ const double actual_rate = result.second * 1.0 / run_times;
+ std::cout << result.first << " select_times=" << result.second
+ << " actual_rate=" << actual_rate
+ << " expect_rate=" << expect_rate << std::endl;
+ // 0.9x ~ 1.1x of the expected rate, which is more than 20 standard
+ // deviations away from the mean at this number of runs.
Review Comment:
The comment about the tolerance being “more than 20 standard deviations” is
numerically incorrect for this test setup (run_times=40000, p=0.25). The
0.9x~1.1x band is ~11.5σ wide, not >20σ. Please update the comment to avoid
misleading readers.
--
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]