1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
| public class LoadBalanceTest extends TimCommonTest {
@Test public void testNodeAddAndRemove() { List<Server> servers = new ArrayList<>(); for (int i = 0; i < 100; i++) { servers.add(new Server(getRandomIp(), 8080)); } List<Server> serverAdd = new ArrayList<>(120); servers.forEach(x -> serverAdd.add(x)); for (int i = 0; i < 20; i++) { serverAdd.add(new Server(getRandomIp(), 8080)); } LoadBalancer chloadBalance = new ConsistentHashLoadBalancer(); List<String> invocations = new ArrayList<>(); for (int i = 0; i < 10000; i++) { invocations.add(UUID.randomUUID().toString()); } int countDel = 0; List<Server> serverDel = servers.subList(0, 80); for (String invocation : invocations) { Server origin = chloadBalance.select(servers, invocation); Server delete = chloadBalance.select(serverDel, invocation); if (origin.getUrl().equals(delete.getUrl())) { countDel++; } } int countAdd = 0; for (String invocation : invocations) { Server origin = chloadBalance.select(servers, invocation); Server add = chloadBalance.select(serverAdd, invocation); if (origin.getUrl().equals(add.getUrl())) { countAdd++; } } log.info("下线20%节点命中率:{}", countDel / 10000D); log.info("上线20%节点命中率:{}", countAdd / 10000D); } int[][] range = {{607649792, 608174079}, {1038614528, 1039007743}, {1783627776, 1784676351}, {2035023872, 2035154943}, {2078801920, 2079064063}, {-1950089216, -1948778497}, {-1425539072, -1425014785}, {-1236271104, -1235419137}, {-770113536, -768606209}, {-569376768, -564133889}, };
public String num2ip(int ip) { int[] b = new int[4]; String x = ""; b[0] = (int) ((ip >> 24) & 0xff); b[1] = (int) ((ip >> 16) & 0xff); b[2] = (int) ((ip >> 8) & 0xff); b[3] = (int) (ip & 0xff); x = b[0] + "." + b[1] + "." + b[2] + "." + b[3]; return x; }
public String getRandomIp() { Random rdint = new Random(); int index = rdint.nextInt(10); String ip = num2ip( range[index][0] + new Random().nextInt(range[index][1] - range[index][0])); return ip;
} }
|