From 81f5617bdbb6489aedb50120b4b653fb49d63c1a Mon Sep 17 00:00:00 2001 From: Shulhan Date: Fri, 10 Dec 2021 15:04:11 +0700 Subject: all: realign all structs Changes, * AttackResult storage size decreased -24 bytes * Environment storage size decreased -32 bytes * HttpTarget storage size decreased -24 bytes * RunRequest storage size decreased -32 bytes * RunResponse storage size decreased -8 bytes * Target storage size decreased -16 bytes * WebSocketTarget storage size decreased -8 bytes --- attack_result.go | 19 ++++++++++--------- environment.go | 46 ++++++++++++++++++++++++---------------------- http_target.go | 31 +++++++++++++++---------------- run_request.go | 8 +++++--- run_response.go | 12 +++++++----- target.go | 14 ++++++++------ trunks.go | 7 ++++--- websocket_target.go | 15 +++++++-------- 8 files changed, 80 insertions(+), 72 deletions(-) diff --git a/attack_result.go b/attack_result.go index f2cc132..544e886 100644 --- a/attack_result.go +++ b/attack_result.go @@ -30,19 +30,20 @@ const ( // AttackResult represent the output from load testing. // type AttackResult struct { - mtx sync.Mutex + fout *os.File + encoder vegeta.Encoder + metrics *vegeta.Metrics + hist *vegeta.Histogram TargetID string // ID of Target. HttpTargetID string // ID of HTTP target which own the result. Name string // Name of output file without path. - TextReport []byte // TextReport the result reported as text. - HistReport []byte // HistReport the result reported as histogram text. - - fullpath string - fout *os.File - encoder vegeta.Encoder - metrics *vegeta.Metrics - hist *vegeta.Histogram + fullpath string + + TextReport []byte // TextReport the result reported as text. + HistReport []byte // HistReport the result reported as histogram text. + + mtx sync.Mutex } // diff --git a/environment.go b/environment.go index 1c23109..6b7815c 100644 --- a/environment.go +++ b/environment.go @@ -16,24 +16,32 @@ import ( // Environment contains global configuration for load testing. // type Environment struct { + // AttackRunning will be set to non-nil if there is a load + // testing currently running. + AttackRunning *RunRequest + // ListenAddress is the address and port where Trunks HTTP web // will run. // If its emtpy, it will set to DefaultListenAddress. ListenAddress string `ini:"trunks::listen_address"` + websocketListenAddress string + + // ResultsDir is the path where the output of load testing will be + // stored. + // This field is optional, if its empty it will be set to the working + // directory where the program is running. + ResultsDir string `ini:"trunks:results:dir"` + + // ResultsSuffix define custom string to add to the file name to + // uniquely identify results on each run. + ResultsSuffix string `ini:"trunks:result:suffix"` + // WebSocketListenPort is the port number where Trunks WebSocket API // will run. // If its empty, it will set to DefaultWebSocketListenPort. WebSocketListenPort int - // MaxAttackDuration define the maximum duration for an attack to be - // run on each target. - // The purpose of this option is to prevent client to attack service - // and bringing it down. - // This field is optional, default to DefaultMaxAttackDuration if its - // zero. - MaxAttackDuration time.Duration `ini:"trunks::max_attack_duration"` - // MaxAttackRate define the maximum AttackRate can be set by client. // The purpose of this option is to prevent client to set attack rate // which may bring down the service to be tested. @@ -41,21 +49,15 @@ type Environment struct { // zero. MaxAttackRate int `ini:"trunks::max_attack_rate"` - // ResultsDir is the path where the output of load testing will be - // stored. - // This field is optional, if its empty it will be set to the working - // directory where the program is running. - ResultsDir string `ini:"trunks:results:dir"` - - // ResultsSuffix define custom string to add to the file name to - // uniquely identify results on each run. - ResultsSuffix string `ini:"trunks:result:suffix"` + // MaxAttackDuration define the maximum duration for an attack to be + // run on each target. + // The purpose of this option is to prevent client to attack service + // and bringing it down. + // This field is optional, default to DefaultMaxAttackDuration if its + // zero. + MaxAttackDuration time.Duration `ini:"trunks::max_attack_duration"` - // AttackRunning will be set to non-nil if there is a load - // testing currently running. - AttackRunning *RunRequest - mtx sync.Mutex - websocketListenAddress string + mtx sync.Mutex } func (env *Environment) init() (err error) { diff --git a/http_target.go b/http_target.go index b648072..1b0d6e8 100644 --- a/http_target.go +++ b/http_target.go @@ -39,29 +39,28 @@ type HttpAttackHandler func(rr *RunRequest) vegeta.Targeter type HttpPreAttackHandler func(rr *RunRequest) type HttpTarget struct { - // Name of target, required. - Name string - Hint string // Description about what this HTTP target is doing. + Params KeyFormInput + ConvertParams HttpConvertParams `json:"-"` + + Headers KeyFormInput + + Run HttpRunHandler `json:"-"` + PreAttack HttpPreAttackHandler `json:"-"` + Attack HttpAttackHandler `json:"-"` // ID of target, optional. // If its empty, it will generated using value from Name. ID string - Method libhttp.RequestMethod - Path string - RequestType libhttp.RequestType - Headers KeyFormInput - Params KeyFormInput - - Run HttpRunHandler `json:"-"` - ConvertParams HttpConvertParams `json:"-"` + Name string // Name of target, required. + Hint string // Description about what this HTTP target is doing. + Path string - Attack HttpAttackHandler `json:"-"` - PreAttack HttpPreAttackHandler `json:"-"` - AttackLocker sync.Mutex `json:"-"` // Use this inside the Attack to lock resource. + Results []*AttackResult // Results contains list of load testing output. + RequestType libhttp.RequestType + Method libhttp.RequestMethod - // Results contains list of load testing output. - Results []*AttackResult + AttackLocker sync.Mutex `json:"-"` // Use this inside the Attack to lock resource. // AllowAttack if its true the "Attack" button will be showed on user // interface and client will be allowed to run load testing on this diff --git a/run_request.go b/run_request.go index 174781d..78c7b12 100644 --- a/run_request.go +++ b/run_request.go @@ -16,11 +16,13 @@ import ( // RunRequest define the request to run HTTP or WebSocket target. // type RunRequest struct { - Locker sync.Mutex `json:"-"` + result *AttackResult + Target Target - HttpTarget HttpTarget WebSocketTarget WebSocketTarget - result *AttackResult + HttpTarget HttpTarget + + Locker sync.Mutex `json:"-"` } // diff --git a/run_response.go b/run_response.go index c916801..aeeda2c 100644 --- a/run_response.go +++ b/run_response.go @@ -18,12 +18,14 @@ import ( // WebSocket target. // type RunResponse struct { - DumpRequest []byte - DumpResponse []byte - ResponseStatus string + ResponseStatus string + ResponseType string + + DumpRequest []byte + DumpResponse []byte + ResponseBody []byte + ResponseStatusCode int - ResponseType string - ResponseBody []byte } // diff --git a/target.go b/target.go index da4382c..3cb095e 100644 --- a/target.go +++ b/target.go @@ -14,22 +14,24 @@ import ( // Target contains group of HttpTarget that can be tested by Trunks. // type Target struct { + // HttpClient that can be used for running HttpTarget. + HttpClient *libhttp.Client `json:"-"` + + Opts *AttackOptions + Vars KeyFormInput + ID string Name string - Hint string // BaseUrl contains the target address that serve the service to // be tested. // This field is required. BaseUrl string - Opts *AttackOptions - Vars KeyFormInput + Hint string + HttpTargets []*HttpTarget WebSocketTargets []*WebSocketTarget - - // HttpClient that can be used for running HttpTarget. - HttpClient *libhttp.Client `json:"-"` } func (target *Target) init() (err error) { diff --git a/trunks.go b/trunks.go index 2e5bcd5..34886c1 100644 --- a/trunks.go +++ b/trunks.go @@ -47,13 +47,14 @@ const ( // type Trunks struct { Env *Environment - Httpd *libhttp.Server // The HTTP server. - Wsd *websocket.Server // The WebSocket server. + Httpd *libhttp.Server + Wsd *websocket.Server - targets []*Target attackq chan *RunRequest cancelq chan bool errq chan error + + targets []*Target } // diff --git a/websocket_target.go b/websocket_target.go index 394d483..6bf68e5 100644 --- a/websocket_target.go +++ b/websocket_target.go @@ -16,14 +16,6 @@ type WebSocketRunHandler func(rr *RunRequest) (interface{}, error) // WebSocketTarget define the target to test WebSocket service. // type WebSocketTarget struct { - // Name of target, required. - Name string - Hint string // Description about what this WebSocket target is doing. - - // ID of target, optional. - // If its empty, it will generated by normalized the value of Name. - ID string - Headers KeyFormInput // Params contains additional parameters to be passed when running @@ -35,6 +27,13 @@ type WebSocketTarget struct { Params KeyFormInput Run WebSocketRunHandler `json:"-"` + + // ID of target, optional. + // If its empty, it will generated by normalized the value of Name. + ID string + + Name string // Name of target, required. + Hint string // Description about what this WebSocket target is doing. } func (wst *WebSocketTarget) init() (err error) { -- cgit v1.3