Skip to content

Commit

Permalink
Merge pull request #115 from bojand/time_duration
Browse files Browse the repository at this point in the history
Change uint duration values to Duration type. Fixes #104.
  • Loading branch information
bojand committed Jul 29, 2019
2 parents 4009959 + 04c42a7 commit 2f1babe
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 45 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ test:

# gofmt and goimports all go files
fmt:
find . -name '*.go' -not -wholename './vendor/*' | while read -r file; do gofmt -w -s "$$file"; goimports -w "$$file"; done
find . -name '*.go' | while read -r file; do gofmt -w -s "$$file"; goimports -w "$$file"; done
.PHONY: fmt

# Cover runs go_test on GO_PKGS and produces code coverage in multiple formats.
Expand Down
68 changes: 43 additions & 25 deletions cmd/ghz/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ type config struct {
QPS uint `json:"qps" toml:"qps" yaml:"qps"`
Z Duration `json:"duration" toml:"duration" yaml:"duration"`
X Duration `json:"max-duration" toml:"max-duration" yaml:"max-duration"`
Timeout uint `json:"timeout" toml:"timeout" yaml:"timeout" default:"20"`
Timeout Duration `json:"timeout" toml:"timeout" yaml:"timeout" default:"20s"`
Data interface{} `json:"data,omitempty" toml:"data,omitempty" yaml:"data,omitempty"`
DataPath string `json:"data-file" toml:"data-file" yaml:"data-file"`
BinData []byte `json:"-" toml:"-" yaml:"-"`
Expand All @@ -57,8 +57,8 @@ type config struct {
SI Duration `json:"stream-interval" toml:"stream-interval" yaml:"stream-interval"`
Output string `json:"output" toml:"output" yaml:"output"`
Format string `json:"format" toml:"format" yaml:"format" default:"summary"`
DialTimeout uint `json:"connect-timeout" toml:"connect-timeout" yaml:"connect-timeout" default:"10"`
KeepaliveTime uint `json:"keepalive" toml:"keepalive" yaml:"keepalive"`
DialTimeout Duration `json:"connect-timeout" toml:"connect-timeout" yaml:"connect-timeout" default:"10s"`
KeepaliveTime Duration `json:"keepalive" toml:"keepalive" yaml:"keepalive"`
CPUs uint `json:"cpus" toml:"cpus" yaml:"cpus"`
ImportPaths []string `json:"import-paths,omitempty" toml:"import-paths,omitempty" yaml:"import-paths,omitempty"`
Name string `json:"name,omitempty" toml:"name,omitempty" yaml:"name,omitempty"`
Expand All @@ -72,9 +72,10 @@ type config struct {
func (c *config) UnmarshalJSON(data []byte) error {
type Alias config
aux := &struct {
Z string `json:"z"`
X string `json:"x"`
SI string `json:"si"`
Z string `json:"z"`
X string `json:"x"`
SI string `json:"si"`
Timeout string `json:"timeout"`
*Alias
}{
Alias: (*Alias)(c),
Expand All @@ -90,26 +91,41 @@ func (c *config) UnmarshalJSON(data []byte) error {
}
}

zd, err := time.ParseDuration(aux.Z)
if err != nil {
return nil
if aux.Z != "" {
zd, err := time.ParseDuration(aux.Z)
if err != nil {
return nil
}

c.Z = Duration(zd)
}

c.Z = Duration(zd)
if aux.X != "" {
xd, err := time.ParseDuration(aux.X)
if err != nil {
return nil
}

xd, err := time.ParseDuration(aux.X)
if err != nil {
return nil
c.X = Duration(xd)
}

c.X = Duration(xd)
if aux.SI != "" {
sid, err := time.ParseDuration(aux.SI)
if err != nil {
return nil
}

sid, err := time.ParseDuration(aux.SI)
if err != nil {
return nil
c.SI = Duration(sid)
}

c.SI = Duration(sid)
if aux.Timeout != "" {
td, err := time.ParseDuration(aux.Timeout)
if err != nil {
return nil
}

c.Timeout = Duration(td)
}

return nil
}
Expand All @@ -119,14 +135,16 @@ func (c config) MarshalJSON() ([]byte, error) {
type Alias config
return json.Marshal(&struct {
*Alias
Z string `json:"z"`
X string `json:"x"`
SI string `json:"si"`
Z string `json:"z"`
X string `json:"x"`
SI string `json:"si"`
Timeout string `json:"timeout"`
}{
Alias: (*Alias)(&c),
Z: c.Z.String(),
X: c.X.String(),
SI: c.SI.String(),
Alias: (*Alias)(&c),
Z: c.Z.String(),
X: c.X.String(),
SI: c.SI.String(),
Timeout: c.Timeout.String(),
})
}

Expand Down
18 changes: 9 additions & 9 deletions cmd/ghz/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ var (
c = kingpin.Flag("concurrency", "Number of requests to run concurrently. Total number of requests cannot be smaller than the concurrency level. Default is 50.").Short('c').Default("50").Uint()
n = kingpin.Flag("total", "Number of requests to run. Default is 200.").Short('n').Default("200").Uint()
q = kingpin.Flag("qps", "Rate limit, in queries per second (QPS). Default is no rate limit.").Default("0").Short('q').Uint()
t = kingpin.Flag("timeout", "Timeout for each request in seconds. Default is 20, use 0 for infinite.").Default("20").Short('t').Uint()
t = kingpin.Flag("timeout", "Timeout for each request. Default is 20s, use 0 for infinite.").Default("20s").Short('t').Duration()
z = kingpin.Flag("duration", "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.").Short('z').Default("0").Duration()
x = kingpin.Flag("max-duration", "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.").Short('x').Default("0").Duration()

Expand All @@ -59,8 +59,8 @@ var (
output = kingpin.Flag("output", "Output path. If none provided stdout is used.").Short('o').PlaceHolder(" ").String()
format = kingpin.Flag("format", "Output format. One of: summary, csv, json, pretty, html, influx-summary, influx-details. Default is summary.").Short('O').Default("summary").PlaceHolder(" ").Enum("summary", "csv", "json", "pretty", "html", "influx-summary", "influx-details")

ct = kingpin.Flag("connect-timeout", "Connection timeout in seconds for the initial connection dial. Default is 10.").Default("10").Uint()
kt = kingpin.Flag("keepalive", "Keepalive time in seconds. Only used if present and above 0.").Default("0").Uint()
ct = kingpin.Flag("connect-timeout", "Connection timeout for the initial connection dial. Default is 10s.").Default("10s").Duration()
kt = kingpin.Flag("keepalive", "Keepalive time duration. Only used if present and above 0.").Default("0").Duration()

name = kingpin.Flag("name", "User specified name for the test.").PlaceHolder(" ").String()
tags = kingpin.Flag("tags", "JSON representation of user-defined string tags.").PlaceHolder(" ").String()
Expand Down Expand Up @@ -115,10 +115,10 @@ func main() {
runner.WithConcurrency(cfg.C),
runner.WithTotalRequests(cfg.N),
runner.WithQPS(cfg.QPS),
runner.WithTimeout(time.Duration(cfg.Timeout)*time.Second),
runner.WithTimeout(time.Duration(cfg.Timeout)),
runner.WithRunDuration(time.Duration(cfg.Z)),
runner.WithDialTimeout(time.Duration(cfg.DialTimeout)*time.Second),
runner.WithKeepalive(time.Duration(cfg.KeepaliveTime)*time.Second),
runner.WithDialTimeout(time.Duration(cfg.DialTimeout)),
runner.WithKeepalive(time.Duration(cfg.KeepaliveTime)),
runner.WithName(cfg.Name),
runner.WithCPUs(cfg.CPUs),
runner.WithMetadata(cfg.Metadata),
Expand Down Expand Up @@ -252,7 +252,7 @@ func createConfigFromArgs() (*config, error) {
QPS: *q,
Z: Duration(*z),
X: Duration(*x),
Timeout: *t,
Timeout: Duration(*t),
Data: dataObj,
DataPath: *dataPath,
BinData: binaryData,
Expand All @@ -263,8 +263,8 @@ func createConfigFromArgs() (*config, error) {
Output: *output,
Format: *format,
ImportPaths: iPaths,
DialTimeout: *ct,
KeepaliveTime: *kt,
DialTimeout: Duration(*ct),
KeepaliveTime: Duration(*kt),
CPUs: *cpus,
Name: *name,
Tags: &tagsMap,
Expand Down
10 changes: 0 additions & 10 deletions internal/tools/tools.go

This file was deleted.

0 comments on commit 2f1babe

Please sign in to comment.