chickenlj closed pull request #1267: [dubbo-1267]add consistentHashLoadBalance
test
URL: https://github.com/apache/incubator-dubbo/pull/1267
This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:
As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):
diff --git
a/dubbo-cluster/src/main/java/com/alibaba/dubbo/rpc/cluster/loadbalance/ConsistentHashLoadBalance.java
b/dubbo-cluster/src/main/java/com/alibaba/dubbo/rpc/cluster/loadbalance/ConsistentHashLoadBalance.java
index a080e62139..e99efeb945 100644
---
a/dubbo-cluster/src/main/java/com/alibaba/dubbo/rpc/cluster/loadbalance/ConsistentHashLoadBalance.java
+++
b/dubbo-cluster/src/main/java/com/alibaba/dubbo/rpc/cluster/loadbalance/ConsistentHashLoadBalance.java
@@ -36,6 +36,8 @@
*/
public class ConsistentHashLoadBalance extends AbstractLoadBalance {
+ public static final String NAME = "consistenthash";
+
private final ConcurrentMap<String, ConsistentHashSelector<?>> selectors =
new ConcurrentHashMap<String, ConsistentHashSelector<?>>();
@SuppressWarnings("unchecked")
diff --git
a/dubbo-cluster/src/test/java/com/alibaba/dubbo/rpc/cluster/loadbalance/LoadBalanceTest.java
b/dubbo-cluster/src/test/java/com/alibaba/dubbo/rpc/cluster/loadbalance/LoadBalanceTest.java
index 902d145eff..4c48566ec0 100644
---
a/dubbo-cluster/src/test/java/com/alibaba/dubbo/rpc/cluster/loadbalance/LoadBalanceTest.java
+++
b/dubbo-cluster/src/test/java/com/alibaba/dubbo/rpc/cluster/loadbalance/LoadBalanceTest.java
@@ -33,6 +33,7 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
+import java.util.Random;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicLong;
@@ -49,6 +50,7 @@
Invoker<LoadBalanceTest> invoker3;
Invoker<LoadBalanceTest> invoker4;
Invoker<LoadBalanceTest> invoker5;
+ List<Invocation> invocationList;
/**
* @throws java.lang.Exception
@@ -71,6 +73,14 @@ public void setUp() throws Exception {
invoker3 = EasyMock.createMock(Invoker.class);
invoker4 = EasyMock.createMock(Invoker.class);
invoker5 = EasyMock.createMock(Invoker.class);
+ invocationList = new ArrayList<Invocation>();
+ for(int i=0; i<1000; i++){
+ Invocation invocation1 = EasyMock.createMock(Invocation.class);
+
EasyMock.expect(invocation1.getMethodName()).andReturn("method1").anyTimes();
+ EasyMock.expect(invocation1.getArguments()).andReturn(new
Object[]{randomString(3),randomString(5)}).anyTimes();
+ EasyMock.replay(invocation1);
+ invocationList.add(invocation1);
+ }
URL url1 = URL.valueOf("test://127.0.0.1:1/DemoService");
URL url2 = URL.valueOf("test://127.0.0.1:2/DemoService");
@@ -117,6 +127,18 @@ public void testRoundRobinLoadBalance_select() {
}
}
+ @Test
+ public void testConsistentHashLoadBalance_select() {
+ Map<Invoker, AtomicLong> counter =
getInvokeCounter(ConsistentHashLoadBalance.NAME, invocationList);
+
+ long runs = invocationList.size();
+ for (Invoker minvoker : counter.keySet()) {
+ Long count = counter.get(minvoker).get();
+ float ratio = count/(runs / (0f + invokers.size()));
+ Assert.assertTrue("invoker ratio must be gt 0.5 and lt
2",ratio>0.5 && ratio< 2);
+ }
+ }
+
@Test
public void testRandomLoadBalance_select() {
int runs = 1000;
@@ -169,6 +191,20 @@ public void testLeastActiveLoadBalance_select() {
return counter;
}
+ public Map<Invoker, AtomicLong> getInvokeCounter(String loadbalanceName,
List<Invocation> invocationList) {
+ Map<Invoker, AtomicLong> counter = new ConcurrentHashMap<Invoker,
AtomicLong>();
+ LoadBalance lb =
ExtensionLoader.getExtensionLoader(LoadBalance.class).getExtension(loadbalanceName);
+ for (Invoker invoker : invokers) {
+ counter.put(invoker, new AtomicLong(0));
+ }
+
+ for(Invocation invocation : invocationList){
+ Invoker sinvoker = lb.select(invokers, invokers.get(0).getUrl(),
invocation);
+ counter.get(sinvoker).incrementAndGet();
+ }
+ return counter;
+ }
+
@Test
public void testLoadBalanceWarmup() {
Assert.assertEquals(1,
@@ -201,4 +237,14 @@ public void testLoadBalanceWarmup() {
.calculateWarmupWeight(20 * 60 * 1000, Constants.DEFAULT_WARMUP,
Constants.DEFAULT_WEIGHT));
}
+ private static String randomString(int length) {
+ String str =
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
+ Random random = new Random();
+ StringBuffer buf = new StringBuffer();
+ for (int i = 0; i < length; i++) {
+ int num = random.nextInt(62);
+ buf.append(str.charAt(num));
+ }
+ return buf.toString();
+ }
}
\ No newline at end of file
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
With regards,
Apache Git Services
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]