diff options
| author | Shulhan <ms@kilabit.info> | 2021-03-12 02:11:11 +0700 |
|---|---|---|
| committer | Shulhan <ms@kilabit.info> | 2021-03-15 01:52:45 +0700 |
| commit | d30bfb7fa1c32984eaba8ad1cf744328812e02b9 (patch) | |
| tree | 316e089ac24694a3793ff92475be531ccd444b74 /attack_options.go | |
| download | gorankusu-d30bfb7fa1c32984eaba8ad1cf744328812e02b9.tar.xz | |
trunks: a module for testing HTTP services
Trunks is a library and HTTP service that provide web user interface
to test HTTP service, similar to Postman, and for load testing.
For the load testing we use vegeta [1] as the backend.
[1] https://github.com/tsenart/vegeta
Diffstat (limited to 'attack_options.go')
| -rw-r--r-- | attack_options.go | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/attack_options.go b/attack_options.go new file mode 100644 index 0000000..4d01f88 --- /dev/null +++ b/attack_options.go @@ -0,0 +1,61 @@ +// Copyright 2021, Shulhan <ms@kilabit.info>. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package trunks + +import ( + "fmt" + "time" + + vegeta "github.com/tsenart/vegeta/lib" +) + +type AttackOptions struct { + // BaseUrl contains the target address that serve the service to + // be tested. + // This field is required. + BaseUrl string `ini:"trunks:attack:base_url"` + + // Duration define the duration for each attack to be executed, + // in seconds. + // For example, if the AttackRate is 500 and AttackDuration is 10 + // seconds, the total of request for each target will be 5000 + // requests. + // This field is optional, default to DefaultAttackDuration if its + // zero. + Duration time.Duration `ini:"trunks:attack:duration"` + + // RatePerSecond define the number of request per second. + // This field is optional, default to DefaultAttackRatePerSecond if + // its zero. + RatePerSecond int `ini:"trunks:attack:rate_per_second"` + ratePerSecond vegeta.Rate + + // Timeout define the overall time to run the attack on each target. + // This field is optional, default to DefaultAttackTimeout if its + // zero. + Timeout time.Duration `ini:"trunks:attack:timeout"` +} + +func (attackOpts *AttackOptions) init() (err error) { + if len(attackOpts.BaseUrl) == 0 { + return fmt.Errorf("AttackOptions.BaseUrl is not defined") + } + + if attackOpts.Timeout == 0 { + attackOpts.Timeout = DefaultAttackTimeout + } + + if attackOpts.RatePerSecond == 0 { + attackOpts.RatePerSecond = DefaultAttackRatePerSecond + } + attackOpts.ratePerSecond = vegeta.Rate{ + Freq: attackOpts.RatePerSecond, + Per: time.Second, + } + if attackOpts.Duration == 0 { + attackOpts.Duration = DefaultAttackDuration + } + return nil +} |
