Skip to content

Commit

Permalink
add disable-template-funcs option
Browse files Browse the repository at this point in the history
  • Loading branch information
bojand committed Nov 5, 2022
1 parent 5021ba3 commit 12e5873
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 15 deletions.
10 changes: 10 additions & 0 deletions cmd/ghz/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,10 @@ 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()

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

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

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

return nil
}

Expand Down
1 change: 1 addition & 0 deletions runner/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ 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"`
}

func checkData(data interface{}) error {
Expand Down
12 changes: 6 additions & 6 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 bool, funcs template.FuncMap) (*dataProvider, error) {

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

// Test if we can preseed data
ctd := newCallData(mtd, "", 0, true, funcs)
ctd := newCallData(mtd, "", 0, withFuncs, funcs)
ha := false
if !dp.binary {
ha, err = ctd.hasAction(string(dp.data))
Expand Down Expand Up @@ -221,9 +221,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 bool, funcs template.FuncMap) (*mdProvider, error) {
// Test if we can preseed data
ctd := newCallData(mtd, "", 0, true, funcs)
ctd := newCallData(mtd, "", 0, withFuncs, funcs)
ha, err := ctd.hasAction(string(mdData))
if err != nil {
return nil, err
Expand Down Expand Up @@ -389,7 +389,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 bool) (*dynamicMessageProvider, error) {
mp := dynamicMessageProvider{
mtd: mtd,
data: data,
Expand Down Expand Up @@ -419,7 +419,7 @@ func newDynamicMessageProvider(mtd *desc.MethodDescriptor, data []byte, streamCa
mp.arrayLen = uint(len(mp.arrayJSONData))

// Test if we have actions
ctd := newCallData(mtd, "", 0, true, nil)
ctd := newCallData(mtd, "", 0, withFuncs, nil)
ha, err := ctd.hasAction(string(mp.data))
if err != nil {
return nil, err
Expand Down
10 changes: 5 additions & 5 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"}`), nil)
mdp, err := newMetadataProvider(mtdUnary, []byte(`{"token":"asdf"}`), 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 }}"}`), nil)
mdp, err := newMetadataProvider(mtdUnary, []byte(`{"token":"{{ .RequestNumber }}"}`), true, nil)
assert.NoError(t, err)
assert.NotNil(t, mdp)
assert.Nil(t, mdp.preseed)
Expand All @@ -304,7 +304,7 @@ func TestMetadata_getMetadataForCall(t *testing.T) {
nil)
assert.NoError(t, err)

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

Expand All @@ -324,7 +324,7 @@ func TestMetadata_getMetadataForCall(t *testing.T) {
nil)
assert.NoError(t, err)

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

Expand Down Expand Up @@ -359,7 +359,7 @@ func TestMetadata_getMetadataForCall(t *testing.T) {
},
}

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

Expand Down
13 changes: 13 additions & 0 deletions runner/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,9 @@ type RunConfig struct {
hasLog bool
log Logger

// template call data
disableTemplateFuncs bool

// misc
name string
cpus int
Expand Down Expand Up @@ -1095,6 +1098,15 @@ func WithDefaultCallOptions(opts []grpc.CallOption) Option {
}
}

// WithDisableTemplateFuncs disables template functions in call data
func WithDisableTemplateFuncs(v bool) Option {
return func(o *RunConfig) error {
o.disableTemplateFuncs = v

return nil
}
}

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

Expand Down Expand Up @@ -1187,6 +1199,7 @@ func fromConfig(cfg *Config) []Option {
WithConcurrencyStepDuration(time.Duration(cfg.CStepDuration)),
WithConcurrencyDuration(time.Duration(cfg.CMaxDuration)),
WithCountErrors(cfg.CountErrors),
WithDisableTemplateFuncs(cfg.DisableTemplateFuncs),
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.funcs)
defaultDataProvider, err := newDataProvider(reqr.mtd, c.binary, c.dataFunc, c.data, !c.disableTemplateFuncs, 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.funcs)
defaultMDProvider, err := newMetadataProvider(reqr.mtd, c.metadata, !c.disableTemplateFuncs, c.funcs)
if err != nil {
return nil, err
}
Expand Down
34 changes: 34 additions & 0 deletions runner/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package runner
import (
"fmt"
"strconv"
"strings"
"testing"
"text/template"
"time"
Expand Down Expand Up @@ -217,6 +218,39 @@ func TestRunUnary(t *testing.T) {
assert.NotEmpty(t, parsed)
})

t.Run("test disabled template functions", func(t *testing.T) {
gs.ResetCounters()

data := make(map[string]interface{})
data["name"] = "{{ newUUID }}"

report, err := Run(
"helloworld.Greeter.SayHello",
internal.TestLocalhost,
WithProtoFile("../testdata/greeter.proto", []string{}),
WithTotalRequests(1),
WithConcurrency(1),
WithTimeout(time.Duration(20*time.Second)),
WithData(data),
WithInsecure(true),
WithDisableTemplateFuncs(true),
)

assert.Error(t, err)
assert.True(t, strings.Contains(err.Error(), `function "newUUID" not defined`))
assert.Nil(t, report)

// count := gs.GetCount(callType)
// assert.Equal(t, 1, count)

// calls := gs.GetCalls(callType)
// assert.NotNil(t, calls)
// assert.Len(t, calls, 1)

// msg := calls[0][0]
// assert.Equal(t, "{{ newUUID }}", msg.GetName())
})

t.Run("test skip first N", func(t *testing.T) {
gs.ResetCounters()

Expand Down
4 changes: 2 additions & 2 deletions runner/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func (w *Worker) Stop() {
func (w *Worker) makeRequest(tv TickValue) error {
reqNum := int64(tv.reqNumber)

ctd := newCallData(w.mtd, w.workerID, reqNum, true, w.config.funcs)
ctd := newCallData(w.mtd, w.workerID, reqNum, !w.config.disableTemplateFuncs, w.config.funcs)

reqMD, err := w.metadataProvider(ctd)
if err != nil {
Expand Down Expand Up @@ -116,7 +116,7 @@ func (w *Worker) makeRequest(tv TickValue) error {
msgProvider = w.msgProvider
} else if w.mtd.IsClientStreaming() {
if w.config.streamDynamicMessages {
mp, err := newDynamicMessageProvider(w.mtd, w.config.data, w.config.streamCallCount)
mp, err := newDynamicMessageProvider(w.mtd, w.config.data, w.config.streamCallCount, !w.config.disableTemplateFuncs)
if err != nil {
return err
}
Expand Down

0 comments on commit 12e5873

Please sign in to comment.