Commit 83daab16 authored by root's avatar root

wrr ut

parent f375e9b6
......@@ -10,6 +10,7 @@
#include "butil/gperftools_profiler.h"
#include "butil/time.h"
#include "butil/containers/doubly_buffered_data.h"
#include "brpc/describable.h"
#include "brpc/socket.h"
#include "butil/strings/string_number_conversions.h"
#include "brpc/policy/weighted_round_robin_load_balancer.h"
......@@ -233,7 +234,7 @@ class SaveRecycle : public brpc::SocketUser {
};
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;
SelectArg sa = { NULL, NULL};
bool is_lalb = false;
......@@ -244,6 +245,8 @@ TEST_F(LoadBalancerTest, update_while_selection) {
} else if (round == 2) {
lb = new LALB;
is_lalb = true;
} else if (round == 3) {
lb = new brpc::policy::WeightedRoundRobinLoadBalancer;
} else {
lb = new brpc::policy::ConsistentHashingLoadBalancer(
::brpc::policy::MurmurHash32);
......@@ -267,6 +270,9 @@ TEST_F(LoadBalancerTest, update_while_selection) {
butil::EndPoint dummy;
ASSERT_EQ(0, str2endpoint(addr, &dummy));
brpc::ServerId id(8888);
if (3 == round) {
id.tag = "1";
}
brpc::SocketOptions options;
options.remote_side = dummy;
options.user = new SaveRecycle;
......@@ -344,7 +350,7 @@ TEST_F(LoadBalancerTest, update_while_selection) {
}
TEST_F(LoadBalancerTest, fairness) {
for (size_t round = 0; round < 4; ++round) {
for (size_t round = 0; round < 6; ++round) {
brpc::LoadBalancer* lb = NULL;
SelectArg sa = { NULL, NULL};
if (round == 0) {
......@@ -353,6 +359,8 @@ TEST_F(LoadBalancerTest, fairness) {
lb = new brpc::policy::RandomizedLoadBalancer;
} else if (round == 2) {
lb = new LALB;
} else if (3 == round || 4 == round) {
lb = new brpc::policy::WeightedRoundRobinLoadBalancer;
} else {
lb = new brpc::policy::ConsistentHashingLoadBalancer(
brpc::policy::MurmurHash32);
......@@ -377,6 +385,15 @@ TEST_F(LoadBalancerTest, fairness) {
butil::EndPoint dummy;
ASSERT_EQ(0, str2endpoint(addr, &dummy));
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;
options.remote_side = dummy;
options.user = new SaveRecycle;
......@@ -420,18 +437,43 @@ TEST_F(LoadBalancerTest, fairness) {
size_t count_sum = 0;
size_t count_squared_sum = 0;
std::cout << lb_name << ':' << '\n';
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 << ' ';
count_sum += count;
count_squared_sum += count * count;
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;
}
std::cout << '\n'
<< ": average=" << count_sum/ids.size()
<< " deviation=" << sqrt(count_squared_sum * ids.size() - count_sum * count_sum) / ids.size() << std::endl;
if (round != 3 && round !=4) {
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 << ' ';
count_sum += count;
count_squared_sum += count * count;
}
std::cout << '\n'
<< ": average=" << count_sum/ids.size()
<< " 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) {
ASSERT_EQ(0, brpc::Socket::SetFailed(ids[i].id));
}
......@@ -530,8 +572,8 @@ TEST_F(LoadBalancerTest, weighted_round_robin) {
brpc::policy::WeightedRoundRobinLoadBalancer wrrlb;
// Add server to selected list. The server with invalid weight will be skipped.
for (int i = 0; i < 6; ++i) {
const char *addr = servers[i];
for (size_t i = 0; i < ARRAY_SIZE(servers); ++i) {
const char *addr = servers[i];
butil::EndPoint dummy;
ASSERT_EQ(0, str2endpoint(addr, &dummy));
brpc::ServerId id(8888);
......
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