Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

贡献一段任意参数支持的代码思路;替换原本的pkg/infra/trafficGenerator/proposal.go:CreateProposal方法 #200

Closed
neusoft-worker opened this issue Aug 16, 2021 · 5 comments
Labels

Comments

@neusoft-worker
Copy link

  • 如下代码目前可以支持大部分的任意参数场景,尤其是json对象参数
package trafficGenerator

import (
	"fmt"
	"github.com/hyperledger/fabric-protos-go/common"
	"github.com/hyperledger/fabric-protos-go/peer"
	"regexp"
	"strconv"
	"strings"
	"tape/internal/fabric/protoutil"
	"tape/pkg/ext/log"
	"tape/pkg/infra"
)

var (
	uuidReg         *regexp.Regexp = regexp.MustCompile(`uuid\(\)`)
	randomIntReg    *regexp.Regexp = regexp.MustCompile(`randomInt\((\d+),\s?(\d+)\)`)
	randomStringReg *regexp.Regexp = regexp.MustCompile(`randomString\((\d+)\)`)
	fmtReg          *regexp.Regexp = regexp.MustCompile(`fmt.Sprintf\((.*)\)`)
)

func CreateProposalExt(signer infra.Crypto, channel, ccname, version string, argsFuncs []func() string) (*peer.Proposal, error) {

	var argsInByte [][]byte
	for _, argF := range argsFuncs {
		argsInByte = append(argsInByte, []byte(argF()))
	}

	spec := &peer.ChaincodeSpec{
		Type:        peer.ChaincodeSpec_GOLANG,
		ChaincodeId: &peer.ChaincodeID{Name: ccname, Version: version},
		Input:       &peer.ChaincodeInput{Args: argsInByte},
	}

	invocation := &peer.ChaincodeInvocationSpec{ChaincodeSpec: spec}

	creator, err := signer.Serialize()
	if err != nil {
		return nil, err
	}

	prop, _, err := protoutil.CreateChaincodeProposal(common.HeaderType_ENDORSER_TRANSACTION, channel, invocation, creator)
	if err != nil {
		return nil, err
	}

	return prop, nil
}

func findFuncs(args []string) []func() string {
	var argFuncs []func() string
	for _, arg := range args {
		log.Log().Debugln(arg)
		if fmtReg.MatchString(arg) {
			fs := fmtReg.FindSubmatch([]byte(arg))
			fsa := strings.Split(string(fs[1]), ", ")
			format := fsa[0]
			formatFuncs := findFuncs(fsa[1:])
			argFuncs = append(argFuncs, func() string {
				var a []interface{}
				for _, f := range formatFuncs {
					a = append(a, f())
				}
				return fmt.Sprintf(format, a...)
			})
		} else {
			argFuncs = append(argFuncs, findFunc(arg))
		}
	}
	return argFuncs
}

func findFunc(s string) func() string {
	if uuidReg.MatchString(s) {
		return func() string {
			return newUUID()
		}
	} else if randomIntReg.MatchString(s) {
		f := randomIntReg.FindSubmatch([]byte(s))
		fmt.Printf("%q", f)
		d1, err := strconv.Atoi(string(f[1]))
		if err != nil {
			panic(err)
		}
		d2, err := strconv.Atoi(string(f[2]))
		if err != nil {
			panic(err)
		}
		return func() string {
			return strconv.Itoa(randomInt(d1, d2))
		}
	} else if randomStringReg.MatchString(s) {
		f := randomStringReg.FindSubmatch([]byte(s))
		fmt.Printf("%q", f)
		d, err := strconv.Atoi(string(f[1]))
		if err != nil {
			panic(err)
		}
		return func() string {
			return randomString(d)
		}
	} else {
		//不做任何变换
		return func() string {
			return s
		}
	}
}
  • 调用部分如下:
