WeightAlgorithm-重みアルゴリズム-Random、HashCode比較

4268 ワード

From GitHub Page:https://hisen.me/20190322-WeightAlgorithm%20-%20%E6%9D%83%E9%87%8D%E7%AE%97%E6%B3%95%20-%20Random%E3%80%81HashCode%E5%AF%B9%E6%AF%94/#more
一、重みアルゴリズム
重みアルゴリズムは一般的にルーティングで使用されることが多く、分布式環境の下で対等なサービスが複数あり、重み付けはランダムに1つのサービスを選択して呼び出す.
他の用途があるかもしれませんが、次のコードは簡単にこの重みを実現し、本質的には配列、ランダムな下付きを使用しています.
二、コードの概要
public static void main(String[] args) {
    String[] weight = {"A", "A", "A", "A", "A", "B", "B", "B", "C", "C"};
    final int times = 500000;
    final long hashStart = System.currentTimeMillis();
    List hashRes = getList4Hash(weight, times);
    printRes("Hash", hashStart, hashRes);

    final long randomStart = System.currentTimeMillis();
    List randomRes = getList4Random(weight, times);
    printRes("Random", randomStart, randomRes);
//        Hash          use millis: 931
//        A:49.92%
//        B:29.99%
//        C:20.09%
//        Random        use millis: 50
//        A:50.08%
//        B:29.93%
//        C:19.99%
}

三、完全なコード
github:https://github.com/hisenyuan/IDEAPractice/blob/master/src/main/java/com/hisen/algorithms/WeightAlgorithm.java
package com.hisen.algorithms;

import com.google.common.collect.Lists;

import java.text.NumberFormat;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.UUID;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @Author hisenyuan
 * @Description $end$
 * @Date 2019/3/21 21:04
 */
public class WeightAlgorithm {
    public static void main(String[] args) {
        String[] weight = {"A", "A", "A", "A", "A", "B", "B", "B", "C", "C"};
        final int times = 500000;
        final long hashStart = System.currentTimeMillis();
        List hashRes = getList4Hash(weight, times);
        printRes("Hash", hashStart, hashRes);

        final long randomStart = System.currentTimeMillis();
        List randomRes = getList4Random(weight, times);
        printRes("Random", randomStart, randomRes);
//        Hash          use millis: 931
//        A:49.92%
//        B:29.99%
//        C:20.09%
//        Random        use millis: 50
//        A:50.08%
//        B:29.93%
//        C:19.99%
    }

    private static void printRes(String method, long start, List resList) {
        final Map collect = resList.stream().collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
        final double a = collect.get("A");
        final double b = collect.get("B");
        final double c = collect.get("C");
        final double sum = a + b + c;

        NumberFormat nt = NumberFormat.getPercentInstance();
        nt.setMinimumFractionDigits(2);

        System.out.println(method + "\t use millis: " + (System.currentTimeMillis() - start));
        System.out.println("A" + ":" + nt.format(a / sum));
        System.out.println("B" + ":" + nt.format(b / sum));
        System.out.println("C" + ":" + nt.format(c / sum));
    }

    private static List getList4Hash(String[] weight, int times) {
        List result = Lists.newArrayList();
        for (int i = 0; i < times; i++) {
            final String a = UUID.randomUUID().toString() + System.currentTimeMillis();
            final int hash = a.hashCode();
            final int index = hash > 0 ? hash : -hash;
            final String res = weight[index % weight.length];
            result.add(res);
        }
        return result;
    }

    private static List getList4Random(String[] weight, int times) {
        List result = Lists.newArrayList();
        Random random = new Random();
        for (int i = 0; i < times; i++) {
            final int index = random.nextInt(weight.length);
            final String res = weight[index];
            result.add(res);
        }
        return result;
    }
}