Skip to content

Commit

Permalink
Merge pull request #376 from bojand/no_template
Browse files Browse the repository at this point in the history
add disable-template-funcs and disable-template-data option
  • Loading branch information
bojand authored Nov 7, 2022
2 parents 5b87c14 + 22d03de commit 6ecfa71
Show file tree
Hide file tree
Showing 15 changed files with 229 additions and 62 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@ jobs:
runs-on: ubuntu-latest
env:
GO111MODULE: on
GOLANGCI_LINT_VERSION: v1.46.2
GOLANGCI_LINT_VERSION: v1.49.0

steps:
- name: Set up Go
uses: actions/setup-go@v3
with:
go-version: 1.18
go-version: 1.19
id: go
- name: Check out code
uses: actions/checkout@v3
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v3
with:
go-version: 1.18
go-version: 1.19

- name: Get current time
uses: gerred/actions/current-time@master
Expand Down
13 changes: 6 additions & 7 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ issues:
# Since we purposefully do that. See comments in code.
- path: runner/requester.go
text: "lostcancel"

# TODO Look into fixing time.Tick() usage SA1015 in worker.go
- path: runner/worker.go
text: "SA1015"

# We intentionally assign nil to err
- path: runner/worker.go
text: "ineffectual assignment to `err`"
Expand All @@ -30,13 +30,12 @@ issues:

# TODO fix protobuf deprecated
- path: runner/
text: "SA1019: package github.com/golang/protobuf"
text: 'SA1019: "github.com/golang/protobuf/proto"'

# TODO fix protobuf deprecated
- path: protodesc/
text: "SA1019: package github.com/golang/protobuf"
text: 'SA1019: "github.com/golang/protobuf/proto"'

# TODO fix protobuf deprecated
- path: runner/
text: "SA1019: \"github.com/golang/protobuf/jsonpb\" is deprecated"

text: 'SA1019: "github.com/golang/protobuf/jsonpb" is deprecated'
20 changes: 20 additions & 0 deletions cmd/ghz/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,14 @@ var (
maxSendMsgSize = kingpin.Flag("max-send-message-size", "Maximum message size the client can send.").
PlaceHolder(" ").IsSetByUser(&isMaxSendMsgSizeSet).String()

isDisableTemplateFuncsSet = false
disableTemplateFuncs = kingpin.Flag("disable-template-functions", "Do not use and execute any template functions in call template data. Useful for better performance").
Default("false").IsSetByUser(&isDisableTemplateFuncsSet).Bool()

isDisableTemplateDataSet = false
disableTemplateData = kingpin.Flag("disable-template-data", "Do not use and execute any call template data. Useful for better performance.").
Default("false").IsSetByUser(&isDisableTemplateDataSet).Bool()

// host main argument
isHostSet = false
host = kingpin.Arg("host", "Host and port to test.").String()
Expand Down Expand Up @@ -507,6 +515,8 @@ func createConfigFromArgs(cfg *runner.Config) error {
cfg.LBStrategy = *lbStrategy
cfg.MaxCallRecvMsgSize = *maxRecvMsgSize
cfg.MaxCallSendMsgSize = *maxSendMsgSize
cfg.DisableTemplateFuncs = *disableTemplateFuncs
cfg.DisableTemplateData = *disableTemplateData

return nil
}
Expand Down Expand Up @@ -760,6 +770,16 @@ func mergeConfig(dest *runner.Config, src *runner.Config) error {
dest.MaxCallSendMsgSize = src.MaxCallSendMsgSize
}

// call data template functions behavior
if isDisableTemplateFuncsSet {
dest.DisableTemplateFuncs = src.DisableTemplateFuncs
}

// call data template behavior
if isDisableTemplateDataSet {
dest.DisableTemplateData = src.DisableTemplateData
}

return nil
}

Expand Down
43 changes: 28 additions & 15 deletions runner/calldata.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,27 +50,32 @@ var tmplFuncMap = template.FuncMap{
"randomInt": randomInt,
}

var commonTemplate *template.Template = template.New("call_template_data").
Funcs(tmplFuncMap).
Funcs(template.FuncMap(sprigFuncMap))

