import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
public class HashAlgorithmPercentTest {
static Random ran = new Random();
/** key's count */
private static final Integer EXE_TIMES = 100000;
private static final Integer NODE_COUNT = 50;
private static final Integer VIRTUAL_NODE_COUNT = 160;
static List<String> allKeys = null;
static {
allKeys = getAllStrings();
}
public static void main(String[] args) {
Map<String, List<Node>> map = generateRecord();
List<Node> allNodes = getNodes(NODE_COUNT);
System.out.println("Normal case : nodes count : " + allNodes.size());
call(allNodes, map);
allNodes = getNodes(NODE_COUNT + 8);
System.out.println("Added case : nodes count : " + allNodes.size());
call(allNodes, map);
allNodes = getNodes(NODE_COUNT - 10);
System.out.println("Reduced case : nodes count : " + allNodes.size());
call(allNodes, map);
int addCount = 0;
int reduceCount = 0;
for (Map.Entry<String, List<Node>> entry : map.entrySet()) {
List<Node> list = entry.getValue();
if (list.size() == 3) {
if (list.get(0).equals(list.get(1))) {
addCount++;
}
if (list.get(0).equals(list.get(2))) {
reduceCount++;
}
} else {
System.out.println("It's wrong size of list, key is " + entry.getKey() + ", size is " + list.size());
}
}
System.out.println(addCount + " --- " + reduceCount);
System.out.println("Same percent in added case : " + (float) addCount * 100/ EXE_TIMES + "%");
System.out.println("Same percent in reduced case : " + (float) reduceCount * 100/ EXE_TIMES + "%");
}
private static void call(List<Node> nodes, Map<String, List<Node>> map) {
KetamaNodeLocator locator = new KetamaNodeLocator(nodes, HashAlgorithm.KETAMA_HASH, VIRTUAL_NODE_COUNT);
for (Map.Entry<String, List<Node>> entry : map.entrySet()) {
Node node = locator.getPrimary(entry.getKey());
if (node != null) {
List<Node> list = entry.getValue();
list.add(node);
}
}
}
private static Map<String, List<Node>> generateRecord() {
Map<String, List<Node>> record = new HashMap<String, List<Node>>(EXE_TIMES);
for (String key : allKeys) {
List<Node> list = record.get(key);
if (list == null) {
list = new ArrayList<Node>();
record.put(key, list);
}
}
return record;
}
/**
* Gets the mock node by the material parameter
*
* @param nodeCount
* the count of node wanted
* @return
* the node list
*/
private static 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 static 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 static 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();
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
Ketama Hashing Algorithm.rar (21个子文件)
Ketama Hashing Algorithm
src
HashAlgorithmPercentTest.java 4KB
Node.java 578B
KetamaNodeLocator.java 1KB
HashAlgorithmTest.java 3KB
HashAlgorithm.java 1KB
Ketama Hashing Algorithm.iml 437B
.idea
misc.xml 476B
compiler.xml 734B
uiDesigner.xml 9KB
workspace.xml 36KB
.name 24B
encodings.xml 171B
modules.xml 329B
scopes
scope_settings.xml 143B
copyright
profiles_settings.xml 115B
vcs.xml 173B
out
production
Ketama Hashing Algorithm
HashAlgorithmTest.class 4KB
HashAlgorithmPercentTest.class 5KB
HashAlgorithm.class 2KB
KetamaNodeLocator.class 3KB
Node.class 813B
共 21 条
- 1
资源评论
- 星宫社2014-12-09提供了很好的学习机会,多看看源代码有助于提升自己能力,谢谢分享主
liyonghui123
- 粉丝: 24
- 资源: 51
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功