Skip to content

Commit

Permalink
Merge pull request #49 from shubham14bajpai/http2
Browse files Browse the repository at this point in the history
Support HTTP2 option
  • Loading branch information
nakabonne committed Oct 7, 2020
2 parents fd4e04b + b78afb1 commit cccdee3
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 10 deletions.
2 changes: 2 additions & 0 deletions attacker/attacker.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ type Options struct {
MaxWorkers uint64
KeepAlive bool
Connections int
HTTP2 bool

Attacker Attacker
}
Expand Down Expand Up @@ -79,6 +80,7 @@ func Attack(ctx context.Context, target string, resCh chan *Result, metricsCh ch
vegeta.MaxBody(opts.MaxBody),
vegeta.Connections(opts.Connections),
vegeta.KeepAlive(opts.KeepAlive),
vegeta.HTTP2(opts.HTTP2),
)
}

Expand Down
24 changes: 14 additions & 10 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ type cli struct {
workers uint64
maxWorkers uint64
connections int
http2 bool

debug bool
version bool
Expand Down Expand Up @@ -76,6 +77,7 @@ func parseFlags(stdout, stderr io.Writer) (*cli, error) {
flagSet.Uint64VarP(&c.workers, "workers", "w", attacker.DefaultWorkers, "Amount of initial workers to spawn.")
flagSet.Uint64VarP(&c.maxWorkers, "max-workers", "W", attacker.DefaultMaxWorkers, "Amount of maximum workers to spawn.")
flagSet.IntVarP(&c.connections, "connections", "c", attacker.DefaultConnections, "Amount of maximum open idle connections per target host")
flagSet.BoolVar(&c.http2, "http2", true, "Issue HTTP/2 requests to servers which support it. (default true)")
flagSet.Usage = c.usage
if err := flagSet.Parse(os.Args[1:]); err != nil {
if !errors.Is(err, flag.ErrHelp) {
Expand Down Expand Up @@ -168,16 +170,18 @@ func (c *cli) makeOptions() (*attacker.Options, error) {
}

return &attacker.Options{
Rate: c.rate,
Duration: c.duration,
Timeout: c.timeout,
Method: c.method,
Body: body,
MaxBody: c.maxBody,
Header: header,
KeepAlive: c.keepAlive,
Workers: c.workers,
MaxWorkers: c.maxWorkers,
Rate: c.rate,
Duration: c.duration,
Timeout: c.timeout,
Method: c.method,
Body: body,
MaxBody: c.maxBody,
Header: header,
KeepAlive: c.keepAlive,
Workers: c.workers,
MaxWorkers: c.maxWorkers,
Connections: c.connections,
HTTP2: c.http2,
}, nil
}

Expand Down
26 changes: 26 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ func TestParseFlags(t *testing.T) {
connections: 10000,
stdout: new(bytes.Buffer),
stderr: new(bytes.Buffer),
http2: true,
},
wantErr: false,
},
Expand Down Expand Up @@ -199,6 +200,31 @@ func TestMakeOptions(t *testing.T) {
want: nil,
wantErr: true,
},
{
name: "http2 given as false",
cli: &cli{
method: "GET",
headers: []string{"key:value"},
body: "",
bodyFile: "testdata/body-1.json",
http2: false,
},
want: &attacker.Options{
Rate: 0,
Duration: 0,
Timeout: 0,
Method: "GET",
Body: []byte(`{"foo": 1}`),
Header: http.Header{
"key": []string{"value"},
},
Workers: 0,
MaxWorkers: 0,
MaxBody: 0,
HTTP2: false,
},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down

0 comments on commit cccdee3

Please sign in to comment.