Skip to content

Commit

Permalink
Slow requests (#43)
Browse files Browse the repository at this point in the history
  • Loading branch information
ostcar authored Mar 24, 2023
1 parent 996b558 commit 1dfe117
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 0 deletions.
2 changes: 2 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/OpenSlides/openslides-performance/connect"
"github.com/OpenSlides/openslides-performance/createusers"
"github.com/OpenSlides/openslides-performance/request"
"github.com/OpenSlides/openslides-performance/slow"
"github.com/OpenSlides/openslides-performance/vote"
"github.com/OpenSlides/openslides-performance/work"
"github.com/alecthomas/kong"
Expand Down Expand Up @@ -59,6 +60,7 @@ var cli struct {
Connect connect.Options `cmd:"" help:"Opens many connections to autoupdate and keeps them open."`
CreateUsers createusers.Options `cmd:"" help:"Create many users."`
Request request.Options `cmd:"" help:"Sends a logged-in request to OpenSlides."`
Slow slow.Options `cmd:"" help:"Sends many slow requests."`
Vote vote.Options `cmd:"" help:"Sends many votes from different users."`
Work work.Options `cmd:"" help:"Generates background work."`
}
16 changes: 16 additions & 0 deletions slow/options.go
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.`
}
59 changes: 59 additions & 0 deletions slow/slow.go
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)
}

0 comments on commit 1dfe117

Please sign in to comment.