package com.github.zangxiaoqiang.dfc;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import com.github.zangxiaoqiang.dfc.locator.KetamaNodeLocator1;
public class HashAlgorithmTest {
static Random ran = new Random();
/** key's count */
private static final Integer EXE_TIMES = 100000;
private static final Integer NODE_COUNT = 5;
private static final Integer VIRTUAL_NODE_COUNT = 160;
public static void main(String[] args) {
HashAlgorithmTest test = new HashAlgorithmTest();
/** Records the times of locating node*/
Map<Node, Integer> nodeRecord = new HashMap<Node, Integer>();
List<Node> allNodes = test.getNodes(NODE_COUNT);
KetamaNodeLocator1 locator = new KetamaNodeLocator1(HashAlgorithm.KETAMA_HASH, VIRTUAL_NODE_COUNT);
List<String> allKeys = test.getAllStrings();
for (String key : allKeys) {
Node node = locator.getNode(key);
Integer times = nodeRecord.get(node);
if (times == null) {
nodeRecord.put(node, 1);
} else {
nodeRecord.put(node, times + 1);
}
}
System.out.println("Nodes count : " + NODE_COUNT + ", Keys count : " + EXE_TIMES + ", Normal percent : " + (float) 100 / NODE_COUNT + "%");
System.out.println("-------------------- boundary ----------------------");
for (Map.Entry<Node, Integer> entry : nodeRecord.entrySet()) {
System.out.println("Node name :" + entry.getKey() + " - Times : " + entry.getValue() + " - Percent : " + (float)entry.getValue() / EXE_TIMES * 100 + "%");
}
}
/**
* Gets the mock node by the material parameter
*
* @param nodeCount
* the count of node wanted
* @return
* the node list
*/
private List<Node> getNodes(int nodeCount) {
List<Node> nodes = new ArrayList<Node>();
for (int k = 1; k <= nodeCount; k++) {
Node node = new Node("node" + k);
nodes.add(node);
}
return nodes;
}
/**
* All the keys
*/
private List<String> getAllStrings() {
List<String> allStrings = new ArrayList<String>(EXE_TIMES);
for (int i = 0; i < EXE_TIMES; i++) {
allStrings.add(generateRandomString(ran.nextInt(50)));
}
return allStrings;
}
/**
* To generate the random string by the random algorithm
* <br>
* The char between 32 and 127 is normal char
*
* @param length
* @return
*/
private String generateRandomString(int length) {
StringBuffer sb = new StringBuffer(length);
for (int i = 0; i < length; i++) {
sb.append((char) (ran.nextInt(95) + 32));
}
return sb.toString();
}
}