github.com/hechain20/hechain@v0.0.0-20220316014945-b544036ba106/core/ledger/kvledger/benchmark/experiments/util.go (about) 1 /* 2 Copyright hechain. All Rights Reserved. 3 4 SPDX-License-Identifier: Apache-2.0 5 */ 6 7 package experiments 8 9 import ( 10 "bytes" 11 "encoding/json" 12 "fmt" 13 "math/rand" 14 "strconv" 15 ) 16 17 type marbleRecord struct { 18 ID string `json:"_id,omitempty"` 19 Rev string `json:"_rev,omitempty"` 20 Prefix string `json:"prefix,omitempty"` 21 AssetType string `json:"asset_type,omitempty"` 22 AssetName string `json:"asset_name,omitempty"` 23 Color string `json:"color,omitempty"` 24 Size int `json:"size,omitempty"` 25 Owner string `json:"owner,omitempty"` 26 DataPadding string `json:"datapadding,omitempty"` 27 } 28 29 var colors = []string{ 30 "red", 31 "green", 32 "purple", 33 "yellow", 34 "white", 35 "black", 36 } 37 38 var owners = []string{ 39 "fred", 40 "jerry", 41 "tom", 42 "alice", 43 "kim", 44 "angela", 45 "john", 46 } 47 48 // TestValue is a struct for holding the test value 49 type TestValue struct { 50 Value string 51 } 52 53 func constructKey(keyNumber int) string { 54 return fmt.Sprintf("key_%09d", keyNumber) 55 } 56 57 func constructValue(keyNumber int, kvSize int) []byte { 58 prefix := constructValuePrefix(keyNumber) 59 randomBytes := constructRandomBytes(kvSize - len(prefix)) 60 return append(prefix, randomBytes...) 61 } 62 63 func constructJSONValue(keyNumber int, kvSize int) []byte { 64 prefix := constructValuePrefix(keyNumber) 65 66 rand.Seed(int64(keyNumber)) 67 color := colors[rand.Intn(len(colors))] 68 size := rand.Intn(len(colors))*10 + 10 69 owner := owners[rand.Intn(len(owners))] 70 assetName := "marble" + strconv.Itoa(keyNumber) 71 72 testRecord := marbleRecord{Prefix: string(prefix), AssetType: "marble", AssetName: assetName, Color: color, Size: size, Owner: owner} 73 74 jsonValue, _ := json.Marshal(testRecord) 75 76 if kvSize > len(jsonValue) { 77 randomJSONBytes := constructRandomBytes(kvSize - len(jsonValue)) 78 79 // add in extra bytes 80 testRecord.DataPadding = string(randomJSONBytes) 81 82 jsonValue, _ = json.Marshal(testRecord) 83 } 84 85 return jsonValue 86 } 87 88 func constructValuePrefix(keyNumber int) []byte { 89 return []byte(fmt.Sprintf("%s%09d", "value_", keyNumber)) 90 } 91 92 func verifyValue(keyNumber int, value []byte) bool { 93 prefix := constructValuePrefix(keyNumber) 94 if len(value) < len(prefix) { 95 return false 96 } 97 return bytes.Equal(value[:len(prefix)], prefix) 98 } 99 100 func verifyJSONValue(keyNumber int, value []byte) bool { 101 prefix := constructValuePrefix(keyNumber) 102 if len(value) < len(prefix) { 103 return false 104 } 105 106 var marble marbleRecord 107 108 json.Unmarshal(value, &marble) 109 110 if len(value) < len(prefix) { 111 return false 112 } 113 114 valuePrefix := []byte(marble.Prefix) 115 return bytes.Equal(valuePrefix, prefix) 116 } 117 118 func calculateShare(total int, numParts int, partNum int) int { 119 share := total / numParts 120 remainder := total % numParts 121 if partNum < remainder { 122 share++ 123 } 124 return share 125 } 126 127 func constructRandomBytes(length int) []byte { 128 b := make([]byte, length) 129 rand.Read(b) 130 return b 131 } 132 133 func panicOnError(err error) { 134 if err != nil { 135 panic(fmt.Errorf("Error:%s", err)) 136 } 137 }