main.go135 lines, 3.6 KB
| 1 | // Command seed fills the database with a made-up social graph, for load tests. |
| 2 | // The same flags always build the same graph. |
| 3 | package main |
| 4 | |
| 5 | import ( |
| 6 | "context" |
| 7 | "crypto/sha256" |
| 8 | "errors" |
| 9 | "flag" |
| 10 | "fmt" |
| 11 | "math" |
| 12 | "math/rand/v2" |
| 13 | "os" |
| 14 | "time" |
| 15 | |
| 16 | "github.com/fiston-user/murmur/internal/platform/postgres" |
| 17 | "github.com/jackc/pgx/v5" |
| 18 | ) |
| 19 | |
| 20 | func main() { |
| 21 | users := flag.Int("users", 100_000, "how many users to create") |
| 22 | seed := flag.Uint64("seed", 1, "the same seed always builds the same graph") |
| 23 | flag.Parse() |
| 24 | if err := run(context.Background(), *users, *seed); err != nil { |
| 25 | fmt.Fprintln(os.Stderr, "seed:", err) |
| 26 | os.Exit(1) |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | // load copies rows into one table, and prints how many. |
| 31 | func load(ctx context.Context, conn *pgx.Conn, table string, cols []string, src pgx.CopyFromSource) error { |
| 32 | start := time.Now() |
| 33 | n, err := conn.CopyFrom(ctx, pgx.Identifier{table}, cols, src) |
| 34 | if err != nil { |
| 35 | return fmt.Errorf("copy %s: %w", table, err) |
| 36 | } |
| 37 | fmt.Printf("%-8s %9d rows %5.1fs\n", table, n, time.Since(start).Seconds()) |
| 38 | return nil |
| 39 | } |
| 40 | |
| 41 | func run(ctx context.Context, users int, seed uint64) error { |
| 42 | if users < 1000 { |
| 43 | return errors.New("--users must be at least 1000") |
| 44 | } |
| 45 | url := os.Getenv("DATABASE_URL") |
| 46 | if url == "" { |
| 47 | return errors.New("DATABASE_URL is not set") |
| 48 | } |
| 49 | if err := postgres.Migrate(url); err != nil { |
| 50 | return fmt.Errorf("migrate: %w", err) |
| 51 | } |
| 52 | conn, err := pgx.Connect(ctx, url) |
| 53 | if err != nil { |
| 54 | return fmt.Errorf("connect to postgres: %w", err) |
| 55 | } |
| 56 | defer conn.Close(ctx) |
| 57 | |
| 58 | // Start from nothing, so the first user is always id 1. |
| 59 | if _, err := conn.Exec(ctx, ` |
| 60 | truncate users, posts, follows, jobs, likes, like_deltas, notifications |
| 61 | restart identity`); err != nil { |
| 62 | return err |
| 63 | } |
| 64 | // We only write ids we just made: skip the foreign key checks. |
| 65 | if _, err := conn.Exec(ctx, `set session_replication_role = replica`); err != nil { |
| 66 | return err |
| 67 | } |
| 68 | |
| 69 | r := rand.New(rand.NewPCG(seed, 0)) |
| 70 | big := rand.NewZipf(r, 1.2, 5, uint64(users-1)) |
| 71 | // pick chooses an account: one time in five a big name, otherwise anybody. |
| 72 | pick := func() int64 { |
| 73 | if r.IntN(5) == 0 { |
| 74 | return int64(big.Uint64()) + 1 |
| 75 | } |
| 76 | return r.Int64N(int64(users)) + 1 |
| 77 | } |
| 78 | |
| 79 | err = load(ctx, conn, "users", []string{"handle", "token_hash"}, |
| 80 | pgx.CopyFromSlice(users, func(i int) ([]any, error) { |
| 81 | hash := sha256.Sum256(fmt.Appendf(nil, "seed-token-%d", i+1)) |
| 82 | return []any{fmt.Sprintf("user%d", i+1), hash[:]}, nil |
| 83 | })) |
| 84 | if err != nil { |
| 85 | return err |
| 86 | } |
| 87 | |
| 88 | follower, left := int64(0), 0 |
| 89 | seen := map[int64]bool{} |
| 90 | err = load(ctx, conn, "follows", []string{"follower_id", "followee_id"}, |
| 91 | pgx.CopyFromFunc(func() ([]any, error) { |
| 92 | if left == 0 { |
| 93 | if follower++; follower > int64(users) { |
| 94 | return nil, nil |
| 95 | } |
| 96 | left = 10 + int(190*math.Pow(r.Float64(), 4)) |
| 97 | clear(seen) |
| 98 | } |
| 99 | for { |
| 100 | if id := pick(); id != follower && !seen[id] { |
| 101 | seen[id] = true |
| 102 | left-- |
| 103 | return []any{follower, id}, nil |
| 104 | } |
| 105 | } |
| 106 | })) |
| 107 | if err != nil { |
| 108 | return err |
| 109 | } |
| 110 | |
| 111 | // Every user's follower count, from the follows we just loaded. |
| 112 | _, err = conn.Exec(ctx, ` |
| 113 | update users u |
| 114 | set followers_count = c.n |
| 115 | from (select followee_id, count(*) as n from follows group by followee_id) c |
| 116 | where u.id = c.followee_id`) |
| 117 | if err != nil { |
| 118 | return err |
| 119 | } |
| 120 | |
| 121 | first := time.Date(2026, 1, 1, 0, 0, 0, 0, time.UTC) |
| 122 | write := writer(seed) |
| 123 | err = load(ctx, conn, "posts", []string{"user_id", "body", "created_at"}, |
| 124 | pgx.CopyFromSlice(20*users, func(i int) ([]any, error) { |
| 125 | author := pick() |
| 126 | return []any{author, write(), first.Add(time.Duration(i) * time.Second)}, nil |
| 127 | })) |
| 128 | if err != nil { |
| 129 | return err |
| 130 | } |
| 131 | |
| 132 | // Fresh statistics for the planner, before anyone measures. |
| 133 | _, err = conn.Exec(ctx, `vacuum analyze`) |
| 134 | return err |
| 135 | } |