63 lines
1.7 KiB
Go
63 lines
1.7 KiB
Go
package scenarios
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"encoding/json"
|
|
"math/rand"
|
|
"net/http"
|
|
"sync/atomic"
|
|
"time"
|
|
|
|
"github.com/topfans/backend/scripts/loadgen/loadgen/lib"
|
|
)
|
|
|
|
type s1Login struct {
|
|
client *http.Client
|
|
users []lib.TestUser
|
|
errCount *atomic.Int64
|
|
totalCount *atomic.Int64
|
|
fiveXXCount *atomic.Int64
|
|
rec *lib.LatencyRecorder
|
|
breaker *lib.CircuitBreaker
|
|
prodSSH string
|
|
baseURL string
|
|
}
|
|
|
|
func init() { register("S1", newS1) }
|
|
|
|
func newS1(c *http.Client, u []lib.TestUser, e, t, f *atomic.Int64, r *lib.LatencyRecorder, b *lib.CircuitBreaker, ssh string) Scenario {
|
|
return &s1Login{client: c, users: u, errCount: e, totalCount: t, fiveXXCount: f, rec: r, breaker: b, prodSSH: ssh, baseURL: DefaultBaseURL}
|
|
}
|
|
|
|
func (s *s1Login) Run(ctx context.Context, rpsOverride int, durationOverride time.Duration, dash *lib.Dashboard, breaker *lib.CircuitBreaker, stages []int) error {
|
|
targetRPS := rpsOverride
|
|
if targetRPS == 0 {
|
|
targetRPS = 15
|
|
}
|
|
duration := durationOverride
|
|
if duration == 0 {
|
|
duration = 2 * time.Minute
|
|
}
|
|
|
|
ticker := time.NewTicker(time.Second / time.Duration(targetRPS))
|
|
defer ticker.Stop()
|
|
timeout := time.NewTimer(duration)
|
|
defer timeout.Stop()
|
|
|
|
for {
|
|
select {
|
|
case <-ctx.Done():
|
|
return nil
|
|
case <-timeout.C:
|
|
return nil
|
|
case <-ticker.C:
|
|
u := s.users[rand.Intn(len(s.users))]
|
|
body, _ := json.Marshal(map[string]string{"mobile": u.Phone, "password": u.Password})
|
|
req, _ := http.NewRequest("POST", s.baseURL+"/api/v1/auth/login", bytes.NewReader(body))
|
|
req.Header.Set("Content-Type", "application/json")
|
|
doRequest(s.client, req, s.rec, s.errCount, s.totalCount, s.fiveXXCount, s.breaker)
|
|
}
|
|
}
|
|
}
|