1 Commits

Author SHA1 Message Date
6e4a676d43 Merge c69c35bef4 into e3cbe1e9b4 2024-11-23 14:16:15 +00:00
8 changed files with 58 additions and 115 deletions

View File

@ -1,6 +1,6 @@
<h1 align="center">Dodo is a simple and easy-to-use HTTP benchmarking tool.</h1> <h1 align="center">Dodo is a simple and easy-to-use HTTP benchmarking tool.</h1>
<p align="center"> <p align="center">
<img width="30%" height="30%" src="https://ftp.aykhans.me/web/client/pubshares/hB6VSdCnBCr8gFPeiMuCji/browse?path=%2Fdodo.png"> <img width="30%" height="30%" src="https://raw.githubusercontent.com/aykhans/dodo/main/assets/dodo.png">
</p> </p>
## Installation ## Installation

BIN
assets/dodo.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 230 KiB

View File

@ -1,7 +1,7 @@
{ {
"method": "GET", "method": "GET",
"url": "https://example.com", "url": "https://example.com",
"no_proxy_check": true, "no_proxy_check": false,
"timeout": 10000, "timeout": 10000,
"dodos_count": 50, "dodos_count": 50,
"request_count": 1000, "request_count": 1000,

View File

@ -21,6 +21,10 @@ const (
MaxDodosCountForProxies uint = 20 // Max dodos count for proxy check MaxDodosCountForProxies uint = 20 // Max dodos count for proxy check
) )
type IConfig interface {
MergeConfigs(newConfig IConfig) IConfig
}
type RequestConfig struct { type RequestConfig struct {
Method string Method string
URL *url.URL URL *url.URL
@ -44,12 +48,6 @@ func (config *RequestConfig) Print() {
{Number: 2, WidthMax: 50}, {Number: 2, WidthMax: 50},
}) })
newHeaders := make(map[string][]string)
newHeaders["User-Agent"] = []string{DefaultUserAgent}
for k, v := range config.Headers {
newHeaders[k] = v
}
t.AppendHeader(table.Row{"Request Configuration"}) t.AppendHeader(table.Row{"Request Configuration"})
t.AppendRow(table.Row{"Method", config.Method}) t.AppendRow(table.Row{"Method", config.Method})
t.AppendSeparator() t.AppendSeparator()
@ -59,11 +57,11 @@ func (config *RequestConfig) Print() {
t.AppendSeparator() t.AppendSeparator()
t.AppendRow(table.Row{"Dodos", config.DodosCount}) t.AppendRow(table.Row{"Dodos", config.DodosCount})
t.AppendSeparator() t.AppendSeparator()
t.AppendRow(table.Row{"Requests", config.RequestCount}) t.AppendRow(table.Row{"Request", config.RequestCount})
t.AppendSeparator() t.AppendSeparator()
t.AppendRow(table.Row{"Params", utils.MarshalJSON(config.Params, 3)}) t.AppendRow(table.Row{"Params", utils.MarshalJSON(config.Params, 3)})
t.AppendSeparator() t.AppendSeparator()
t.AppendRow(table.Row{"Headers", utils.MarshalJSON(newHeaders, 3)}) t.AppendRow(table.Row{"Headers", utils.MarshalJSON(config.Headers, 3)})
t.AppendSeparator() t.AppendSeparator()
t.AppendRow(table.Row{"Cookies", utils.MarshalJSON(config.Cookies, 3)}) t.AppendRow(table.Row{"Cookies", utils.MarshalJSON(config.Cookies, 3)})
t.AppendSeparator() t.AppendSeparator()
@ -97,27 +95,7 @@ type Config struct {
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 utils.Option[bool] `json:"no_proxy_check"` NoProxyCheck utils.IOption[bool] `json:"no_proxy_check"`
}
func NewConfig(
method string,
timeout uint32,
dodosCount uint,
requestCount uint,
noProxyCheck utils.Option[bool],
) *Config {
if noProxyCheck == nil {
noProxyCheck = utils.NewNoneOption[bool]()
}
return &Config{
Method: method,
Timeout: timeout,
DodosCount: dodosCount,
RequestCount: requestCount,
NoProxyCheck: noProxyCheck,
}
} }
func (config *Config) MergeConfigs(newConfig *Config) { func (config *Config) MergeConfigs(newConfig *Config) {
@ -166,7 +144,7 @@ type Proxy struct {
} }
type JSONConfig struct { type JSONConfig struct {
*Config Config
Params map[string][]string `json:"params"` Params map[string][]string `json:"params"`
Headers map[string][]string `json:"headers"` Headers map[string][]string `json:"headers"`
Cookies map[string][]string `json:"cookies"` Cookies map[string][]string `json:"cookies"`
@ -174,21 +152,8 @@ type JSONConfig struct {
Body []string `json:"body"` Body []string `json:"body"`
} }
func NewJSONConfig(
config *Config,
params map[string][]string,
headers map[string][]string,
cookies map[string][]string,
proxies []Proxy,
body []string,
) *JSONConfig {
return &JSONConfig{
config, params, headers, cookies, proxies, body,
}
}
func (config *JSONConfig) MergeConfigs(newConfig *JSONConfig) { func (config *JSONConfig) MergeConfigs(newConfig *JSONConfig) {
config.Config.MergeConfigs(newConfig.Config) config.Config.MergeConfigs(&newConfig.Config)
if len(newConfig.Params) != 0 { if len(newConfig.Params) != 0 {
config.Params = newConfig.Params config.Params = newConfig.Params
} }
@ -207,23 +172,13 @@ func (config *JSONConfig) MergeConfigs(newConfig *JSONConfig) {
} }
type CLIConfig struct { type CLIConfig struct {
*Config Config
Yes bool `json:"yes" validate:"omitempty"` Yes bool `json:"yes" validate:"omitempty"`
ConfigFile string `validation_name:"config-file" validate:"omitempty,filepath"` ConfigFile string `validation_name:"config-file" validate:"omitempty,filepath"`
} }
func NewCLIConfig(
config *Config,
yes bool,
configFile string,
) *CLIConfig {
return &CLIConfig{
config, yes, configFile,
}
}
func (config *CLIConfig) MergeConfigs(newConfig *CLIConfig) { func (config *CLIConfig) MergeConfigs(newConfig *CLIConfig) {
config.Config.MergeConfigs(newConfig.Config) config.Config.MergeConfigs(&newConfig.Config)
if newConfig.ConfigFile != "" { if newConfig.ConfigFile != "" {
config.ConfigFile = newConfig.ConfigFile config.ConfigFile = newConfig.ConfigFile
} }

14
main.go
View File

@ -21,10 +21,8 @@ import (
func main() { func main() {
validator := validation.NewValidator() validator := validation.NewValidator()
conf := config.NewConfig("", 0, 0, 0, nil) conf := config.Config{}
jsonConf := config.NewJSONConfig( jsonConf := config.JSONConfig{}
config.NewConfig("", 0, 0, 0, nil), nil, nil, nil, nil, nil,
)
cliConf, err := readers.CLIConfigReader() cliConf, err := readers.CLIConfigReader()
if err != nil || cliConf == nil { if err != nil || cliConf == nil {
@ -53,11 +51,11 @@ func main() {
), ),
) )
} }
jsonConf = jsonConfNew jsonConf = *jsonConfNew
conf.MergeConfigs(jsonConf.Config) conf.MergeConfigs(&jsonConf.Config)
} }
conf.MergeConfigs(cliConf.Config) conf.MergeConfigs(&cliConf.Config)
conf.SetDefaults() conf.SetDefaults()
if err := validator.Struct(conf); err != nil { if err := validator.Struct(conf); err != nil {
utils.PrintErrAndExit( utils.PrintErrAndExit(
@ -83,7 +81,7 @@ func main() {
Proxies: jsonConf.Proxies, Proxies: jsonConf.Proxies,
Body: jsonConf.Body, Body: jsonConf.Body,
Yes: cliConf.Yes, Yes: cliConf.Yes,
NoProxyCheck: conf.NoProxyCheck.ValueOrPanic(), NoProxyCheck: *conf.NoProxyCheck.ValueOrPanic(),
} }
requestConf.Print() requestConf.Print()
if !cliConf.Yes { if !cliConf.Yes {

View File

@ -13,7 +13,11 @@ import (
func CLIConfigReader() (*config.CLIConfig, error) { func CLIConfigReader() (*config.CLIConfig, error) {
var ( var (
returnNil = false returnNil = false
cliConfig = config.NewCLIConfig(config.NewConfig("", 0, 0, 0, nil), false, "") cliConfig = &config.CLIConfig{
Config: config.Config{
NoProxyCheck: utils.NewNoneOption[bool](),
},
}
dodosCount uint dodosCount uint
requestCount uint requestCount uint
timeout uint32 timeout uint32

View File

@ -6,6 +6,7 @@ import (
"github.com/aykhans/dodo/config" "github.com/aykhans/dodo/config"
customerrors "github.com/aykhans/dodo/custom_errors" customerrors "github.com/aykhans/dodo/custom_errors"
"github.com/aykhans/dodo/utils"
) )
func JSONConfigReader(filePath string) (*config.JSONConfig, error) { func JSONConfigReader(filePath string) (*config.JSONConfig, error) {
@ -13,10 +14,11 @@ func JSONConfigReader(filePath string) (*config.JSONConfig, error) {
if err != nil { if err != nil {
return nil, customerrors.OSErrorFormater(err) return nil, customerrors.OSErrorFormater(err)
} }
jsonConf := config.NewJSONConfig( jsonConf := &config.JSONConfig{
config.NewConfig("", 0, 0, 0, nil), Config: config.Config{
nil, nil, nil, nil, nil, NoProxyCheck: utils.NewNoneOption[bool](),
) },
}
err = json.Unmarshal(data, &jsonConf) err = json.Unmarshal(data, &jsonConf)
if err != nil { if err != nil {

View File

@ -5,20 +5,20 @@ import (
"errors" "errors"
) )
type NonNilT interface { type NonNilConcrete interface {
~int | ~float64 | ~string | ~bool ~int | ~float64 | ~string | ~bool
} }
type Option[T NonNilT] interface { type IOption[T NonNilConcrete] interface {
IsNone() bool IsNone() bool
ValueOrErr() (T, error) ValueOrErr() (*T, error)
ValueOr(def T) T ValueOr(def *T) *T
ValueOrPanic() T ValueOrPanic() *T
UnmarshalJSON(data []byte) error UnmarshalJSON(data []byte) error
} }
// Don't call this struct directly, use NewOption[T] or NewNoneOption[T] instead. // Don't call this struct directly, use NewOption[T] or NewNoneOption[T] instead.
type option[T NonNilT] struct { type option[T NonNilConcrete] struct {
// value holds the actual value of the Option if it is not None. // value holds the actual value of the Option if it is not None.
value T value T
// none indicates whether the Option is None (i.e., has no value). // none indicates whether the Option is None (i.e., has no value).
@ -29,59 +29,43 @@ func (o *option[T]) IsNone() bool {
return o.none return o.none
} }
// If the Option is None, it will return zero value of the type and an error. // The returned value can be nil, if the Option is None, it will return nil and an error.
func (o *option[T]) ValueOrErr() (T, error) { func (o *option[T]) ValueOrErr() (*T, error) {
if o.IsNone() { if o.IsNone() {
return o.value, errors.New("Option is None") return nil, errors.New("Option is None")
} }
return o.value, nil return &o.value, nil
} }
// If the Option is None, it will return the default value. // The returned value can't be nil, if the Option is None, it will return the default value.
func (o *option[T]) ValueOr(def T) T { func (o *option[T]) ValueOr(def *T) *T {
if o.IsNone() { if o.IsNone() {
return def return def
} }
return o.value return &o.value
} }
// If the Option is None, it will panic. // The returned value can't be nil, if the Option is None, it will panic.
func (o *option[T]) ValueOrPanic() T { func (o *option[T]) ValueOrPanic() *T {
if o.IsNone() { if o.IsNone() {
panic("Option is None") panic("Option is None")
} }
return o.value return &o.value
}
func (o *option[T]) SetValue(value T) {
o.value = value
o.none = false
}
func (o *option[T]) SetNone() {
var zeroValue T
o.value = zeroValue
o.none = true
} }
func (o *option[T]) UnmarshalJSON(data []byte) error { func (o *option[T]) UnmarshalJSON(data []byte) error {
if string(data) == "null" || len(data) == 0 { if string(data) == "null" {
o.SetNone() o.none = true
return nil return nil
} }
if err := json.Unmarshal(data, &o.value); err != nil {
o.SetNone()
return err
}
o.none = false o.none = false
return nil return json.Unmarshal(data, &o.value)
} }
func NewOption[T NonNilT](value T) *option[T] { func NewOption[T NonNilConcrete](value T) *option[T] {
return &option[T]{value: value} return &option[T]{value: value}
} }
func NewNoneOption[T NonNilT]() *option[T] { func NewNoneOption[T NonNilConcrete]() *option[T] {
return &option[T]{none: true} return &option[T]{none: true}
} }