63 Star 183 Fork 3

Gitee 极速下载/hyperledger-fabric

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
此仓库是为了提升国内下载速度的镜像仓库,每日同步一次。 原始仓库: https://github.com/hyperledger/fabric
克隆/下载
util.go 2.96 KB
一键复制 编辑 原始数据 按行查看 历史
manish 提交于 2020-01-14 21:51 . [FAB-16895] Move ledger benchmark tests
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package experiments
import (
"bytes"
"encoding/json"
"fmt"
"math/rand"
"strconv"
"github.com/hyperledger/fabric/common/flogging"
)
var logger = flogging.MustGetLogger("experiments")
type marbleRecord struct {
ID string `json:"_id,omitempty"`
Rev string `json:"_rev,omitempty"`
Prefix string `json:"prefix,omitempty"`
AssetType string `json:"asset_type,omitempty"`
AssetName string `json:"asset_name,omitempty"`
Color string `json:"color,omitempty"`
Size int `json:"size,omitempty"`
Owner string `json:"owner,omitempty"`
DataPadding string `json:"datapadding,omitempty"`
}
var colors = []string{
"red",
"green",
"purple",
"yellow",
"white",
"black",
}
var owners = []string{
"fred",
"jerry",
"tom",
"alice",
"kim",
"angela",
"john",
}
//TestValue is a struct for holding the test value
type TestValue struct {
Value string
}
func constructKey(keyNumber int) string {
return fmt.Sprintf("key_%09d", keyNumber)
}
func constructValue(keyNumber int, kvSize int) []byte {
prefix := constructValuePrefix(keyNumber)
randomBytes := constructRandomBytes(kvSize - len(prefix))
return append(prefix, randomBytes...)
}
func constructJSONValue(keyNumber int, kvSize int) []byte {
prefix := constructValuePrefix(keyNumber)
rand.Seed(int64(keyNumber))
color := colors[rand.Intn(len(colors))]
size := rand.Intn(len(colors))*10 + 10
owner := owners[rand.Intn(len(owners))]
assetName := "marble" + strconv.Itoa(keyNumber)
testRecord := marbleRecord{Prefix: string(prefix), AssetType: "marble", AssetName: assetName, Color: color, Size: size, Owner: owner}
jsonValue, _ := json.Marshal(testRecord)
if kvSize > len(jsonValue) {
randomJSONBytes := constructRandomBytes(kvSize - len(jsonValue))
//add in extra bytes
testRecord.DataPadding = string(randomJSONBytes)
jsonValue, _ = json.Marshal(testRecord)
}
return jsonValue
}
func constructValuePrefix(keyNumber int) []byte {
return []byte(fmt.Sprintf("%s%09d", "value_", keyNumber))
}
func verifyValue(keyNumber int, value []byte) bool {
prefix := constructValuePrefix(keyNumber)
if len(value) < len(prefix) {
return false
}
return bytes.Equal(value[:len(prefix)], prefix)
}
func verifyJSONValue(keyNumber int, value []byte) bool {
prefix := constructValuePrefix(keyNumber)
if len(value) < len(prefix) {
return false
}
var marble marbleRecord
json.Unmarshal(value, &marble)
if len(value) < len(prefix) {
return false
}
valuePrefix := []byte(marble.Prefix)
return bytes.Equal(valuePrefix, prefix)
}
func calculateShare(total int, numParts int, partNum int) int {
share := total / numParts
remainder := total % numParts
if partNum < remainder {
share++
}
return share
}
func constructRandomBytes(length int) []byte {
b := make([]byte, length)
rand.Read(b)
return b
}
func panicOnError(err error) {
if err != nil {
panic(fmt.Errorf("Error:%s", err))
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/mirrors/hyperledger-fabric.git
git@gitee.com:mirrors/hyperledger-fabric.git
mirrors
hyperledger-fabric
hyperledger-fabric
v2.1.0

搜索帮助

0d507c66 1850385 C8b1a773 1850385