作者 crossoverJie

:white_check_mark: Adding tests.性能测试 treeMap 确实要由于排序Map

... ... @@ -13,8 +13,17 @@ public class SortArrayMapConsistentHash extends AbstractConsistentHash {
private SortArrayMap sortArrayMap = new SortArrayMap();
/**
* 虚拟节点数量
*/
private static final int VIRTUAL_NODE_SIZE = 2 ;
@Override
public void add(long key, String value) {
for (int i = 0; i < VIRTUAL_NODE_SIZE; i++) {
Long hash = super.hash("vir" + key + i);
sortArrayMap.add(hash,value);
}
sortArrayMap.add(key, value);
}
... ...
... ... @@ -13,8 +13,17 @@ import java.util.TreeMap;
public class TreeMapConsistentHash extends AbstractConsistentHash {
private TreeMap<Long,String> treeMap = new TreeMap<Long, String>() ;
/**
* 虚拟节点数量
*/
private static final int VIRTUAL_NODE_SIZE = 2 ;
@Override
public void add(long key, String value) {
for (int i = 0; i < VIRTUAL_NODE_SIZE; i++) {
Long hash = super.hash("vir" + key + i);
treeMap.put(hash,value);
}
treeMap.put(key, value);
}
... ...
... ... @@ -6,8 +6,6 @@ import org.junit.Test;
import java.util.ArrayList;
import java.util.List;
import static org.junit.Assert.*;
public class SortArrayMapConsistentHashTest {
@Test
... ... @@ -15,13 +13,12 @@ public class SortArrayMapConsistentHashTest {
AbstractConsistentHash map = new SortArrayMapConsistentHash() ;
List<String> strings = new ArrayList<String>();
strings.add("") ;
for (int i = 0; i < 10; i++) {
strings.add("127.0.0." + i) ;
}
String process = map.process(strings,"zhangsan");
System.out.println(process);
Assert.assertEquals("127.0.0.8",process);
Assert.assertEquals("127.0.0.2",process);
}
... ... @@ -36,7 +33,7 @@ public class SortArrayMapConsistentHashTest {
String process = map.process(strings,"zhangsan2");
System.out.println(process);
Assert.assertEquals("127.0.0.4",process);
Assert.assertEquals("127.0.0.3",process);
}
@Test
... ... @@ -79,7 +76,7 @@ public class SortArrayMapConsistentHashTest {
String process = map.process(strings,"1551253899106");
System.out.println(process);
Assert.assertEquals("45.78.28.220:9100:10081",process);
Assert.assertEquals("45.78.28.220:9000:8081",process);
}
@Test
... ... @@ -94,7 +91,7 @@ public class SortArrayMapConsistentHashTest {
String process = map.process(strings,"1551253899106");
System.out.println(process);
Assert.assertEquals("45.78.28.220:9100:10081",process);
Assert.assertEquals("45.78.28.220:9000:8081",process);
}
@Test
public void getFirstNodeValue7() {
... ... @@ -109,7 +106,7 @@ public class SortArrayMapConsistentHashTest {
String process = map.process(strings,"1551253899106");
System.out.println(process);
Assert.assertEquals("45.78.28.220:9100:10081",process);
Assert.assertEquals("45.78.28.220:9000:8081",process);
}
... ...
... ... @@ -18,7 +18,7 @@ public class TreeMapConsistentHashTest {
}
String process = map.process(strings,"zhangsan");
System.out.println(process);
Assert.assertEquals("127.0.0.8",process);
Assert.assertEquals("127.0.0.2",process);
}
... ... @@ -34,7 +34,7 @@ public class TreeMapConsistentHashTest {
String process = map.process(strings,"zhangsan2");
System.out.println(process);
Assert.assertEquals("127.0.0.4",process);
Assert.assertEquals("127.0.0.3",process);
}
... ...