Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in / Register
Toggle navigation
B
brpc
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Packages
Packages
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
submodule
brpc
Commits
74df9729
Commit
74df9729
authored
Feb 12, 2018
by
root
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
test case of wrr lb
parent
f3e8790b
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
66 additions
and
0 deletions
+66
-0
brpc_load_balancer_unittest.cpp
test/brpc_load_balancer_unittest.cpp
+66
-0
No files found.
test/brpc_load_balancer_unittest.cpp
View file @
74df9729
...
...
@@ -11,6 +11,8 @@
#include "butil/time.h"
#include "butil/containers/doubly_buffered_data.h"
#include "brpc/socket.h"
#include "butil/strings/string_number_conversions.h"
#include "brpc/policy/weighted_round_robin_load_balancer.h"
#include "brpc/policy/round_robin_load_balancer.h"
#include "brpc/policy/randomized_load_balancer.h"
#include "brpc/policy/locality_aware_load_balancer.h"
...
...
@@ -513,4 +515,68 @@ TEST_F(LoadBalancerTest, consistent_hashing) {
}
}
}
TEST_F
(
LoadBalancerTest
,
weighted_round_robin
)
{
const
char
*
servers
[]
=
{
"10.92.115.19:8831"
,
"10.42.108.25:8832"
,
"10.36.150.32:8833"
,
"10.92.149.48:8834"
,
"10.42.122.201:8835"
,
"10.42.122.202:8836"
};
std
::
string
weight
[]
=
{
"3"
,
"2"
,
"7"
,
"1ab"
,
"-1"
,
"0"
};
std
::
map
<
butil
::
EndPoint
,
int
>
configed_weight
;
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
];
butil
::
EndPoint
dummy
;
ASSERT_EQ
(
0
,
str2endpoint
(
addr
,
&
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
=
weight
[
i
];
if
(
i
<
3
)
{
int
weight_num
=
0
;
ASSERT_TRUE
(
butil
::
StringToInt
(
weight
[
i
],
&
weight_num
));
configed_weight
[
dummy
]
=
weight_num
;
EXPECT_TRUE
(
wrrlb
.
AddServer
(
id
));
}
else
{
EXPECT_FALSE
(
wrrlb
.
AddServer
(
id
));
}
}
// Select the best server according to weight configured.
// There are 3 valid servers with weight 3, 2 and 7 respectively.
// We run SelectServer for 12 times. The result number of each server seleted should be
// consistent with weight configured.
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
);
int
total_weight
=
12
;
std
::
vector
<
butil
::
EndPoint
>
select_servers
;
for
(
int
i
=
0
;
i
!=
total_weight
;
++
i
)
{
EXPECT_EQ
(
0
,
wrrlb
.
SelectServer
(
in
,
&
out
));
select_servers
.
emplace_back
(
ptr
->
remote_side
());
++
select_result
[
ptr
->
remote_side
()];
}
for
(
const
auto
&
s
:
select_servers
)
{
std
::
cout
<<
"1="
<<
s
<<
", "
;
}
std
::
cout
<<
std
::
endl
;
// Check whether slected result is consistent with expected.
EXPECT_EQ
(
3
,
select_result
.
size
());
for
(
const
auto
&
result
:
select_result
)
{
std
::
cout
<<
result
.
first
<<
" result="
<<
result
.
second
<<
" configured="
<<
configed_weight
[
result
.
first
]
<<
std
::
endl
;
EXPECT_EQ
(
result
.
second
,
configed_weight
[
result
.
first
]);
}
}
}
//namespace
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment