From 2b96b8f4e1191a826948949ada15657a01c63a20 Mon Sep 17 00:00:00 2001 From: Bojan Djurkovic Date: Fri, 1 Jun 2018 15:37:11 -0300 Subject: [PATCH] Fix -z duration option and add -x duration option --- cmd/grpcannon/main.go | 8 ++++-- config/config.go | 19 +++++++++++++-- config/config_test.go | 31 +++++++++++++++++++++++- testdata/localhost.crt | 28 ++++++++++----------- testdata/localhost.key | 55 +++++++++++++++++++++--------------------- 5 files changed, 95 insertions(+), 46 deletions(-) diff --git a/cmd/grpcannon/main.go b/cmd/grpcannon/main.go index 25dd5391..3aeadd50 100644 --- a/cmd/grpcannon/main.go +++ b/cmd/grpcannon/main.go @@ -31,7 +31,8 @@ var ( n = flag.Int("n", 200, "Number of requests to run. Default is 200.") q = flag.Int("q", 0, "Rate limit, in queries per second (QPS). Default is no rate limit.") t = flag.Int("t", 20, "Timeout for each request in seconds.") - z = flag.Duration("z", 0, "") + z = flag.Duration("z", 0, "Duration of application to send requests.") + x = flag.Duration("x", 0, "Maximum duration of application to send requests.") data = flag.String("d", "", "The call data as stringified JSON.") dataPath = flag.String("D", "", "Path for call data JSON file.") @@ -70,6 +71,9 @@ Options: -z Duration of application to send requests. When duration is reached, application stops and exits. If duration is specified, n is ignored. Examples: -z 10s -z 3m. + -x Maximum duration of application to send requests with n setting respected. + If duration is reached before n requests are completed, application stops and exits. + Examples: -x 10s -x 3m. -d The call data as stringified JSON. -D Path for call data JSON file. For example, /home/user/file.json or ./file.json. @@ -132,7 +136,7 @@ func main() { iPaths = strings.Split(pathsTrimmed, ",") } - cfg, err = config.New(*proto, *protoset, *call, *cert, *cname, *n, *c, *q, *z, *t, + cfg, err = config.New(*proto, *protoset, *call, *cert, *cname, *n, *c, *q, *z, *x, *t, *data, *dataPath, *md, *mdPath, *output, *format, host, *ct, *kt, *cpus, iPaths) if err != nil { errAndExit(err.Error()) diff --git a/config/config.go b/config/config.go index 4f684799..4248f792 100644 --- a/config/config.go +++ b/config/config.go @@ -4,6 +4,7 @@ import ( "encoding/json" "fmt" "io/ioutil" + "math" "path/filepath" "runtime" "strings" @@ -23,6 +24,7 @@ type Config struct { C int `json:"c"` QPS int `json:"q"` Z time.Duration `json:"z"` + X time.Duration `json:"x"` Timeout int `json:"t"` Data interface{} `json:"d,omitempty"` DataPath string `json:"D"` @@ -38,8 +40,8 @@ type Config struct { } // New creates a new config -func New(proto, protoset, call, cert, cName string, n, c, qps int, z time.Duration, timeout int, - data, dataPath, metadata, mdPath, output, format, host string, +func New(proto, protoset, call, cert, cName string, n, c, qps int, z time.Duration, x time.Duration, + timeout int, data, dataPath, metadata, mdPath, output, format, host string, dialTimout, keepaliveTime, cpus int, importPaths []string) (*Config, error) { cfg := &Config{ @@ -52,6 +54,7 @@ func New(proto, protoset, call, cert, cName string, n, c, qps int, z time.Durati C: c, QPS: qps, Z: z, + X: x, Timeout: timeout, DataPath: dataPath, MetadataPath: mdPath, @@ -174,6 +177,7 @@ func (c *Config) UnmarshalJSON(data []byte) error { type Alias Config aux := &struct { Z string `json:"z"` + X string `json:"x"` *Alias }{ Alias: (*Alias)(c), @@ -199,6 +203,7 @@ func (c Config) MarshalJSON() ([]byte, error) { return json.Marshal(&struct { *Alias Z string `json:"z"` + X string `json:"x"` }{ Alias: (*Alias)(&c), Z: c.Z.String(), @@ -263,6 +268,14 @@ func (c *Config) initMetadata() error { return nil } +func (c *Config) initDurations() { + if c.X > 0 { + c.Z = c.X + } else if c.Z > 0 { + c.N = math.MaxInt32 + } +} + func (c *Config) init() error { err := c.initData() if err != nil { @@ -274,6 +287,8 @@ func (c *Config) init() error { return err } + c.initDurations() + c.Default() err = c.Validate() diff --git a/config/config_test.go b/config/config_test.go index 3ef39b5a..f9e85369 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -3,6 +3,7 @@ package config import ( "encoding/json" "io/ioutil" + "math" "os" "runtime" "testing" @@ -11,7 +12,7 @@ import ( "github.com/stretchr/testify/assert" ) -const expected = `{"proto":"asdf","protoset":"","call":"","cert":"","cName":"","n":0,"c":0,"q":0,"t":0,"D":"","M":"","o":"","O":"oval","host":"","T":0,"L":0,"cpus":0,"z":"4h30m0s"}` +const expected = `{"proto":"asdf","protoset":"","call":"","cert":"","cName":"","n":0,"c":0,"q":0,"t":0,"D":"","M":"","o":"","O":"oval","host":"","T":0,"L":0,"cpus":0,"z":"4h30m0s","x":""}` func TestConfig_MarshalJSON(t *testing.T) { z, _ := time.ParseDuration("4h30m") @@ -418,3 +419,31 @@ func TestConfig_initData(t *testing.T) { assert.Equal(t, c.Data, data) }) } + +func TestConfig_initDurations(t *testing.T) { + t.Run("with Z specified should set N to max int", func(t *testing.T) { + dur, _ := time.ParseDuration("4h30m") + c := &Config{N: 500, Z: dur} + c.initDurations() + assert.Equal(t, c.N, math.MaxInt32) + }) + + t.Run("with X specified should set Z to X and keep N", func(t *testing.T) { + dur, _ := time.ParseDuration("4h30m") + c := &Config{N: 500, X: dur} + c.initDurations() + assert.Equal(t, c.N, 500) + assert.Equal(t, c.X, dur) + assert.Equal(t, c.Z, dur) + }) + + t.Run("with X and Z specified should set Z to X and keep N", func(t *testing.T) { + dur, _ := time.ParseDuration("4m") + dur2, _ := time.ParseDuration("5m") + c := &Config{N: 500, X: dur, Z: dur2} + c.initDurations() + assert.Equal(t, c.N, 500) + assert.Equal(t, c.X, dur) + assert.Equal(t, c.Z, dur) + }) +} diff --git a/testdata/localhost.crt b/testdata/localhost.crt index 27eff570..478d2248 100644 --- a/testdata/localhost.crt +++ b/testdata/localhost.crt @@ -1,18 +1,18 @@ -----BEGIN CERTIFICATE----- -MIIC5TCCAc2gAwIBAgIJALLvqFlCiW3VMA0GCSqGSIb3DQEBCwUAMBQxEjAQBgNV -BAMMCWxvY2FsaG9zdDAeFw0xODA0MDcxNjU0MjRaFw0xODA1MDcxNjU0MjRaMBQx +MIIC5TCCAc2gAwIBAgIJAJFwlruqLNgaMA0GCSqGSIb3DQEBCwUAMBQxEjAQBgNV +BAMMCWxvY2FsaG9zdDAeFw0x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 -Rj2egomZYCQlOaWyqB7jA6pZM5JHBQOGPJttuASFZC8IdhNrI25Ki8JBibpT3DqK -BJP4PccK8gGT5yxtv7fYf3Tp4Spsiqk0yyRawAIKXCbIpSTY7MrjYJ9iqzTT7WiY -NXMRASmS7yTP4NK8Um1X6GP4+gAuh5UeNnYAGFCqqYznxrohoNbqEnp3E4Oqy/EQ -6qe8HePlE+oho7TecaZRvuLXcHMCVatJMA== +AQsFAAOCAQEAK3MQsX8AN9x1FeOV79Qitk9Hzy0UoGDGcfn/so/2cAbU1mY1Uk6h +W9iCLiBKrKkoO8Ys6hR5Rx5f809pbe7hKmyQ4C0WdGxwUydAPloKj4kvf8oWcPRd +SYxuaeD7I/tGuet7A5krneUKJi2bZtB/8OiDg64PRHNbK3wU25yB9t14aQbeXEzF +qDlId9HZVZLVOb0G/XhcSX5kHiVVrk97x3hXsMh8iAt7zjbn2y8QitKsTpGpfsZ2 +j2a8OoV/shKt20Ea7zGMc/U/UDp3auKnD1RMA1Z11sht5cW+l8YTJ7UVLfZEdlnP +XQSacAbjHwZ/YxEdSCiKTc+8wHGTb28aeQ== -----END CERTIFICATE----- diff --git a/testdata/localhost.key b/testdata/localhost.key index e024c723..898e7cd7 100644 --- a/testdata/localhost.key +++ b/testdata/localhost.key @@ -1,27 +1,28 @@ ------BEGIN RSA PRIVATE KEY----- -MIIEpQIBAAKCAQEA3OZyIWGK2rOawcscoLH/IwcVg4BccEWdatqz41xb/mIPKbU5 -7VCBefTLVxfVv1wg1wvnEeI2MjeIVbb8U9DxNnY6kXHcwD8dbSto7Jmqh+5t9KLJ -BclXamL7mmyM//3+iA+Ve5aO2YCyf3bdeF7Eo4vEOTefmSGAPvi3Gtw61fxUY8ac -wcsO1RYJ2JnBIqhxY2pTb6szDd+K+7LM8pXHm4wHcUOOogdNPKstFJHxa1FlBoj5 -ewheJgLfW1WcSe97Yug9Fug2B1UNvuten/HZJQ5Ac3aZ6cvLc/qSRSIAbvzujF3G -Xa/R1yA8ZYNN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