测试dubbo的RandomLoadBalance

import java.util.Random;
import java.util.TreeMap;

public class TestDubboRandomLoadBalance {
    static int      turn_max    = 1000;
    static int      loop_max    = 100;
    static double[] weight      = new double[] { 0.06, 2.94, 97 };
    static int[]    weightArray = new int[] { 1, 1, 3 };

    public static void main(String[] args) {
        sortTree();
        sortInt();
    }

    /** TreeMap 的 ceilingKey 原理: key值为每个权重值的落点范围**/
    private static void sortTree() {
        for (int loop = 0; loop < loop_max; loop++) {

            int count1 = 0, count2 = 0, count3 = 0;
            TreeMap<Double, Integer> map = new TreeMap<Double, Integer>();
            double weightTotal = 0;
            for (int index = 0; index < weight.length; index++) {
                weightTotal += weight[index];
                map.put(weightTotal, index + 1);
            }
            int turn = turn_max;

            while (turn > 0) {
                double random = (Math.random() * weightTotal);
                Integer index = map.get(map.ceilingKey(random)); //returns the entry for the least key greater than the specified key
                if (1 == index) {
                    count1++;
                } else if (2 == index) {
                    count2++;
                } else if (3 == index) {
                    count3++;
                }
                --turn;
            }

            System.out.println("1-" + count1 + ";2-" + count2 + ";3-" + count3);
        }

    }

    /**
     * 1)假设有四个集群节点A,B,C,D,对应的权重分别是1,2,3,4,那么请求到A节点的概率就为1/(1+2+3+4) = 10%. B,C,D节点依次类推为20%,30%,40%. 
     * 2)总权重为10(1+2+3+4), 根据10随机出一个整数,假如为随机出来的是2.然后依次和权重相减,比如2(随机数)-1(A的权重) = 1,
     *  然后1(上一步计算的结果)-2(B的权重) = -1,此时-1 < 0,那么则调用B,其他的以此类推
     * 
     */
    private static void sortInt() {

        for (int loop = 0; loop < loop_max; loop++) {

            int totalWeight = 0;
            boolean sameWeight = true;
            int length = weightArray.length;
            for (int i = 0; i < length; i++) {
                int weight = weightArray[i];
                totalWeight += weight;
                if (sameWeight && i > 0 && weight != weightArray[i - 1]) {
                    sameWeight = false;
                }
            }
            Random random = new Random();

            int count1 = 0, count2 = 0, count3 = 0;
            int turn = turn_max;

            while (turn > 0) {
                int index = -1;
                if (totalWeight > 0 && !sameWeight) {
                    int offset = random.nextInt(totalWeight);
                    for (int i = 0; i < length; i++) {
                        offset -= weightArray[i];
                        if (offset < 0) {
                            index = i;
                            break;
                        }
                    }
                }
                if (index == -1) {
                    index = random.nextInt(length);
                }
                if (0 == index) {
                    count1++;
                } else if (1 == index) {
                    count2++;
                } else if (2 == index) {
                    count3++;
                }
                --turn;
            }
            System.out.println("1-" + count1 + ";2-" + count2 + ";3-" + count3);
        }

    }
}

tip:

  1. Math.random()  线程安全,多线程环境能被调用; 方法生成[0, 1)范围内的double类型伪随机数;
    Math.class
    private static final class RandomNumberGeneratorHolder {
            static final Random randomNumberGenerator = new Random();
        }
    
    public static double random() {
        return RandomNumberGeneratorHolder.randomNumberGenerator.nextDouble();
    }

     

  2. Random类中的nextInt(n)系列方法生成[0, n)的伪随机数;
    nextInt()方法会产生所有(32位)有效的整数,所以会有负数。
  3. 伪随机既有规则的随机。具体表现为:相同种子数的Random对象生成的随机数序列相同
    import java.util.ArrayList;
    import java.util.List;
    import java.util.Random;
    import java.util.stream.Collectors;
    
    public class Test {
        public static void main(String[] args) {
            Random r1 = new Random(100);
            Random r2 = new Random(100);
            List<String> l1 = new ArrayList<>();
            List<String> l2 = new ArrayList<>();
    
            for (int i = 0; i < 100; i++) {
                l1.add(String.valueOf(r1.nextInt(10)));
                l2.add(String.valueOf(r2.nextInt(10)));
            }
    
            System.out.println(l1.stream().collect(Collectors.joining(",")));
            System.out.println(String.join(",", l2));
        }
    }
    
    
    5,0,4,8,1,6,6,8,3,3,2,7,6,7,2,9,0,8,3,9,3,8,5,2,7,1,8...
    5,0,4,8,1,6,6,8,3,3,2,7,6,7,2,9,0,8,3,9,3,8,5,2,7,1,8...
    

     未定义种子的构造方法里,使用当前系统时间相关的一个数字作为种子数,该种子数只作为随机算法的起源数字,与生成的随机数区间无关系

    public Random() {
      this(seedUniquifier() ^ System.nanoTime());
    }
    
    public Random(long seed) {
        if (getClass() == Random.class)
            this.seed = new AtomicLong(initialScramble(seed));
        else {
            // subclass might have overriden setSeed
            this.seed = new AtomicLong();
            setSeed(seed);
        }
    }

     

转载于:https://my.oschina.net/u/3434392/blog/1789312

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值