Skip to content

Commit

Permalink
feat : parse and attack
Browse files Browse the repository at this point in the history
  • Loading branch information
seipan committed Sep 13, 2023
1 parent 2a6788c commit 562404f
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 16 deletions.
43 changes: 43 additions & 0 deletions cmd/attack.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package cmd

import (
"context"
"fmt"

"github.com/seipan/bluma/lib"
)

func ParseAndAttack(ctx context.Context, path string) error {
oapi := lib.NewOpenAPI(path)
paths, err := oapi.Parse(ctx)
if err != nil {
return fmt.Errorf("failed to parse openapi: %w", err)
}
atks, err := ParthOpenAPItoAttacker(paths)
if err != nil {
return fmt.Errorf("failed to convert openapi to attacker: %w", err)
}
for _, atk := range atks {
atk.Attack()
}
return nil
}

func ParthOpenAPItoAttacker(pathes []lib.Path) ([]lib.Attacker, error) {
var res []lib.Attacker
for i, path := range pathes {
mtd := path.Method(i)
body := mtd.Bodys()
bodyjson, err := body[0].MarshalJSON()
if err != nil {
return nil, fmt.Errorf("failed to marshal json: %w", err)
}
atk := lib.Attacker{
Path: path,
MethodIndex: i,
Body: bodyjson,
}
res = append(res, atk)
}
return res, nil
}
12 changes: 6 additions & 6 deletions lib/attack.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,21 @@ import (

func (atk *Attacker) Attack() {
target := vegeta.Target{
Method: atk.path.method[atk.methodIndex].method,
URL: atk.path.path,
Body: atk.body,
Header: atk.header,
Method: atk.Path.method[atk.MethodIndex].method,
URL: atk.Path.path,
Body: atk.Body,
Header: atk.Header,
}
targeter := vegeta.NewStaticTargeter(target)
rate := vegeta.Rate{Freq: atk.frequency, Per: time.Second}
rate := vegeta.Rate{Freq: atk.Frequency, Per: time.Second}

attacker := vegeta.NewAttacker()

var metrics vegeta.Metrics
var results vegeta.Results
reporter := vegeta.NewJSONReporter(&metrics)

for res := range attacker.Attack(targeter, rate, atk.duration, "Vegeta Load Testing") {
for res := range attacker.Attack(targeter, rate, atk.Duration, "Vegeta Load Testing") {
results.Add(res)
}
results.Close()
Expand Down
8 changes: 4 additions & 4 deletions lib/attack_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,17 @@ func TestAttack(t *testing.T) {
t.Run("Attack", func(t *testing.T) {
t.Skip()
atk := Attacker{
path: Path{
Path: Path{
path: "http://localhost:8080/health",
method: []Method{
{
method: "GET",
},
},
},
methodIndex: 0,
frequency: 10,
duration: 1,
MethodIndex: 0,
Frequency: 10,
Duration: 1,
}
assert.NotPanics(t, atk.Attack)
})
Expand Down
12 changes: 6 additions & 6 deletions lib/vegeta.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import (
)

type Attacker struct {
path Path
methodIndex int
body []byte
header http.Header
frequency int
duration time.Duration
Path Path
MethodIndex int
Body []byte
Header http.Header
Frequency int
Duration time.Duration
}

0 comments on commit 562404f

Please sign in to comment.