RandomLoadBalancer extends AbstractLoadBalancer { @Override public ProviderInfo doSelect(SofaRequest invocation, List<ProviderInfo> providerInfos) { ProviderInfo providerInfo = null; int size = providerInfos.size(); int totalWeight = 0; boolean isWeightSame = true; for (int i = 0; i < size; i++) { int weight = getWeight(providerInfos.get(i)); totalWeight += weight; if (isWeightSame && i > 0 && weight != getWeight(providerInfos.get(i - 1))) { isWeightSame = false; } } if (totalWeight > 0 && !isWeightSame) { int offset = random.nextInt(totalWeight); for (int i = 0; i < size; i++) { offset -= getWeight(providerInfos.get(i)); if (offset < 0) { providerInfo = providerInfos.get(i); break; } } } else { providerInfo = providerInfos.get(random.nextInt(size)); } return providerInfo; } RandomLoadBalancer(ConsumerBootstrap consumerBootstrap); @Override ProviderInfo doSelect(SofaRequest invocation, List<ProviderInfo> providerInfos);  }
@Test public void doSelect() throws Exception { RandomLoadBalancer loadBalancer = new RandomLoadBalancer(null); Map<Integer, Integer> cnt = new HashMap<Integer, Integer>(); int size = 20; int total = 100000; SofaRequest request = new SofaRequest(); { for (int i = 0; i < size; i++) { cnt.put(9000 + i, 0); } List<ProviderInfo> providers = buildSameWeightProviderList(size); long start = System.currentTimeMillis(); for (int i = 0; i < total; i++) { ProviderInfo provider = loadBalancer.doSelect(request, providers); int port = provider.getPort(); cnt.put(port, cnt.get(port) + 1); } long end = System.currentTimeMillis(); LOGGER.info("elapsed" + (end - start) + "ms"); LOGGER.info("avg " + (end - start) * 1000 * 1000 / total + "ns"); int avg = total / size; for (int i = 0; i < size; i++) { Assert.assertTrue(avg * 0.9 < cnt.get(9000 + i) && avg * 1.1 > cnt.get(9000 + i)); } } { for (int i = 0; i < size; i++) { cnt.put(9000 + i, 0); } List<ProviderInfo> providers = buildDiffWeightProviderList(size); long start = System.currentTimeMillis(); for (int i = 0; i < total; i++) { ProviderInfo provider = loadBalancer.doSelect(request, providers); int port = provider.getPort(); cnt.put(port, cnt.get(port) + 1); } long end = System.currentTimeMillis(); LOGGER.info("elapsed" + (end - start) + "ms"); LOGGER.info("avg " + (end - start) * 1000 * 1000 / total + "ns"); Assert.assertTrue(cnt.get(9000) == 0); int count = 0; for (int i = 0; i < size; i++) { count += i; } int per = total / count; for (int i = 1; i < size; i++) { Assert.assertTrue(per * i * 0.85 < cnt.get(9000 + i) && per * i * 1.15 > cnt.get(9000 + i)); } } }