2021-02-25 08:40:04 +01:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/OpenSlides/OpenSlides/performance/client"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"github.com/vbauerster/mpb"
|
|
|
|
)
|
|
|
|
|
|
|
|
const browserHelp = `Browser simulates that a user opens a browser tab.
|
|
|
|
|
|
|
|
First, a login request is send to teh server. After the user is authenticated,
|
|
|
|
it sends 4 requests to the backend at the same time:
|
|
|
|
* whoami
|
|
|
|
* a get request to login
|
|
|
|
* constants
|
|
|
|
* servertime
|
|
|
|
|
|
|
|
This command tests only the python backend. The requests to the autoupdate-service
|
|
|
|
are skipped for this test. Use the "connect" command to test the autoupdate-service.`
|
|
|
|
|
|
|
|
const (
|
|
|
|
pathLogin = "/apps/users/login/"
|
|
|
|
pathWhoami = "/apps/users/whoami/"
|
|
|
|
pathServertime = "/apps/core/servertime/"
|
|
|
|
pathConstants = "/apps/core/constants/"
|
|
|
|
)
|
|
|
|
|
|
|
|
func cmdBrowser(cfg *config) *cobra.Command {
|
|
|
|
var clientCount int
|
|
|
|
|
|
|
|
cmd := &cobra.Command{
|
|
|
|
Use: "browser",
|
|
|
|
Short: "Simulates that a user logs in and opens the browser",
|
|
|
|
Long: browserHelp,
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
|
|
log.Printf("using %d clients to %s", clientCount, cfg.domain)
|
|
|
|
|
|
|
|
wg := new(sync.WaitGroup)
|
|
|
|
progress := mpb.New(mpb.WithWaitGroup(wg))
|
|
|
|
bar := progress.AddBar(int64(clientCount) * 5)
|
|
|
|
|
|
|
|
clients := make([]*client.Client, clientCount)
|
|
|
|
for i := range clients {
|
2021-05-07 15:43:43 +02:00
|
|
|
c, err := client.New(cfg.addr(), cfg.username, cfg.password)
|
2021-02-25 08:40:04 +01:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("creating client: %w", err)
|
|
|
|
}
|
|
|
|
clients[i] = c
|
|
|
|
}
|
|
|
|
|
|
|
|
start := time.Now()
|
|
|
|
|
|
|
|
for _, c := range clients {
|
|
|
|
wg.Add(1)
|
|
|
|
go func(c *client.Client) {
|
|
|
|
defer wg.Done()
|
2021-05-07 15:43:43 +02:00
|
|
|
if err := browser(cfg.addr(), c, bar); err != nil {
|
2021-02-25 08:40:04 +01:00
|
|
|
log.Printf("Client failed: %v", err)
|
|
|
|
}
|
|
|
|
}(c)
|
|
|
|
}
|
|
|
|
|
|
|
|
progress.Wait()
|
|
|
|
log.Printf("Run for %v", time.Now().Sub(start))
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd.Flags().IntVarP(&clientCount, "amount", "a", 10, "Amount of users to use.")
|
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
2021-05-07 15:43:43 +02:00
|
|
|
func browser(url string, c *client.Client, bar *mpb.Bar) error {
|
2021-02-25 08:40:04 +01:00
|
|
|
if err := c.Login(); err != nil {
|
|
|
|
return fmt.Errorf("login client: %w", err)
|
|
|
|
}
|
|
|
|
bar.Increment()
|
|
|
|
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
|
|
|
|
for _, path := range []string{
|
|
|
|
pathWhoami,
|
|
|
|
pathLogin,
|
|
|
|
pathServertime,
|
|
|
|
pathConstants,
|
|
|
|
} {
|
|
|
|
wg.Add(1)
|
|
|
|
go func(path string) {
|
|
|
|
defer wg.Done()
|
|
|
|
|
2021-05-07 15:43:43 +02:00
|
|
|
req, err := http.NewRequest("GET", url+path, nil)
|
2021-02-25 08:40:04 +01:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("Error creating request: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := c.Do(req); err != nil {
|
|
|
|
log.Printf("Error get request to %s: %v", path, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
bar.Increment()
|
|
|
|
}(path)
|
|
|
|
}
|
|
|
|
|
|
|
|
wg.Wait()
|
|
|
|
return nil
|
|
|
|
}
|