// newCallData returns new CallData
func newCallData(
mtd *desc.MethodDescriptor,
funcs template.FuncMap,
workerID string, reqNum int64) *CallData {
workerID string, reqNum int64, withFuncs, withTemplateData bool, funcs template.FuncMap) *CallData {

var t *template.Template
if withTemplateData {
t = template.New("call_template_data")

if withFuncs {
t = t.
Funcs(tmplFuncMap).
Funcs(template.FuncMap(sprigFuncMap))

fns := make(template.FuncMap, len(funcs))
if len(funcs) > 0 {
fns := make(template.FuncMap, len(funcs))

if len(funcs) > 0 {
for k, v := range funcs {
fns[k] = v
for k, v := range funcs {
fns[k] = v
}

t = t.Funcs(fns)
}
}
}

t, _ := commonTemplate.Clone()
t.Funcs(fns)

now := time.Now()
newUUID, _ := uuid.NewRandom()

Expand Down Expand Up @@ -119,6 +124,10 @@ func (td *CallData) Regenerate() *CallData {
}

func (td *CallData) execute(data string) (*bytes.Buffer, error) {
if td.t == nil {
return nil, nil
}

t, err := td.t.Parse(data)
if err != nil {
return nil, err
Expand All @@ -134,6 +143,10 @@ func (td *CallData) execute(data string) (*bytes.Buffer, error) {
// The *parse.Tree field is exported only for use by html/template
// and should be treated as unexported by all other clients.
func (td *CallData) hasAction(data string) (bool, error) {
if td.t == nil {
return false, nil
}

t, err := td.t.Parse(data)
if err != nil {
return false, err
Expand Down Expand Up @@ -165,7 +178,7 @@ func (td *CallData) ExecuteData(data string) ([]byte, error) {
if len(data) > 0 {
input := []byte(data)
tpl, err := td.execute(data)
if err == nil {
if err == nil && tpl != nil {
input = tpl.Bytes()
}

Expand All @@ -181,7 +194,7 @@ func (td *CallData) executeMetadata(metadata string) (map[string]string, error)
if len(metadata) > 0 {
input := []byte(metadata)
tpl, err := td.execute(metadata)
if err == nil {
if err == nil && tpl != nil {
input = tpl.Bytes()
}

Expand Down
20 changes: 10 additions & 10 deletions runner/calldata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func TestCallData_New(t *testing.T) {
assert.NoError(t, err)
assert.NotNil(t, md)

ctd := newCallData(md, nil, "worker_id_123", 100)
ctd := newCallData(md, "worker_id_123", 100, true, true, nil)

assert.NotNil(t, ctd)
assert.Equal(t, "worker_id_123", ctd.WorkerID)
Expand Down Expand Up @@ -73,7 +73,7 @@ func TestCallData_ExecuteData(t *testing.T) {

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ctd := newCallData(md, nil, "worker_id_123", 200)
ctd := newCallData(md, "worker_id_123", 200, true, true, nil)
assert.NotNil(t, ctd)

r, err := ctd.ExecuteData(tt.in)
Expand Down Expand Up @@ -124,7 +124,7 @@ func TestCallData_ExecuteMetadata(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {

ctd := newCallData(md, nil, "worker_id_123", 200)
ctd := newCallData(md, "worker_id_123", 200, true, true, nil)
assert.NotNil(t, ctd)

r, err := ctd.executeMetadata(tt.in)
Expand All @@ -146,7 +146,7 @@ func TestCallTemplateData_ExecuteFuncs(t *testing.T) {

t.Run("newUUID", func(t *testing.T) {

ctd := newCallData(md, nil, "worker_id_123", 200)
ctd := newCallData(md, "worker_id_123", 200, true, true, nil)
assert.NotNil(t, ctd)

// no template
Expand Down Expand Up @@ -194,7 +194,7 @@ func TestCallTemplateData_ExecuteFuncs(t *testing.T) {
})

t.Run("randomString", func(t *testing.T) {
ctd := newCallData(md, nil, "worker_id_123", 200)
ctd := newCallData(md, "worker_id_123", 200, true, true, nil)
assert.NotNil(t, ctd)

// no template
Expand Down Expand Up @@ -256,7 +256,7 @@ func TestCallTemplateData_ExecuteFuncs(t *testing.T) {
})

t.Run("randomInt", func(t *testing.T) {
ctd := newCallData(md, nil, "worker_id_123", 200)
ctd := newCallData(md, "worker_id_123", 200, true, true, nil)
assert.NotNil(t, ctd)

// no template
Expand Down Expand Up @@ -295,17 +295,17 @@ func TestCallTemplateData_ExecuteFuncs(t *testing.T) {

t.Run("custom functions", func(t *testing.T) {

ctd := newCallData(md, nil, "worker_id_123", 200)
ctd := newCallData(md, "worker_id_123", 200, true, true, nil)
assert.NotNil(t, ctd)

ctd = newCallData(md, template.FuncMap{
ctd = newCallData(md, "worker_id_123", 200, true, true, template.FuncMap{
"getSKU": func() string {
return "custom-sku"
},
"newUUID": func() string {
return "custom-uuid"
},
}, "worker_id_123", 200)
})

r, err := ctd.ExecuteData(`{"trace_id":"{{newUUID}}", "span_id":"{{getSKU}}"}`)
assert.NoError(t, err)
Expand All @@ -320,7 +320,7 @@ func TestCallTemplateData_ExecuteFuncs(t *testing.T) {

t.Run("sprig functions", func(t *testing.T) {

ctd := newCallData(md, nil, "worker_id_123", 200)
ctd := newCallData(md, "worker_id_123", 200, true, true, nil)
assert.NotNil(t, ctd)

r, err := ctd.ExecuteData(`{"trace_id":"{{add 1 2}}"}`)
Expand Down
2 changes: 2 additions & 0 deletions runner/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,8 @@ type Config struct {
LBStrategy string `json:"lb-strategy" toml:"lb-strategy" yaml:"lb-strategy"`
MaxCallRecvMsgSize string `json:"max-recv-message-size" toml:"max-recv-message-size" yaml:"max-recv-message-size"`
MaxCallSendMsgSize string `json:"max-send-message-size" toml:"max-send-message-size" yaml:"max-send-message-size"`
DisableTemplateFuncs bool `json:"disable-template-functions" toml:"disable-template-functions" yaml:"disable-template-functions"`
DisableTemplateData bool `json:"disable-template-data" toml:"disable-template-data" yaml:"disable-template-data"`
}

func checkData(data interface{}) error {
Expand Down
33 changes: 20 additions & 13 deletions runner/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ type mdProvider struct {

func newDataProvider(mtd *desc.MethodDescriptor,
binary bool, dataFunc BinaryDataFunc, data []byte,
funcs template.FuncMap) (*dataProvider, error) {
withFuncs, withTemplateData bool, funcs template.FuncMap) (*dataProvider, error) {

dp := dataProvider{
binary: binary,
Expand Down Expand Up @@ -98,12 +98,15 @@ func newDataProvider(mtd *desc.MethodDescriptor,
}

// Test if we can preseed data
ctd := newCallData(mtd, funcs, "", 0)
ha := false
if !dp.binary {
ha, err = ctd.hasAction(string(dp.data))
if err != nil {
return nil, err
ctd := newCallData(mtd, "", 0, withFuncs, withTemplateData, funcs)

if withTemplateData {
if !dp.binary {
ha, err = ctd.hasAction(string(dp.data))
if err != nil {
return nil, err
}
}
}

Expand Down Expand Up @@ -221,9 +224,9 @@ func (dp *dataProvider) getMessages(ctd *CallData, i int, inputData []byte) ([]*
return inputs, nil
}

func newMetadataProvider(mtd *desc.MethodDescriptor, mdData []byte, funcs template.FuncMap) (*mdProvider, error) {
func newMetadataProvider(mtd *desc.MethodDescriptor, mdData []byte, withFuncs, withTemplateData bool, funcs template.FuncMap) (*mdProvider, error) {
// Test if we can preseed data
ctd := newCallData(mtd, funcs, "", 0)
ctd := newCallData(mtd, "", 0, withFuncs, withTemplateData, funcs)
ha, err := ctd.hasAction(string(mdData))
if err != nil {
return nil, err
Expand Down Expand Up @@ -389,7 +392,7 @@ type dynamicMessageProvider struct {
indexCounter uint
}

func newDynamicMessageProvider(mtd *desc.MethodDescriptor, data []byte, streamCallCount uint) (*dynamicMessageProvider, error) {
func newDynamicMessageProvider(mtd *desc.MethodDescriptor, data []byte, streamCallCount uint, withFuncs, withTemplateData bool) (*dynamicMessageProvider, error) {
mp := dynamicMessageProvider{
mtd: mtd,
data: data,
Expand Down Expand Up @@ -419,10 +422,14 @@ func newDynamicMessageProvider(mtd *desc.MethodDescriptor, data []byte, streamCa
mp.arrayLen = uint(len(mp.arrayJSONData))

// Test if we have actions
ctd := newCallData(mtd, nil, "", 0)
ha, err := ctd.hasAction(string(mp.data))
if err != nil {
return nil, err
ha := false
ctd := newCallData(mtd, "", 0, withFuncs, withTemplateData, nil)

if withTemplateData {
ha, err = ctd.hasAction(string(mp.data))
if err != nil {
return nil, err
}
}

if !ha {
Expand Down
Loading

0 comments on commit 6ecfa71

Please sign in to comment.