133 lines
3.2 KiB
Go
133 lines
3.2 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"crypto/rand"
|
|
"encoding/hex"
|
|
"fmt"
|
|
mrand "math/rand/v2"
|
|
"sync"
|
|
"time"
|
|
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
// seedPassword is shared by the synthetic users of one run.
|
|
const seedPassword = "loadgen-synthetic-password"
|
|
|
|
// seedTimeout bounds one setup request. The setup is not measured, and
|
|
// registration hashes a password with Argon2id, slow on a small replica.
|
|
const seedTimeout = 30 * time.Second
|
|
|
|
// seedWorkers bounds the parallel registrations: each one hashes a password
|
|
// with Argon2id, and the setup must not be what the study measures.
|
|
const seedWorkers = 8
|
|
|
|
type actor struct {
|
|
id, token string
|
|
}
|
|
|
|
type room struct {
|
|
id string
|
|
members []int // indexes into dataset.actors
|
|
}
|
|
|
|
type dataset struct {
|
|
actors []actor
|
|
rooms []room
|
|
// messages is the number of messages written while seeding.
|
|
messages int
|
|
}
|
|
|
|
// seed registers users and logs each in once, creates chats of two to five
|
|
// random members and writes perChat messages into each. Logins happen here,
|
|
// so the password hashing never shows up in the measured steps.
|
|
func seed(ctx context.Context, c *client, rng *mrand.Rand, users, chats, perChat int) (dataset, error) {
|
|
if users < 2 || chats < 1 {
|
|
return dataset{}, fmt.Errorf("need at least 2 users and 1 chat")
|
|
}
|
|
runID := randomHex(3)
|
|
ds := dataset{actors: make([]actor, users)}
|
|
if err := parallel(users, func(i int) error {
|
|
name := fmt.Sprintf("lg_%s_%d", runID, i)
|
|
id, err := c.register(ctx, name, seedPassword)
|
|
if err != nil {
|
|
return fmt.Errorf("register %s: %w", name, err)
|
|
}
|
|
token, err := c.login(ctx, name, seedPassword)
|
|
if err != nil {
|
|
return fmt.Errorf("log in %s: %w", name, err)
|
|
}
|
|
ds.actors[i] = actor{id: id, token: token}
|
|
return nil
|
|
}); err != nil {
|
|
return dataset{}, err
|
|
}
|
|
|
|
for j := range chats {
|
|
size := 2 + rng.IntN(4)
|
|
members := rng.Perm(users)[:min(size, users)]
|
|
ids := make([]string, len(members))
|
|
for k, m := range members {
|
|
ids[k] = ds.actors[m].id
|
|
}
|
|
id, err := c.createChat(ctx, ds.actors[members[0]].token, fmt.Sprintf("load %s %d", runID, j), ids)
|
|
if err != nil {
|
|
return dataset{}, fmt.Errorf("create chat %d: %w", j, err)
|
|
}
|
|
ds.rooms = append(ds.rooms, room{id: id, members: members})
|
|
}
|
|
|
|
if err := parallel(len(ds.rooms), func(j int) error {
|
|
r := ds.rooms[j]
|
|
for k := range perChat {
|
|
sender := ds.actors[r.members[k%len(r.members)]]
|
|
res := c.send(ctx, r.id, sender.token, uuid.NewString(), fmt.Sprintf("seed %d", k))
|
|
if err := expect(res, 201, nil); err != nil {
|
|
return fmt.Errorf("seed message into chat %d: %w", j, err)
|
|
}
|
|
}
|
|
return nil
|
|
}); err != nil {
|
|
return dataset{}, err
|
|
}
|
|
ds.messages = len(ds.rooms) * perChat
|
|
return ds, nil
|
|
}
|
|
|
|
// parallel runs fn(0..n-1) on seedWorkers goroutines and returns the first
|
|
// error.
|
|
func parallel(n int, fn func(i int) error) error {
|
|
var (
|
|
wg sync.WaitGroup
|
|
mu sync.Mutex
|
|
first error
|
|
)
|
|
next := make(chan int)
|
|
for range seedWorkers {
|
|
wg.Go(func() {
|
|
for i := range next {
|
|
if err := fn(i); err != nil {
|
|
mu.Lock()
|
|
if first == nil {
|
|
first = err
|
|
}
|
|
mu.Unlock()
|
|
}
|
|
}
|
|
})
|
|
}
|
|
for i := range n {
|
|
next <- i
|
|
}
|
|
close(next)
|
|
wg.Wait()
|
|
return first
|
|
}
|
|
|
|
func randomHex(n int) string {
|
|
b := make([]byte, n)
|
|
_, _ = rand.Read(b)
|
|
return hex.EncodeToString(b)
|
|
}
|