Commit 83daab16 authored by root's avatar root

wrr ut

parent f375e9b6
...@@ -10,6 +10,7 @@ ...@@ -10,6 +10,7 @@
#include "butil/gperftools_profiler.h" #include "butil/gperftools_profiler.h"
#include "butil/time.h" #include "butil/time.h"
#include "butil/containers/doubly_buffered_data.h" #include "butil/containers/doubly_buffered_data.h"
#include "brpc/describable.h"
#include "brpc/socket.h" #include "brpc/socket.h"
#include "butil/strings/string_number_conversions.h" #include "butil/strings/string_number_conversions.h"
#include "brpc/policy/weighted_round_robin_load_balancer.h" #include "brpc/policy/weighted_round_robin_load_balancer.h"
...@@ -233,7 +234,7 @@ class SaveRecycle : public brpc::SocketUser { ...@@ -233,7 +234,7 @@ class SaveRecycle : public brpc::SocketUser {
}; };
TEST_F(LoadBalancerTest, update_while_selection) { TEST_F(LoadBalancerTest, update_while_selection) {
for (size_t round = 0; round < 4; ++round) { for (size_t round = 0; round < 5; ++round) {
brpc::LoadBalancer* lb = NULL; brpc::LoadBalancer* lb = NULL;
SelectArg sa = { NULL, NULL}; SelectArg sa = { NULL, NULL};
bool is_lalb = false; bool is_lalb = false;
...@@ -244,6 +245,8 @@ TEST_F(LoadBalancerTest, update_while_selection) { ...@@ -244,6 +245,8 @@ TEST_F(LoadBalancerTest, update_while_selection) {
} else if (round == 2) { } else if (round == 2) {
lb = new LALB; lb = new LALB;
is_lalb = true; is_lalb = true;
} else if (round == 3) {
lb = new brpc::policy::WeightedRoundRobinLoadBalancer;
} else { } else {
lb = new brpc::policy::ConsistentHashingLoadBalancer( lb = new brpc::policy::ConsistentHashingLoadBalancer(
::brpc::policy::MurmurHash32); ::brpc::policy::MurmurHash32);
...@@ -267,6 +270,9 @@ TEST_F(LoadBalancerTest, update_while_selection) { ...@@ -267,6 +270,9 @@ TEST_F(LoadBalancerTest, update_while_selection) {
butil::EndPoint dummy; butil::EndPoint dummy;
ASSERT_EQ(0, str2endpoint(addr, &dummy)); ASSERT_EQ(0, str2endpoint(addr, &dummy));
brpc::ServerId id(8888); brpc::ServerId id(8888);
if (3 == round) {
id.tag = "1";
}
brpc::SocketOptions options; brpc::SocketOptions options;
options.remote_side = dummy; options.remote_side = dummy;
options.user = new SaveRecycle; options.user = new SaveRecycle;
...@@ -344,7 +350,7 @@ TEST_F(LoadBalancerTest, update_while_selection) { ...@@ -344,7 +350,7 @@ TEST_F(LoadBalancerTest, update_while_selection) {
} }
TEST_F(LoadBalancerTest, fairness) { TEST_F(LoadBalancerTest, fairness) {
for (size_t round = 0; round < 4; ++round) { for (size_t round = 0; round < 6; ++round) {
brpc::LoadBalancer* lb = NULL; brpc::LoadBalancer* lb = NULL;
SelectArg sa = { NULL, NULL}; SelectArg sa = { NULL, NULL};
if (round == 0) { if (round == 0) {
...@@ -353,6 +359,8 @@ TEST_F(LoadBalancerTest, fairness) { ...@@ -353,6 +359,8 @@ TEST_F(LoadBalancerTest, fairness) {
lb = new brpc::policy::RandomizedLoadBalancer; lb = new brpc::policy::RandomizedLoadBalancer;
} else if (round == 2) { } else if (round == 2) {
lb = new LALB; lb = new LALB;
} else if (3 == round || 4 == round) {
lb = new brpc::policy::WeightedRoundRobinLoadBalancer;
} else { } else {
lb = new brpc::policy::ConsistentHashingLoadBalancer( lb = new brpc::policy::ConsistentHashingLoadBalancer(
brpc::policy::MurmurHash32); brpc::policy::MurmurHash32);
...@@ -377,6 +385,15 @@ TEST_F(LoadBalancerTest, fairness) { ...@@ -377,6 +385,15 @@ TEST_F(LoadBalancerTest, fairness) {
butil::EndPoint dummy; butil::EndPoint dummy;
ASSERT_EQ(0, str2endpoint(addr, &dummy)); ASSERT_EQ(0, str2endpoint(addr, &dummy));
brpc::ServerId id(8888); brpc::ServerId id(8888);
if (3 == round) {
id.tag = "100";
} else if (4 == round) {
if ( i % 50 == 0) {
id.tag = std::to_string(i / 50 * 100 + butil::fast_rand_less_than(40) + 80);
} else {
id.tag = std::to_string(butil::fast_rand_less_than(40) + 80);
}
}
brpc::SocketOptions options; brpc::SocketOptions options;
options.remote_side = dummy; options.remote_side = dummy;
options.user = new SaveRecycle; options.user = new SaveRecycle;
...@@ -420,6 +437,15 @@ TEST_F(LoadBalancerTest, fairness) { ...@@ -420,6 +437,15 @@ TEST_F(LoadBalancerTest, fairness) {
size_t count_sum = 0; size_t count_sum = 0;
size_t count_squared_sum = 0; size_t count_squared_sum = 0;
std::cout << lb_name << ':' << '\n'; std::cout << lb_name << ':' << '\n';
if (3 == round || 4 == round) {
std::cout << "configured weight: " << std::endl;
std::ostringstream os;
brpc::DescribeOptions opt;
lb->Describe(os, opt);
std::cout << os.str() << std::endl;
}
if (round != 3 && round !=4) {
for (size_t i = 0; i < ids.size(); ++i) { for (size_t i = 0; i < ids.size(); ++i) {
size_t count = total_count[ids[i].id]; size_t count = total_count[ids[i].id];
ASSERT_NE(0ul, count) << "i=" << i; ASSERT_NE(0ul, count) << "i=" << i;
...@@ -430,8 +456,24 @@ TEST_F(LoadBalancerTest, fairness) { ...@@ -430,8 +456,24 @@ TEST_F(LoadBalancerTest, fairness) {
std::cout << '\n' std::cout << '\n'
<< ": average=" << count_sum/ids.size() << ": average=" << count_sum/ids.size()
<< " deviation=" << sqrt(count_squared_sum * ids.size() - count_sum * count_sum) / ids.size() << std::endl; << " deviation=" << sqrt(count_squared_sum * ids.size()
- count_sum * count_sum) / ids.size() << std::endl;
} else { // for weighted round robin load balancer
double scaling_count_sum = 0.0;
double scaling_count_squared_sum = 0.0;
for (size_t i = 0; i < ids.size(); ++i) {
size_t count = total_count[ids[i].id];
ASSERT_NE(0ul, count) << "i=" << i;
std::cout << i << '=' << count << ' ';
double scaling_count = static_cast<double>(count) / std::stoi(ids[i].tag);
scaling_count_sum += scaling_count;
scaling_count_squared_sum += scaling_count * scaling_count;
}
std::cout << '\n'
<< ": scaling average=" << scaling_count_sum/ids.size()
<< " scaling deviation=" << sqrt(scaling_count_squared_sum * ids.size()
- scaling_count_sum * scaling_count_sum) / ids.size() << std::endl;
}
for (size_t i = 0; i < ids.size(); ++i) { for (size_t i = 0; i < ids.size(); ++i) {
ASSERT_EQ(0, brpc::Socket::SetFailed(ids[i].id)); ASSERT_EQ(0, brpc::Socket::SetFailed(ids[i].id));
} }
...@@ -530,7 +572,7 @@ TEST_F(LoadBalancerTest, weighted_round_robin) { ...@@ -530,7 +572,7 @@ TEST_F(LoadBalancerTest, weighted_round_robin) {
brpc::policy::WeightedRoundRobinLoadBalancer wrrlb; brpc::policy::WeightedRoundRobinLoadBalancer wrrlb;
// Add server to selected list. The server with invalid weight will be skipped. // Add server to selected list. The server with invalid weight will be skipped.
for (int i = 0; i < 6; ++i) { for (size_t i = 0; i < ARRAY_SIZE(servers); ++i) {
const char *addr = servers[i]; const char *addr = servers[i];
butil::EndPoint dummy; butil::EndPoint dummy;
ASSERT_EQ(0, str2endpoint(addr, &dummy)); ASSERT_EQ(0, str2endpoint(addr, &dummy));
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment