github.com/cheng762/platon-go@v1.8.17-0.20190529111256-7deff2d7be26/core/ticketcache/ticketids_cache_test.go (about) 1 package ticketcache 2 3 import ( 4 "github.com/PlatONnetwork/PlatON-Go/common" 5 "github.com/PlatONnetwork/PlatON-Go/common/byteutil" 6 "github.com/PlatONnetwork/PlatON-Go/crypto" 7 "github.com/PlatONnetwork/PlatON-Go/ethdb" 8 "github.com/PlatONnetwork/PlatON-Go/p2p/discover" 9 "fmt" 10 "math/big" 11 "testing" 12 ) 13 14 const ( 15 blockCount = 20 16 nodeCount = 200 17 ticketCount = 51200 18 ) 19 20 func getBlockMaxData() (TicketCache, error) { 21 //every nodeid has 256 ticket total has 200 nodeid 22 ret := NewTicketCache() 23 for n:=0; n<nodeCount; n++ { 24 nodeid := make([]byte, 0, 64) 25 nodeid = append(nodeid, crypto.Keccak256Hash([]byte("nodeid"), byteutil.IntToBytes(n)).Bytes()...) 26 nodeid = append(nodeid, crypto.Keccak256Hash([]byte("nodeid"), byteutil.IntToBytes(n*10)).Bytes()...) 27 NodeId, err := discover.BytesID(nodeid) 28 if err!=nil { 29 return ret, err 30 } 31 tids := make([]common.Hash, 0) 32 for i:=0; i<ticketCount/nodeCount ; i++ { 33 tids = append(tids, crypto.Keccak256Hash([]byte("tid"), byteutil.IntToBytes(i))) 34 } 35 ret[NodeId] = tids 36 } 37 return ret, nil 38 } 39 40 func Test_Timer (t *testing.T) { 41 timer := Timer{} 42 for i:=0; i<1000; i++ { 43 timer.Begin() 44 //time.Sleep() 45 fmt.Println("i: ", i, " t: ", timer.End()) 46 } 47 } 48 49 func Test_GenerateData (t *testing.T) { 50 for i:=0; i<blockCount; i++ { 51 _,err := getBlockMaxData() 52 if err!=nil { 53 fmt.Println("getMaxtickets faile err: ", err.Error()) 54 t.Errorf("getMaxtickets faile") 55 } 56 } 57 } 58 59 func Test_New(t *testing.T) { 60 ldb, err := ethdb.NewLDBDatabase("./data/platon/chaindata", 0, 0) 61 if err!=nil { 62 t.Errorf("NewLDBDatabase faile") 63 } 64 timer := Timer{} 65 timer.Begin() 66 NewTicketIdsCache(ldb) 67 fmt.Printf("NewTicketIdsCache time [ms=%.3f]\n", timer.End()) 68 ldb.Close() 69 } 70 71 func Test_Submit2Cache(t *testing.T) { 72 ldb, err := ethdb.NewLDBDatabase("./data/platon/chaindata", 0, 0) 73 if err!=nil { 74 t.Errorf("NewLDBDatabase faile") 75 } 76 tc := NewTicketIdsCache(ldb) 77 for i:=0; i<blockCount; i++ { 78 number := big.NewInt(int64(i)) 79 bkhash := crypto.Keccak256Hash(byteutil.IntToBytes(i)) 80 mapCache ,err := getBlockMaxData() 81 if err!=nil { 82 fmt.Println("getMaxtickets faile err: ", err.Error()) 83 t.Errorf("getMaxtickets faile") 84 } 85 timer := Timer{} 86 timer.Begin() 87 fmt.Println("msg==> ", "begin: ", timer.End()) 88 tc.Submit2Cache(number, big.NewInt(int64(blockCount)), bkhash, mapCache) 89 fmt.Printf("run submit time [index=%d][ms=%.3f]\n", i, timer.End()) 90 } 91 ldb.Close() 92 } 93 94 func Test_Write(t *testing.T) { 95 ldb, err := ethdb.NewLDBDatabase("./data/platon/chaindata", 0, 0) 96 if err!=nil { 97 t.Errorf("NewLDBDatabase faile") 98 } 99 timer := Timer{} 100 tc := NewTicketIdsCache(ldb) 101 for i:=0; i<blockCount; i++ { 102 number := big.NewInt(int64(i)) 103 bkhash := crypto.Keccak256Hash(byteutil.IntToBytes(i)) 104 mapCache ,err := getBlockMaxData() 105 if err!=nil { 106 fmt.Println("getMaxtickets faile err: ", err.Error()) 107 t.Errorf("getMaxtickets faile") 108 } 109 110 ////Submit2Cache 111 timer.Begin() 112 tc.Submit2Cache(number, big.NewInt(int64(blockCount)), bkhash, mapCache) 113 fmt.Printf("run submit time [index=%d][ms=%.3f]\n", i, timer.End()) 114 115 //Hash 116 timer.Begin() 117 chash, err:= Hash(mapCache) 118 fmt.Printf("run hash time [index=%d][ms=%.3f][hash=%s]\n", i, timer.End(), chash.Hex()) 119 } 120 121 //Commit 122 timer.Begin() 123 tc.Commit(ldb, big.NewInt(0), common.Hash{}) 124 fmt.Printf("run Commit time [ms=%.3f]\n", timer.End()) 125 ldb.Close() 126 } 127 128 func Test_Read(t *testing.T) { 129 ldb, err := ethdb.NewLDBDatabase("./data/platon/chaindata", 0, 0) 130 if err!=nil { 131 t.Errorf("NewLDBDatabase faile") 132 } 133 timer := Timer{} 134 tcCopy := NewTicketIdsCache(ldb) 135 for i:=0; i<blockCount; i++ { 136 number := big.NewInt(int64(i)) 137 bkhash := crypto.Keccak256Hash(byteutil.IntToBytes(i)) 138 139 //==>run GetNodeTicketsMap time 140 timer.Begin() 141 tcCopy.GetNodeTicketsMap(number, bkhash) 142 fmt.Printf("run getNodeTicketsMap time [index=%d] [ms=%.3f]\n", i, timer.End()) 143 } 144 ldb.Close() 145 } 146