Skip to content

Commit

Permalink
Merge pull request #29 from seipan/feat/file-ignore-option
Browse files Browse the repository at this point in the history
feat(option) : Add ignore option
  • Loading branch information
seipan committed Jun 23, 2024
2 parents 120814a + c5272d8 commit 32abd98
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 4 deletions.
13 changes: 10 additions & 3 deletions cmd/attack.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,16 @@ import (
vegeta "github.com/tsenart/vegeta/lib"
)

func ParseAndAttack(ctx context.Context, beseEndpoint string, path string, freq int, duration time.Duration) error {
func ParseAndAttack(ctx context.Context, ignore []string, beseEndpoint string, path string, freq int, duration time.Duration) 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, beseEndpoint, freq, duration)

ignores := lib.NewIgnore(ignore)

atks, err := ParthOpenAPItoAttacker(paths, ignores, beseEndpoint, freq, duration)
if err != nil {
return fmt.Errorf("failed to convert openapi to attacker: %w", err)
}
Expand All @@ -52,9 +55,13 @@ func ParseAndAttack(ctx context.Context, beseEndpoint string, path string, freq
return nil
}

func ParthOpenAPItoAttacker(pathes []lib.Path, beseEndpoint string, freq int, duration time.Duration) ([]lib.Attacker, error) {
func ParthOpenAPItoAttacker(pathes []lib.Path, ignores lib.Ignore, beseEndpoint string, freq int, duration time.Duration) ([]lib.Attacker, error) {
var res []lib.Attacker
for i, path := range pathes {
if ignores.IsIgnored(path.Path()) {
continue
}

mtd := path.Method(0)
bodys := mtd.Bodys()
body, err := createBody(bodys)
Expand Down
7 changes: 6 additions & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,11 @@ var rootCmd = &cobra.Command{
if err != nil {
log.Println(err)
}
err = ParseAndAttack(cmd.Context(), base, path, freq, duration)
ignore, err := cmd.Flags().GetStringArray("ignore")
if err != nil {
log.Println(err)
}
err = ParseAndAttack(cmd.Context(), ignore, base, path, freq, duration)
if err != nil {
log.Println(err)
}
Expand All @@ -67,6 +71,7 @@ func Execute() {
func init() {
rootCmd.Flags().StringP("path", "p", "", "FilePath for Parsing OpenAPI")
rootCmd.Flags().StringP("base", "b", "", "BaseURL for stress test")
rootCmd.Flags().StringArrayP("ignore", "i", []string{}, "Ignore Paths for stress test")
rootCmd.Flags().IntP("frequency", "f", 1, "stress test frequency")
rootCmd.Flags().DurationP("duration", "d", 1, "stress test duration")
}
24 changes: 24 additions & 0 deletions lib/ignore.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package lib

type Ignore struct {
Paths []string
}

func (i *Ignore) IsIgnored(path string) bool {
for _, p := range i.Paths {
if p == path {
return true
}
}
return false
}

func (i *Ignore) Add(path string) {
i.Paths = append(i.Paths, path)
}

func NewIgnore(paths []string) Ignore {
return Ignore{
Paths: paths,
}
}
1 change: 1 addition & 0 deletions lib/ignore_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package lib

0 comments on commit 32abd98

Please sign in to comment.