Skip to content

Commit

Permalink
add disable-template-data option
Browse files Browse the repository at this point in the history
  • Loading branch information
bojand committed Nov 6, 2022
1 parent ab7fa04 commit ec50830
Show file tree
Hide file tree
Showing 10 changed files with 151 additions and 57 deletions.
12 changes: 11 additions & 1 deletion cmd/ghz/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,10 @@ var (
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 @@ -512,6 +516,7 @@ func createConfigFromArgs(cfg *runner.Config) error {
cfg.MaxCallRecvMsgSize = *maxRecvMsgSize
cfg.MaxCallSendMsgSize = *maxSendMsgSize
cfg.DisableTemplateFuncs = *disableTemplateFuncs
cfg.DisableTemplateData = *disableTemplateData

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

// call data template behavior
// 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
39 changes: 25 additions & 14 deletions runner/calldata.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,23 +53,26 @@ var tmplFuncMap = template.FuncMap{
// newCallData returns new CallData
func newCallData(
mtd *desc.MethodDescriptor,
workerID string, reqNum int64, withFuncs bool, funcs template.FuncMap) *CallData {
workerID string, reqNum int64, withFuncs, withTemplateData bool, funcs template.FuncMap) *CallData {

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

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

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

for k, v := range funcs {
fns[k] = v
}
for k, v := range funcs {
fns[k] = v
}

t = t.Funcs(fns)
t = t.Funcs(fns)
}
}
}

Expand Down Expand Up @@ -121,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 @@ -136,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 @@ -167,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 @@ -183,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
18 changes: 9 additions & 9 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, "worker_id_123", 100, true, nil)
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, "worker_id_123", 200, true, nil)
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, "worker_id_123", 200, true, nil)
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, "worker_id_123", 200, true, nil)
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, "worker_id_123", 200, true, nil)
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, "worker_id_123", 200, true, nil)
ctd := newCallData(md, "worker_id_123", 200, true, true, nil)
assert.NotNil(t, ctd)

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

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

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

