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