-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
77 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package slow | ||
|
||
import ( | ||
"net/url" | ||
) | ||
|
||
// Options is the meta information for the cli. | ||
type Options struct { | ||
URL *url.URL `arg:"" help:"URL for the request"` | ||
Amount int `help:"Amount of user to be created." short:"n" default:"10"` | ||
} | ||
|
||
// Help returns the help message | ||
func (o Options) Help() string { | ||
return `Sends many slow request with an endless body.` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package slow | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"io" | ||
"math/rand" | ||
"net/http" | ||
"time" | ||
|
||
"github.com/OpenSlides/openslides-performance/client" | ||
"golang.org/x/sync/errgroup" | ||
) | ||
|
||
// Run sends the request. | ||
func (o Options) Run(ctx context.Context, cfg client.Config) error { | ||
c, err := client.New(cfg) | ||
if err != nil { | ||
return fmt.Errorf("creating client: %w", err) | ||
} | ||
|
||
eg, ctx := errgroup.WithContext(ctx) | ||
|
||
for i := 0; i < o.Amount; i++ { | ||
eg.Go(func() error { | ||
for ctx.Err() == nil { | ||
req, err := http.NewRequestWithContext(ctx, "POST", o.URL.String(), slowRandromReader{}) | ||
if err != nil { | ||
return fmt.Errorf("creating request: %w", err) | ||
} | ||
|
||
resp, err := c.Do(req) | ||
if err != nil { | ||
return fmt.Errorf("sending request: %w", err) | ||
} | ||
defer resp.Body.Close() | ||
|
||
if _, err := io.ReadAll(resp.Body); err != nil { | ||
return fmt.Errorf("draining body: %w", err) | ||
} | ||
} | ||
|
||
return nil | ||
}) | ||
} | ||
|
||
return eg.Wait() | ||
} | ||
|
||
// slowRandomReader is a reader that reads pseudo random data very slow | ||
type slowRandromReader struct{} | ||
|
||
func (s slowRandromReader) Read(p []byte) (int, error) { | ||
if len(p) > 100 { | ||
p = p[:100] | ||
} | ||
time.Sleep(time.Second) | ||
return rand.Read(p) | ||
} |