作者 crossoverJie

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

@@ -13,8 +13,17 @@ public class SortArrayMapConsistentHash extends AbstractConsistentHash { @@ -13,8 +13,17 @@ public class SortArrayMapConsistentHash extends AbstractConsistentHash {
13 13
14 private SortArrayMap sortArrayMap = new SortArrayMap(); 14 private SortArrayMap sortArrayMap = new SortArrayMap();
15 15
  16 + /**
  17 + * 虚拟节点数量
  18 + */
  19 + private static final int VIRTUAL_NODE_SIZE = 2 ;
  20 +
16 @Override 21 @Override
17 public void add(long key, String value) { 22 public void add(long key, String value) {
  23 + for (int i = 0; i < VIRTUAL_NODE_SIZE; i++) {
  24 + Long hash = super.hash("vir" + key + i);
  25 + sortArrayMap.add(hash,value);
  26 + }
18 sortArrayMap.add(key, value); 27 sortArrayMap.add(key, value);
19 } 28 }
20 29
@@ -13,8 +13,17 @@ import java.util.TreeMap; @@ -13,8 +13,17 @@ import java.util.TreeMap;
13 public class TreeMapConsistentHash extends AbstractConsistentHash { 13 public class TreeMapConsistentHash extends AbstractConsistentHash {
14 private TreeMap<Long,String> treeMap = new TreeMap<Long, String>() ; 14 private TreeMap<Long,String> treeMap = new TreeMap<Long, String>() ;
15 15
  16 + /**
  17 + * 虚拟节点数量
  18 + */
  19 + private static final int VIRTUAL_NODE_SIZE = 2 ;
  20 +
16 @Override 21 @Override
17 public void add(long key, String value) { 22 public void add(long key, String value) {
  23 + for (int i = 0; i < VIRTUAL_NODE_SIZE; i++) {
  24 + Long hash = super.hash("vir" + key + i);
  25 + treeMap.put(hash,value);
  26 + }
18 treeMap.put(key, value); 27 treeMap.put(key, value);
19 } 28 }
20 29
@@ -6,8 +6,6 @@ import org.junit.Test; @@ -6,8 +6,6 @@ import org.junit.Test;
6 import java.util.ArrayList; 6 import java.util.ArrayList;
7 import java.util.List; 7 import java.util.List;
8 8
9 -import static org.junit.Assert.*;  
10 -  
11 public class SortArrayMapConsistentHashTest { 9 public class SortArrayMapConsistentHashTest {
12 10
13 @Test 11 @Test
@@ -15,13 +13,12 @@ public class SortArrayMapConsistentHashTest { @@ -15,13 +13,12 @@ public class SortArrayMapConsistentHashTest {
15 AbstractConsistentHash map = new SortArrayMapConsistentHash() ; 13 AbstractConsistentHash map = new SortArrayMapConsistentHash() ;
16 14
17 List<String> strings = new ArrayList<String>(); 15 List<String> strings = new ArrayList<String>();
18 - strings.add("") ;  
19 for (int i = 0; i < 10; i++) { 16 for (int i = 0; i < 10; i++) {
20 strings.add("127.0.0." + i) ; 17 strings.add("127.0.0." + i) ;
21 } 18 }
22 String process = map.process(strings,"zhangsan"); 19 String process = map.process(strings,"zhangsan");
23 System.out.println(process); 20 System.out.println(process);
24 - Assert.assertEquals("127.0.0.8",process); 21 + Assert.assertEquals("127.0.0.2",process);
25 22
26 } 23 }
27 24
@@ -36,7 +33,7 @@ public class SortArrayMapConsistentHashTest { @@ -36,7 +33,7 @@ public class SortArrayMapConsistentHashTest {
36 String process = map.process(strings,"zhangsan2"); 33 String process = map.process(strings,"zhangsan2");
37 System.out.println(process); 34 System.out.println(process);
38 35
39 - Assert.assertEquals("127.0.0.4",process); 36 + Assert.assertEquals("127.0.0.3",process);
40 } 37 }
41 38
42 @Test 39 @Test
@@ -79,7 +76,7 @@ public class SortArrayMapConsistentHashTest { @@ -79,7 +76,7 @@ public class SortArrayMapConsistentHashTest {
79 String process = map.process(strings,"1551253899106"); 76 String process = map.process(strings,"1551253899106");
80 77
81 System.out.println(process); 78 System.out.println(process);
82 - Assert.assertEquals("45.78.28.220:9100:10081",process); 79 + Assert.assertEquals("45.78.28.220:9000:8081",process);
83 } 80 }
84 81
85 @Test 82 @Test
@@ -94,7 +91,7 @@ public class SortArrayMapConsistentHashTest { @@ -94,7 +91,7 @@ public class SortArrayMapConsistentHashTest {
94 String process = map.process(strings,"1551253899106"); 91 String process = map.process(strings,"1551253899106");
95 92
96 System.out.println(process); 93 System.out.println(process);
97 - Assert.assertEquals("45.78.28.220:9100:10081",process); 94 + Assert.assertEquals("45.78.28.220:9000:8081",process);
98 } 95 }
99 @Test 96 @Test
100 public void getFirstNodeValue7() { 97 public void getFirstNodeValue7() {
@@ -109,7 +106,7 @@ public class SortArrayMapConsistentHashTest { @@ -109,7 +106,7 @@ public class SortArrayMapConsistentHashTest {
109 String process = map.process(strings,"1551253899106"); 106 String process = map.process(strings,"1551253899106");
110 107
111 System.out.println(process); 108 System.out.println(process);
112 - Assert.assertEquals("45.78.28.220:9100:10081",process); 109 + Assert.assertEquals("45.78.28.220:9000:8081",process);
113 } 110 }
114 111
115 112
@@ -18,7 +18,7 @@ public class TreeMapConsistentHashTest { @@ -18,7 +18,7 @@ public class TreeMapConsistentHashTest {
18 } 18 }
19 String process = map.process(strings,"zhangsan"); 19 String process = map.process(strings,"zhangsan");
20 System.out.println(process); 20 System.out.println(process);
21 - Assert.assertEquals("127.0.0.8",process); 21 + Assert.assertEquals("127.0.0.2",process);
22 } 22 }
23 23
24 24
@@ -34,7 +34,7 @@ public class TreeMapConsistentHashTest { @@ -34,7 +34,7 @@ public class TreeMapConsistentHashTest {
34 String process = map.process(strings,"zhangsan2"); 34 String process = map.process(strings,"zhangsan2");
35 System.out.println(process); 35 System.out.println(process);
36 36
37 - Assert.assertEquals("127.0.0.4",process); 37 + Assert.assertEquals("127.0.0.3",process);
38 } 38 }
39 39
40 40