Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support HTTP2 option #49

Merged
merged 2 commits into from
Oct 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice catch!

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