Skip to content

Commit

Permalink
feat : parse oapi to vegeta
Browse files Browse the repository at this point in the history
  • Loading branch information
seipan committed Sep 20, 2023
1 parent 6afdf30 commit 937095d
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 10 deletions.
26 changes: 18 additions & 8 deletions cmd/attack.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,19 @@ import (
"context"
"encoding/json"
"fmt"
"net/http"
"time"

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

func ParseAndAttack(ctx context.Context, path string) error {
func ParseAndAttack(ctx context.Context, 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)
atks, err := ParthOpenAPItoAttacker(paths, freq, duration)
if err != nil {
return fmt.Errorf("failed to convert openapi to attacker: %w", err)
}
Expand All @@ -46,28 +48,36 @@ func ParseAndAttack(ctx context.Context, path string) error {
return nil
}

func ParthOpenAPItoAttacker(pathes []lib.Path) ([]lib.Attacker, error) {
func ParthOpenAPItoAttacker(pathes []lib.Path, freq int, duration time.Duration) ([]lib.Attacker, error) {
var res []lib.Attacker
for i, path := range pathes {
mtd := path.Method(0)
bodys := mtd.Bodys()
body, err := createBody(bodys)
if err != nil {
return nil, err
}
atk := lib.Attacker{
Path: path,
MethodIndex: i,
Body: body,
Header: http.Header{},
Frequency: freq,
Duration: duration,
}
res = append(res, atk)
}
return res, nil
}

func createBody(bodys []lib.Body) (string, error) {
func createBody(bodys []lib.Body) ([]byte, error) {
mp := make(map[string]interface{})
for _, body := range bodys {
mp[body.Name] = body.Shema.Value.Example
}
jsonData, err := json.Marshal(mp)
if err != nil {
return "", err
return nil, err
}

jsonString := string(jsonData)
return jsonString, nil
return jsonData, nil
}
4 changes: 2 additions & 2 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import (
// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Use: "bluma",
Short: "A brief description of your application",
Short: "CLI tool to parse OpenAPI and stress test each endpoint.",
Long: `A longer description that spans multiple lines and likely contains
examples and usage of using your application. For example:
Expand Down Expand Up @@ -62,6 +62,6 @@ func init() {
// Cobra also supports local flags, which will only run
// when this action is called directly.
rootCmd.Flags().StringP("path", "path", "", "Path for Parsing OpenAPI")
rootCmd.Flags().Int32P("frequency", "frequency", 1, "stress test frequency")
rootCmd.Flags().IntP("frequency", "frequency", 1, "stress test frequency")
rootCmd.Flags().DurationP("duration", "duration", 1, "stress test frequency")
}

0 comments on commit 937095d

Please sign in to comment.