ctd = newCallData(md, "worker_id_123", 200, true, template.FuncMap{
ctd = newCallData(md, "worker_id_123", 200, true, true, template.FuncMap{
"getSKU": func() string {
return "custom-sku"
},
Expand All @@ -320,7 +320,7 @@ func TestCallTemplateData_ExecuteFuncs(t *testing.T) {

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

ctd := newCallData(md, "worker_id_123", 200, true, nil)
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
1 change: 1 addition & 0 deletions runner/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ type Config struct {
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,
withFuncs bool, 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, "", 0, withFuncs, funcs)
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, withFuncs bool, 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, "", 0, withFuncs, funcs)
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, withFuncs bool) (*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, "", 0, withFuncs, nil)
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
18 changes: 9 additions & 9 deletions runner/data_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ func TestMetadata_newMetadataProvider(t *testing.T) {
nil)
assert.NoError(t, err)

mdp, err := newMetadataProvider(mtdUnary, []byte(`{"token":"asdf"}`), true, nil)
mdp, err := newMetadataProvider(mtdUnary, []byte(`{"token":"asdf"}`), true, true, nil)
assert.NoError(t, err)
assert.NotNil(t, mdp)
assert.NotNil(t, mdp.preseed)
Expand All @@ -289,7 +289,7 @@ func TestMetadata_newMetadataProvider(t *testing.T) {
nil)
assert.NoError(t, err)

mdp, err := newMetadataProvider(mtdUnary, []byte(`{"token":"{{ .RequestNumber }}"}`), true, nil)
mdp, err := newMetadataProvider(mtdUnary, []byte(`{"token":"{{ .RequestNumber }}"}`), true, true, nil)
assert.NoError(t, err)
assert.NotNil(t, mdp)
assert.Nil(t, mdp.preseed)
Expand All @@ -304,11 +304,11 @@ func TestMetadata_getMetadataForCall(t *testing.T) {
nil)
assert.NoError(t, err)

mdp, err := newMetadataProvider(mtdUnary, []byte(`{"token":"asdf"}`), true, nil)
mdp, err := newMetadataProvider(mtdUnary, []byte(`{"token":"asdf"}`), true, true, nil)
assert.NoError(t, err)
assert.NotNil(t, mdp.preseed)

cd := newCallData(mtdUnary, "123", 1, true, nil)
cd := newCallData(mtdUnary, "123", 1, true, true, nil)

md, err := mdp.getMetadataForCall(cd)
assert.NoError(t, err)
Expand All @@ -324,19 +324,19 @@ func TestMetadata_getMetadataForCall(t *testing.T) {
nil)
assert.NoError(t, err)

mdp, err := newMetadataProvider(mtdUnary, []byte(`{"token":"{{ .RequestNumber }}"}`), true, nil)
mdp, err := newMetadataProvider(mtdUnary, []byte(`{"token":"{{ .RequestNumber }}"}`), true, true, nil)
assert.NoError(t, err)
assert.Nil(t, mdp.preseed)

cd := newCallData(mtdUnary, "123", 1, true, nil)
cd := newCallData(mtdUnary, "123", 1, true, true, nil)

md1, err := mdp.getMetadataForCall(cd)
assert.NoError(t, err)
assert.NotNil(t, md1)
assert.Equal(t, []string{"1"}, md1.Get("token"))
assert.NotSame(t, mdp.preseed, md1)

cd = newCallData(mtdUnary, "123", 2, true, nil)
cd = newCallData(mtdUnary, "123", 2, true, true, nil)
md2, err := mdp.getMetadataForCall(cd)
assert.NoError(t, err)
assert.NotNil(t, md2)
Expand All @@ -359,11 +359,11 @@ func TestMetadata_getMetadataForCall(t *testing.T) {
},
}

mdp, err := newMetadataProvider(mtdUnary, []byte(`{"token":"{{ customFunc }}"}`), true, funcs)
mdp, err := newMetadataProvider(mtdUnary, []byte(`{"token":"{{ customFunc }}"}`), true, true, funcs)
assert.NoError(t, err)
assert.Nil(t, mdp.preseed)

cd := newCallData(mtdUnary, "123", 1, true, funcs)
cd := newCallData(mtdUnary, "123", 1, true, true, funcs)

md1, err := mdp.getMetadataForCall(cd)
assert.NoError(t, err)
Expand Down
11 changes: 11 additions & 0 deletions runner/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ type RunConfig struct {

// template call data
disableTemplateFuncs bool
disableTemplateData bool

// misc
name string
Expand Down Expand Up @@ -1107,6 +1108,15 @@ func WithDisableTemplateFuncs(v bool) Option {
}
}

// WithDisableTemplateData disables template data execution in call data
func WithDisableTemplateData(v bool) Option {
return func(o *RunConfig) error {
o.disableTemplateData = v

return nil
}
}

func createClientTransportCredentials(skipVerify bool, cacertFile, clientCertFile, clientKeyFile, cname string) (credentials.TransportCredentials, error) {
var tlsConf tls.Config

Expand Down Expand Up @@ -1200,6 +1210,7 @@ func fromConfig(cfg *Config) []Option {
WithConcurrencyDuration(time.Duration(cfg.CMaxDuration)),
WithCountErrors(cfg.CountErrors),
WithDisableTemplateFuncs(cfg.DisableTemplateFuncs),
WithDisableTemplateData(cfg.DisableTemplateData),
func(o *RunConfig) error {
o.call = cfg.Call
return nil
Expand Down
4 changes: 2 additions & 2 deletions runner/requester.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ func NewRequester(c *RunConfig) (*Requester, error) {
if c.dataProviderFunc != nil {
reqr.dataProvider = c.dataProviderFunc
} else {
defaultDataProvider, err := newDataProvider(reqr.mtd, c.binary, c.dataFunc, c.data, !c.disableTemplateFuncs, c.funcs)
defaultDataProvider, err := newDataProvider(reqr.mtd, c.binary, c.dataFunc, c.data, !c.disableTemplateFuncs, !c.disableTemplateData, c.funcs)
if err != nil {
return nil, err
}
Expand All @@ -141,7 +141,7 @@ func NewRequester(c *RunConfig) (*Requester, error) {
if c.mdProviderFunc != nil {
reqr.metadataProvider = c.mdProviderFunc
} else {
defaultMDProvider, err := newMetadataProvider(reqr.mtd, c.metadata, !c.disableTemplateFuncs, c.funcs)
defaultMDProvider, err := newMetadataProvider(reqr.mtd, c.metadata, !c.disableTemplateFuncs, !c.disableTemplateData, c.funcs)
if err != nil {
return nil, err
}
Expand Down
Loading

0 comments on commit ec50830

Please sign in to comment.