diff options
| author | Shulhan <ms@kilabit.info> | 2021-03-21 04:30:07 +0700 |
|---|---|---|
| committer | Shulhan <ms@kilabit.info> | 2021-03-21 04:30:07 +0700 |
| commit | 05474abb07f73aace79bcf339be8e5619dd4564b (patch) | |
| tree | 6cd71065014161f3f4f95aacb6b0508ce20a216c /example | |
| parent | 77e3074ccbbb01945e06d5608d34d5f39352500e (diff) | |
| download | gorankusu-05474abb07f73aace79bcf339be8e5619dd4564b.tar.xz | |
all: implement API and interface for attack functionality
When the user click "Attack" it will call the API to run the load testing.
If there is load testing currently, it will return with an error.
On success, the result of load testing will be stored on directory
defined in Environment.ResultsDir with file named
<HttpTarget.ID>.<date_time>.<RPS>x<Duration>s.<ResultsSuffix>.bin
This file contains the vegeta.Results.
Diffstat (limited to 'example')
| -rw-r--r-- | example/example.go | 42 |
1 files changed, 36 insertions, 6 deletions
diff --git a/example/example.go b/example/example.go index 44c3b5b..33c30d1 100644 --- a/example/example.go +++ b/example/example.go @@ -10,8 +10,10 @@ import ( "net/http" "time" - "git.sr.ht/~shulhan/trunks" libhttp "github.com/shuLhan/share/lib/http" + vegeta "github.com/tsenart/vegeta/v12/lib" + + "git.sr.ht/~shulhan/trunks" ) const ( @@ -19,7 +21,8 @@ const ( ) type Example struct { - trunks *trunks.Trunks + trunks *trunks.Trunks + targetExampleGet vegeta.Target } // @@ -27,7 +30,7 @@ type Example struct { // func New() (ex *Example, err error) { env := &trunks.Environment{ - ResultsDir: "/tmp", + ResultsDir: "testdata/example/", ResultsSuffix: "_trunks_example", } @@ -76,7 +79,7 @@ func (ex *Example) registerEndpoints() (err error) { func (ex *Example) registerTargets() (err error) { targetHttp := &trunks.Target{ Name: "Example HTTP target", - AttackOpts: &trunks.AttackOptions{ + Opts: &trunks.AttackOptions{ BaseUrl: fmt.Sprintf("http://%s", ex.trunks.Env.ListenAddress), Duration: 5 * time.Second, RatePerSecond: 10, @@ -92,7 +95,10 @@ func (ex *Example) registerTargets() (err error) { Params: trunks.KeyValue{ "Param1": "1", }, - Run: ex.runExampleGet, + Run: ex.runExampleGet, + Attack: ex.attackExampleGet, + PreAttack: ex.preattackExampleGet, + AllowAttack: true, }}, } @@ -112,7 +118,7 @@ func (ex *Example) pathExampleGet(epr *libhttp.EndpointRequest) ([]byte, error) func (ex *Example) runExampleGet(target *trunks.Target, req *trunks.RunRequest) ([]byte, error) { if target.HttpClient == nil { - target.HttpClient = libhttp.NewClient(target.AttackOpts.BaseUrl, nil, true) + target.HttpClient = libhttp.NewClient(target.Opts.BaseUrl, nil, true) } _, resbody, err := target.HttpClient.Get( req.HttpTarget.Path, @@ -123,3 +129,27 @@ func (ex *Example) runExampleGet(target *trunks.Target, req *trunks.RunRequest) } return resbody, nil } + +func (ex *Example) preattackExampleGet(rr *trunks.RunRequest) { + ex.targetExampleGet = vegeta.Target{ + Method: rr.HttpTarget.Method.String(), + URL: fmt.Sprintf("%s%s", rr.Target.Opts.BaseUrl, rr.HttpTarget.Path), + Header: rr.HttpTarget.Headers.ToHttpHeader(), + } + + q := rr.HttpTarget.Params.ToUrlValues().Encode() + if len(q) > 0 { + ex.targetExampleGet.URL += "?" + q + } + + fmt.Printf("preattackExampleGet: %+v\n", ex.targetExampleGet) +} + +func (ex *Example) attackExampleGet(rr *trunks.RunRequest) vegeta.Targeter { + return func(tgt *vegeta.Target) error { + rr.HttpTarget.AttackLocker.Lock() + *tgt = ex.targetExampleGet + rr.HttpTarget.AttackLocker.Unlock() + return nil + } +} |
