github.com/chain5j/chain5j-pkg@v1.0.7/collection/maps/linked_hashmap/linked_hashmap_test.go (about) 1 // Package linkedHashMap 2 // 3 // @author: xwc1125 4 package linkedHashMap 5 6 import ( 7 "crypto/rand" 8 "fmt" 9 "log" 10 "math/big" 11 "strconv" 12 "sync" 13 "testing" 14 15 "github.com/chain5j/chain5j-pkg/util/dateutil" 16 ) 17 18 func TestLinkedHashMap(t *testing.T) { 19 var wg sync.WaitGroup 20 linkedHashMap := NewLinkedHashMap() 21 startTime := dateutil.CurrentTime() 22 for i := 0; i < 100; i++ { 23 wg.Add(1) 24 go add(linkedHashMap, i, 1000, &wg) 25 } 26 wg.Wait() 27 endTime := dateutil.CurrentTime() 28 log.Println(fmt.Sprintf("【添加】end-start=%d,len=%d", endTime-startTime, linkedHashMap.Len())) 29 30 ids := make(map[string]string, 0) 31 for i := 0; i < 100; i++ { 32 ids2 := gene(i, 100) 33 for _, id := range ids2 { 34 ids[id] = id 35 } 36 } 37 log.Println(fmt.Sprintf("【ids】len=%d", len(ids))) 38 } 39 40 func TestLinkedHashMapBatch(t *testing.T) { 41 var wg sync.WaitGroup 42 linkedHashMap := NewLinkedHashMap() 43 startTime := dateutil.CurrentTime() 44 for i := 0; i < 1000; i++ { 45 wg.Add(1) 46 go addBatch(linkedHashMap, i, 1000, &wg) 47 } 48 wg.Wait() 49 endTime := dateutil.CurrentTime() 50 log.Println(fmt.Sprintf("【添加】end-start=%d,len=%d", endTime-startTime, linkedHashMap.Len())) 51 52 var count = 0 53 list := linkedHashMap.GetLinkList() 54 if list != nil && list.Len() > 0 { 55 head := list.Front() 56 for head != nil && count < 10 { 57 fmt.Println(head.Value) 58 count++ 59 head = head.Next() 60 } 61 } 62 63 ids := make(map[string]string, 0) 64 for i := 0; i < 1000; i++ { 65 ids2 := gene(i, 1000) 66 for _, id := range ids2 { 67 ids[id] = id 68 } 69 } 70 log.Println(fmt.Sprintf("【ids】len=%d", len(ids))) 71 } 72 73 func add(linkedHashMap *LinkedHashMap, index int, count int, wg *sync.WaitGroup) { 74 defer wg.Done() 75 for i := 0; i < count; i++ { 76 linkedHashMap.Add(strconv.Itoa(index)+"_"+strconv.Itoa(i), strconv.Itoa(index)+"_"+strconv.Itoa(i)) 77 } 78 } 79 80 func addBatch(linkedHashMap *LinkedHashMap, index int, count int, wg *sync.WaitGroup) { 81 defer wg.Done() 82 kvs := make([]KV, count) 83 for i := 0; i < count; i++ { 84 kvs[i] = KV{ 85 Key: strconv.Itoa(index) + "_" + strconv.Itoa(i), 86 Val: strconv.Itoa(index) + "_" + strconv.Itoa(i), 87 } 88 } 89 linkedHashMap.BatchAdd(kvs...) 90 } 91 92 func gene(index int, count int) []string { 93 ids := make([]string, count) 94 for j := 0; j < count; j++ { 95 n, _ := rand.Int(rand.Reader, big.NewInt(int64(count))) 96 ids[j] = strconv.Itoa(index) + "_" + strconv.Itoa(int(n.Int64())) 97 } 98 return ids 99 }