Compare commits

..

No commits in common. "4686e26ede689827caa707463732f2b783fcb4a6" and "7a40bbbd3cd6e693faf21c4e03c96d7749bde7b6" have entirely different histories.

6 changed files with 69 additions and 111 deletions

View File

@ -6,7 +6,6 @@ import (
"os" "os"
"time" "time"
. "github.com/aykhans/dodo/types"
"github.com/aykhans/dodo/utils" "github.com/aykhans/dodo/utils"
"github.com/jedib0t/go-pretty/v6/table" "github.com/jedib0t/go-pretty/v6/table"
) )
@ -93,12 +92,12 @@ func (config *RequestConfig) GetMaxConns(minConns uint) uint {
} }
type Config struct { type Config struct {
Method string `json:"method" validate:"http_method"` // custom validations: http_method Method string `json:"method" validate:"http_method"` // custom validations: http_method
URL string `json:"url" validate:"http_url,required"` URL string `json:"url" validate:"http_url,required"`
Timeout uint32 `json:"timeout" validate:"gte=1,lte=100000"` Timeout uint32 `json:"timeout" validate:"gte=1,lte=100000"`
DodosCount uint `json:"dodos_count" validate:"gte=1"` DodosCount uint `json:"dodos_count" validate:"gte=1"`
RequestCount uint `json:"request_count" validation_name:"request-count" validate:"gte=1"` RequestCount uint `json:"request_count" validation_name:"request-count" validate:"gte=1"`
NoProxyCheck Option[bool] `json:"no_proxy_check"` NoProxyCheck utils.Option[bool] `json:"no_proxy_check"`
} }
func NewConfig( func NewConfig(
@ -106,10 +105,10 @@ func NewConfig(
timeout uint32, timeout uint32,
dodosCount uint, dodosCount uint,
requestCount uint, requestCount uint,
noProxyCheck Option[bool], noProxyCheck utils.Option[bool],
) *Config { ) *Config {
if noProxyCheck == nil { if noProxyCheck == nil {
noProxyCheck = NewNoneOption[bool]() noProxyCheck = utils.NewNoneOption[bool]()
} }
return &Config{ return &Config{
@ -156,7 +155,7 @@ func (config *Config) SetDefaults() {
config.RequestCount = DefaultRequestCount config.RequestCount = DefaultRequestCount
} }
if config.NoProxyCheck.IsNone() { if config.NoProxyCheck.IsNone() {
config.NoProxyCheck = NewOption(false) config.NoProxyCheck = utils.NewOption(false)
} }
} }

View File

@ -5,7 +5,6 @@ import (
"github.com/aykhans/dodo/config" "github.com/aykhans/dodo/config"
"github.com/aykhans/dodo/custom_errors" "github.com/aykhans/dodo/custom_errors"
. "github.com/aykhans/dodo/types"
"github.com/aykhans/dodo/utils" "github.com/aykhans/dodo/utils"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/spf13/pflag" "github.com/spf13/pflag"
@ -62,7 +61,7 @@ func CLIConfigReader() (*config.CLIConfig, error) {
case "timeout": case "timeout":
cliConfig.Timeout = timeout cliConfig.Timeout = timeout
case "no-proxy-check": case "no-proxy-check":
cliConfig.NoProxyCheck = NewOption(noProxyCheck) cliConfig.NoProxyCheck = utils.NewOption(noProxyCheck)
} }
}) })
if returnNil { if returnNil {

View File

@ -5,7 +5,7 @@ import (
"os" "os"
"time" "time"
. "github.com/aykhans/dodo/types" "github.com/aykhans/dodo/utils"
"github.com/jedib0t/go-pretty/v6/table" "github.com/jedib0t/go-pretty/v6/table"
) )
@ -18,32 +18,24 @@ type Response struct {
type Responses []*Response type Responses []*Response
// Print prints the responses in a tabular format, including information such as // Print prints the responses in a tabular format, including information such as
// response count, minimum time, maximum time, average time, and latency percentiles. // response count, minimum time, maximum time, and average time.
func (respones Responses) Print() { func (respones Responses) Print() {
total := struct { var (
Count int totalMinDuration time.Duration = respones[0].Time
Min time.Duration totalMaxDuration time.Duration = respones[0].Time
Max time.Duration totalDuration time.Duration
Sum time.Duration totalCount int = len(respones)
P90 time.Duration )
P95 time.Duration mergedResponses := make(map[string][]time.Duration)
P99 time.Duration
}{
Count: len(respones),
Min: respones[0].Time,
Max: respones[0].Time,
}
mergedResponses := make(map[string]Durations)
var allDurations Durations
for _, response := range respones { for _, response := range respones {
if response.Time < total.Min { if response.Time < totalMinDuration {
total.Min = response.Time totalMinDuration = response.Time
} }
if response.Time > total.Max { if response.Time > totalMaxDuration {
total.Max = response.Time totalMaxDuration = response.Time
} }
total.Sum += response.Time totalDuration += response.Time
if response.Error != nil { if response.Error != nil {
mergedResponses[response.Error.Error()] = append( mergedResponses[response.Error.Error()] = append(
@ -56,60 +48,38 @@ func (respones Responses) Print() {
response.Time, response.Time,
) )
} }
allDurations = append(allDurations, response.Time)
} }
allDurations.Sort()
allDurationsLenAsFloat := float64(len(allDurations) - 1)
total.P90 = allDurations[int(0.90*allDurationsLenAsFloat)]
total.P95 = allDurations[int(0.95*allDurationsLenAsFloat)]
total.P99 = allDurations[int(0.99*allDurationsLenAsFloat)]
t := table.NewWriter() t := table.NewWriter()
t.SetOutputMirror(os.Stdout) t.SetOutputMirror(os.Stdout)
t.SetStyle(table.StyleLight) t.SetStyle(table.StyleLight)
t.SetColumnConfigs([]table.ColumnConfig{ t.SetColumnConfigs([]table.ColumnConfig{
{Number: 1, WidthMax: 40}, {Number: 1, WidthMax: 80},
}) })
t.AppendHeader(table.Row{ t.AppendHeader(table.Row{
"Response", "Response",
"Count", "Count",
"Min Time", "Min Time",
"Max Time", "Max Time",
"Average Time", "Average Time",
"P90",
"P95",
"P99",
}) })
for key, durations := range mergedResponses { for key, durations := range mergedResponses {
durations.Sort()
durationsLen := len(durations)
durationsLenAsFloat := float64(durationsLen - 1)
t.AppendRow(table.Row{ t.AppendRow(table.Row{
key, key,
durationsLen, len(durations),
durations.First(), utils.MinDuration(durations...),
durations.Last(), utils.MaxDuration(durations...),
durations.Avg(), utils.AvgDuration(durations...),
durations[int(0.90*durationsLenAsFloat)],
durations[int(0.95*durationsLenAsFloat)],
durations[int(0.99*durationsLenAsFloat)],
}) })
t.AppendSeparator() t.AppendSeparator()
} }
t.AppendRow(table.Row{
if len(mergedResponses) > 1 { "Total",
t.AppendRow(table.Row{ totalCount,
"Total", totalMinDuration,
total.Count, totalMaxDuration,
total.Min, totalDuration / time.Duration(totalCount),
total.Max, })
total.Sum / time.Duration(total.Count), // Average
total.P90,
total.P95,
total.P99,
})
}
t.Render() t.Render()
} }

View File

@ -1,41 +0,0 @@
package types
import (
"sort"
"time"
)
type Durations []time.Duration
func (d Durations) Sort(ascending ...bool) {
// If ascending is provided and is false, sort in descending order
if len(ascending) > 0 && ascending[0] == false {
sort.Slice(d, func(i, j int) bool {
return d[i] > d[j]
})
} else { // Otherwise, sort in ascending order
sort.Slice(d, func(i, j int) bool {
return d[i] < d[j]
})
}
}
func (d Durations) First() *time.Duration {
return &d[0]
}
func (d Durations) Last() *time.Duration {
return &d[len(d)-1]
}
func (d Durations) Sum() time.Duration {
sum := time.Duration(0)
for _, duration := range d {
sum += duration
}
return sum
}
func (d Durations) Avg() time.Duration {
return d.Sum() / time.Duration(len(d))
}

31
utils/time.go Normal file
View File

@ -0,0 +1,31 @@
package utils
import "time"
func MinDuration(durations ...time.Duration) time.Duration {
min := durations[0]
for _, d := range durations {
if d < min {
min = d
}
}
return min
}
func MaxDuration(durations ...time.Duration) time.Duration {
max := durations[0]
for _, d := range durations {
if d > max {
max = d
}
}
return max
}
func AvgDuration(durations ...time.Duration) time.Duration {
total := time.Duration(0)
for _, d := range durations {
total += d
}
return total / time.Duration(len(durations))
}

View File

@ -1,4 +1,4 @@
package types package utils
import ( import (
"encoding/json" "encoding/json"