func (initiator *Initiator) Start() {

	initiator.statRaw = stat.StartNewStatx(1, "Raw Create")
	limit := rate.Inf
	ctx := context.Background()
	if initiator.R > 0 {
		limit = rate.Limit(initiator.R)
	}
	limiter := rate.NewLimiter(limit, initiator.Burst)
	argFuncs := findFuncs(initiator.Config.Args) //初始化一次

	for i := 0; i < initiator.Num; i++ {
		prop, err := CreateProposalExt(
			initiator.Crypto,
			initiator.Config.Channel,
			initiator.Config.Chaincode,
			initiator.Config.Version,
			argFuncs, //传入动态函数
		)
//...
  • 配置文件写法, 如下
args:
  - put
  - fmt.Sprintf(key:%s, randomInt(1,50))
  - fmt.Sprintf({"k1":"%s","key2":"%s","keys":"%s"}, uuid(), randomInt(10000,20000), randomString(10))
  - uuid()
@SamYuan1990
Copy link
Member

建议开个pr上来。
正则是很好的改进方向。
不过
既然我们选择了正则表达式,就不需要支持fmt在配置里了吧?

- (key:randomInt1_50)
- {"k1":"uuid","key2":"randomInt10000_20000","keys":" randomString10"}

但是这样的话就没办法区分uuid,如果原本字符就是uuid的话。

@neusoft-worker
Copy link
Author

neusoft-worker commented Aug 17, 2021 via email

@SamYuan1990
Copy link
Member

我觉得如果要考虑json格式的话可能这样会比较方便,相比较于fmt,不知道@guoger什么看法。

- (key:randomInt1_50)
- {"k1":"uuid","key2":"randomInt10000_20000","keys":" randomString10"}

SamYuan1990 added a commit to SamYuan1990/tape that referenced this issue Aug 24, 2021
SamYuan1990 added a commit that referenced this issue Aug 24, 2021
Signed-off-by: Sam Yuan <[email protected]>
@SamYuan1990
Copy link
Member

稍微做个了实现,在pr里。请参考。

@stale
Copy link

stale bot commented Sep 23, 2021

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the stale label Sep 23, 2021
@stale stale bot closed this as completed Oct 1, 2021
davidkhala pushed a commit that referenced this issue Mar 23, 2022
Signed-off-by: Sam Yuan <[email protected]>
davidkhala pushed a commit that referenced this issue Mar 23, 2022
* 109 to alpha (#189)

* 1st impl for #109

Signed-off-by: Sam Yuan <[email protected]>

* fix up

Signed-off-by: Sam Yuan <[email protected]>

* fix up

Signed-off-by: Sam Yuan <[email protected]>

* Arch refactor alpha (#190)

* add crpto interface as repare for #127

Signed-off-by: Sam Yuan <[email protected]>

* adding Worker interface for #56 and decouple Assembler and Integrator

Signed-off-by: Sam Yuan <[email protected]>

* refactor for worker interface

Signed-off-by: Sam Yuan <[email protected]>

* package structure

Signed-off-by: Sam Yuan <[email protected]>

* fix up

Signed-off-by: Sam Yuan <[email protected]>

* package refactor

Signed-off-by: Sam Yuan <[email protected]>

* fix up

Signed-off-by: Sam Yuan <[email protected]>

* fix up

Signed-off-by: Sam Yuan <[email protected]>

* remove Envelope from elements

Signed-off-by: Sam Yuan <[email protected]>

* add memeory free

Signed-off-by: Sam Yuan <[email protected]>

* remove Proposal from Elements

Signed-off-by: Sam Yuan <[email protected]>

* fix up

Signed-off-by: Sam Yuan <[email protected]>

* move start time to ctx

Signed-off-by: Sam Yuan <[email protected]>

* fix up for time setting (#191)

* fix up

Signed-off-by: Sam Yuan <[email protected]>

* fix up

Signed-off-by: Sam Yuan <[email protected]>

* impl for #173 supports for configurable signers in parallel (#194)

Signed-off-by: Sam Yuan <[email protected]>

* adding for commit phase only dummy impl (#195)

* adding for commit phase only dummy impl

Signed-off-by: Sam Yuan <[email protected]>

* fix up

Signed-off-by: Sam Yuan <[email protected]>

* impl for endorsement only

Signed-off-by: Sam Yuan <[email protected]>

* fix up

Signed-off-by: Sam Yuan <[email protected]>

* fix up

Signed-off-by: Sam Yuan <[email protected]>

* update document and prepare for release (#196)

* update document and prepare for release

Signed-off-by: Sam Yuan <[email protected]>

* fix up

Signed-off-by: Sam Yuan <[email protected]>

* fix up

Signed-off-by: Sam Yuan <[email protected]>

* fix up

Signed-off-by: Sam Yuan <[email protected]>

* fix up

Signed-off-by: Sam Yuan <[email protected]>

* fix up

Signed-off-by: Sam Yuan <[email protected]>

* fix up

Signed-off-by: Sam Yuan <[email protected]>

* fix up

Signed-off-by: Sam Yuan <[email protected]>

* fix up in readme (#197)

Signed-off-by: Sam Yuan <[email protected]>

* impl for #200 (#207)

Signed-off-by: Sam Yuan <[email protected]>

* Impl173 (#208)

* adjust code

Signed-off-by: Sam Yuan <[email protected]>

* code refactor

Signed-off-by: Sam Yuan <[email protected]>

* code refactor

Signed-off-by: Sam Yuan <[email protected]>

* use const instead

Signed-off-by: Sam Yuan <[email protected]>

* adding parallel flag

Signed-off-by: Sam Yuan <[email protected]>

* try to check escapes in ci (#209)

* try to check escapes in ci

Signed-off-by: Sam Yuan <[email protected]>

* fix up

Signed-off-by: Sam Yuan <[email protected]>

* fix up

Signed-off-by: Sam Yuan <[email protected]>

* add benchmark test and adjust regex impl solution

Signed-off-by: Sam Yuan <[email protected]>

* attempt

Signed-off-by: Sam Yuan <[email protected]>

* fix up

Signed-off-by: Sam Yuan <[email protected]>

* 174 (#210)

* use kingpin global flag and adding test case with timeout command

Signed-off-by: Sam Yuan <[email protected]>

* impl for #174

Signed-off-by: Sam Yuan <[email protected]>

* adding test case

Signed-off-by: Sam Yuan <[email protected]>

* fix up

Signed-off-by: Sam Yuan <[email protected]>

* fix up

Signed-off-by: Sam Yuan <[email protected]>

* fix up

Signed-off-by: Sam Yuan <[email protected]>

* fix up

Signed-off-by: Sam Yuan <[email protected]>

* fix up

Signed-off-by: Sam Yuan <[email protected]>

* add signal listening

Signed-off-by: Sam Yuan <[email protected]>

* fix up

Signed-off-by: Sam Yuan <[email protected]>

* fix up

Signed-off-by: Sam Yuan <[email protected]>

* fix up

Signed-off-by: Sam Yuan <[email protected]>

* adding patch for #174 (#211)

* adding patch for #174

Signed-off-by: Sam Yuan <[email protected]>

* bug fix

Signed-off-by: Sam Yuan <[email protected]>

* fix up

Signed-off-by: Sam Yuan <[email protected]>

* fix up

Signed-off-by: Sam Yuan <[email protected]>

* impl for #213 (#214)

* impl for #213

Signed-off-by: Sam Yuan <[email protected]>

* fix up

Signed-off-by: Sam Yuan <[email protected]>

* fix up

Signed-off-by: Sam Yuan <[email protected]>

* fix up

Signed-off-by: Sam Yuan <[email protected]>

* fix up

Signed-off-by: Sam Yuan <[email protected]>

* fix up

Signed-off-by: Sam Yuan <[email protected]>

* fix up

Signed-off-by: Sam Yuan <[email protected]>

* fix up

Signed-off-by: Sam Yuan <[email protected]>

* add orderer observer into full workflow process (#216)

* add orderer observer into full workflow process

Signed-off-by: Sam Yuan <[email protected]>

* fix up

Signed-off-by: Sam Yuan <[email protected]>

* fix up

Signed-off-by: Sam Yuan <[email protected]>

* fix up

Signed-off-by: Sam Yuan <[email protected]>

* impl for txid tracing at traffic generate (#218)

* impl for txid tracing at traffic generate

Signed-off-by: Sam Yuan <[email protected]>

* fix up

Signed-off-by: Sam Yuan <[email protected]>

* fix up

Signed-off-by: Sam Yuan <[email protected]>

* fix up

Signed-off-by: Sam Yuan <[email protected]>

* try to add tx tracing for observer (#219)

Signed-off-by: Sam Yuan <[email protected]>

* nit fix (#220)

Signed-off-by: Sam Yuan <[email protected]>

* Nitfix for date format (#221)

* use RFC3339Nano

Signed-off-by: Sam Yuan <[email protected]>

* fix up

Signed-off-by: Sam Yuan <[email protected]>

* impl for #223 (#225)

* impl for #223

Signed-off-by: Sam Yuan <[email protected]>

* fix up

Signed-off-by: Sam Yuan <[email protected]>

* fix up (#226)

* fix up

Signed-off-by: Sam Yuan <[email protected]>

* fix up

Signed-off-by: Sam Yuan <[email protected]>

* fix up

Signed-off-by: Sam Yuan <[email protected]>

* fix up (#227)

* fix up

Signed-off-by: Sam Yuan <[email protected]>

* fix up

Signed-off-by: Sam Yuan <[email protected]>

* fix up

Signed-off-by: Sam Yuan <[email protected]>

* fix up

Signed-off-by: Sam Yuan <[email protected]>

* fix up delete map to avoid OOM (#228)

Signed-off-by: Sam Yuan <[email protected]>

* update for git doc (#235)

Signed-off-by: Sam Yuan <[email protected]>

* impl for policy support (#237)

* add plan

Signed-off-by: Sam Yuan <[email protected]>

* update ginkgo and add test for policy impl

Signed-off-by: Sam Yuan <[email protected]>

* fix up

Signed-off-by: Sam Yuan <[email protected]>

* fix up

Signed-off-by: Sam Yuan <[email protected]>

* fix up

Signed-off-by: Sam Yuan <[email protected]>

* fix up

Signed-off-by: Sam Yuan <[email protected]>

* fix up

Signed-off-by: Sam Yuan <[email protected]>

* try to impl with policy support

Signed-off-by: Sam Yuan <[email protected]>

* fix up

Signed-off-by: Sam Yuan <[email protected]>

* fix up

Signed-off-by: Sam Yuan <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

No branches or pull requests

2 participants