*/ private const STATUS_MIX = [ 'open' => 40, 'in-progress' => 15, 'on-hold' => 8, 'blocked' => 8, 'completed' => 20, 'cancelled' => 5, 'deferred' => 4, ]; /** * Folder colours, matching the palette the folder UI offers. * * @var list */ private const COLORS = ['red', 'orange', 'amber', 'green', 'emerald', 'blue', 'indigo', 'purple', 'pink']; /** * Execute the console command. */ public function handle(): int { if (app()->environment('production') && ! $this->option('force')) { $this->error('Refusing to seed load-test data in production without --force.'); return self::FAILURE; } $users = (int) $this->option('users'); $admins = (int) $this->option('admins'); $folders = (int) $this->option('folders'); $todos = (int) $this->option('todos'); $chunk = max(1, (int) $this->option('chunk')); if ($folders < 1) { $this->error('--folders must be at least 1, because every todo belongs to a folder.'); return self::FAILURE; } if ($this->option('fresh')) { $this->deleteSeededUsers(); } $this->line(sprintf( 'Seeding %d user(s) and %d admin(s), %d folder(s) and %d todo(s) each...', $users, $admins, $folders, $todos, )); $manifest = []; $progress = $this->output->createProgressBar($users + $admins); $progress->start(); for ($index = 0; $index < $users + $admins; $index += $chunk) { $size = min($chunk, $users + $admins - $index); foreach ($this->createChunk($index, $size, $folders, $todos) as $entry) { $manifest[] = $entry; } $progress->advance($size); } $progress->finish(); $this->newLine(2); $path = $this->writeManifest($manifest); $this->line(sprintf('Wrote %d session(s) to %s', count($manifest), $path)); $this->newLine(); $this->line('Run the load tests against a served copy of this app with:'); $this->line(' k6 run load-tests/app.js'); return self::SUCCESS; } /** * Create one batch of users and populate each with folders and todos. * * @return list, folderIds: list}> */ private function createChunk(int $offset, int $size, int $folders, int $todos): array { $entries = []; $cookieName = (string) config('session.cookie'); for ($i = $offset; $i < $offset + $size; $i++) { $isAdmin = $i >= (int) $this->option('users'); $user = $this->createUser($i, $isAdmin); $organizationId = (int) $user->current_organization_id; $folderIds = $this->seedFolders($user->id, $organizationId, $folders); $this->seedTodos($user->id, $organizationId, $folderIds, $todos); $sessionId = $this->createSession($user); $entries[] = [ 'userId' => $user->id, 'email' => $user->email, 'isAdmin' => $isAdmin, 'sessionId' => $sessionId, 'cookieName' => $cookieName, 'cookie' => $this->sessionCookie($sessionId), 'todoIds' => $this->sampleTodoIds($user->id, $folderIds), 'folderIds' => array_slice($folderIds, 0, 10), ]; } return $entries; } /** * Create one entitled user. * * UserFactory already attaches a personal organization and a local * subscription row, which is all EnsureSubscribed reads, so no Stripe * call is involved and none is possible. */ private function createUser(int $index, bool $isAdmin): User { $factory = User::factory(); if ($isAdmin) { $factory = $factory->asAdmin(); } return $factory->create([ 'name' => $isAdmin ? 'Load Admin' : 'Load User '.$index, 'email' => sprintf('loadtest%05d@%s', $index, self::EMAIL_DOMAIN), ]); } /** * Insert a folder tree for a user and return the ids of every folder. * * Parents are inserted in their own batch ahead of their children so the * self-referencing foreign key is always satisfied by a row that already * exists. * * @return list */ private function seedFolders(int $userId, int $organizationId, int $count): array { $now = now(); $visibility = $this->visibilityFor($organizationId); $insert = fn (string $name, ?int $parentId, int $position): array => [ 'user_id' => $userId, 'organization_id' => $organizationId, 'name' => $name, 'visibility' => $visibility, 'parent_id' => $parentId, 'color' => self::COLORS[($position + $userId) % count(self::COLORS)], 'created_at' => $now, 'updated_at' => $now, ]; $rootCount = max(1, (int) round($count / 3)); $childCount = (int) round(($count - $rootCount) / 2); $rows = []; for ($i = 0; $i < $rootCount && $i < $count; $i++) { $rows[] = $insert('Project '.($i + 1), null, $i); } DB::table('folders')->insert($rows); $rootIds = $this->folderIds($userId); $childIds = []; if ($childCount > 0 && $rootIds !== []) { $rows = []; for ($i = 0; $i < $childCount; $i++) { $rows[] = $insert('Milestone '.($i + 1), $rootIds[$i % count($rootIds)], $i); } DB::table('folders')->insert($rows); $childIds = array_values(array_diff($this->folderIds($userId), $rootIds)); } $remaining = $count - count($rootIds) - count($childIds); if ($remaining > 0 && $childIds !== []) { $rows = []; for ($i = 0; $i < $remaining; $i++) { $rows[] = $insert('Task '.($i + 1), $childIds[$i % count($childIds)], $i); } DB::table('folders')->insert($rows); $childIds = array_values(array_diff($this->folderIds($userId), $rootIds)); } return $childIds; } /** * Read back a user's folder ids in insertion order. * * Ids are read rather than predicted because a cascade delete advances * MySQL's auto-increment counter without lowering MAX(id), so the next * insert is not guaranteed to continue from the current maximum. * * @return list */ private function folderIds(int $userId): array { return DB::table('folders') ->where('user_id', $userId) ->orderBy('id') ->pluck('id') ->map(fn ($id): int => (int) $id) ->all(); } /** * Get the folder visibility a workspace defaults to, so bulk inserts match * what the application would have written. */ private function visibilityFor(int $organizationId): string { $type = DB::table('organizations')->where('id', $organizationId)->value('type'); return strval($type) === 'personal' ? 'private' : 'workspace'; } /** * Insert a user's todos, spread across their folders. * * @param list $folderIds */ private function seedTodos(int $userId, int $organizationId, array $folderIds, int $count): void { if ($folderIds === [] || $count < 1) { return; } $statuses = $this->weightedStatuses(); $now = now(); $today = $now->copy()->startOfDay(); foreach (array_chunk(range(0, $count - 1), 500) as $batch) { $rows = []; $completedSeen = 0; foreach ($batch as $index) { $status = $statuses[$index % count($statuses)]; $isTerminal = $status === 'completed'; if ($isTerminal) { $completedSeen++; } // Only active statuses can be overdue or due today, because // both dashboard counts filter on active() as well. $dueDate = $this->dueDateFor($status, $today, $index); $rows[] = [ 'user_id' => $userId, 'organization_id' => $organizationId, 'folder_id' => $folderIds[$index % count($folderIds)], 'description' => $this->descriptionFor($index), 'notes' => $index % 4 === 0 ? 'Context for item '.$index.'. '.str_repeat('Additional detail. ', 4) : null, 'status' => $status, // Leaving roughly a third without a position exercises the // "position IS NULL" branch of the priority sort. 'position' => $index % 3 === 0 ? $completedSeen + 1 : null, 'due_date' => $dueDate, 'completed_at' => $isTerminal ? $today->copy()->subDays($index % 30)->setTime(12, 0) : null, 'created_at' => $now->copy()->subDays($index % 60), 'updated_at' => $now, ]; } DB::table('todos')->insert($rows); } } /** * Expand the status mix into one entry per todo. * * @return list */ private function weightedStatuses(): array { $statuses = []; foreach (self::STATUS_MIX as $status => $weight) { for ($i = 0; $i < $weight; $i++) { $statuses[] = $status; } } return $statuses; } /** * Choose a due date so the overdue, due-today and future buckets are all * populated for the active statuses. */ private function dueDateFor(string $status, CarbonInterface $today, int $index): ?string { $active = ! in_array($status, ['completed', 'cancelled', 'deferred'], true); if ($index % 5 === 0) { return null; } if ($active && $index % 5 === 1) { return $today->copy()->subDays(($index % 21) + 1)->toDateString(); } if ($active && $index % 5 === 2) { return $today->toDateString(); } return $today->copy()->addDays(($index % 90) + 1)->toDateString(); } /** * Build a todo description, varying length so the search column holds a * realistic mix rather than one uniform string. */ private function descriptionFor(int $index): string { $short = [ 'Review pull request', 'Reply to inbox', 'Book dentist', 'Water the plants', 'Refill prescriptions', 'Pay the invoice', 'Update the changelog', 'Renew the domain', ]; $base = $short[$index % count($short)]; if ($index % 3 === 0) { return $base.' #'.($index + 1); } if ($index % 3 === 1) { return $base.' for the '.['launch', 'quarterly review', 'migration', 'handover'][$index % 4].' workstream'; } return $base.' and confirm the follow-up items with the team before the '.['standup', 'review', 'retro'][$index % 3].' on Friday'; } /** * Pick todo ids for the write scenarios to act on. * * The load tests need real ids because the write calls go through * findOrFail against the requesting user, so a fabricated id would measure * a 404 instead of a write. Rotating over several ids keeps the write * spread across rows the way real clicking does, rather than hammering * one row that stays hot in the buffer pool. * * @param list $folderIds * @return list */ private function sampleTodoIds(int $userId, array $folderIds): array { return DB::table('todos') ->where('user_id', $userId) ->whereIn('folder_id', $folderIds === [] ? [0] : $folderIds) ->whereNull('completed_at') ->orderBy('id') ->limit(5) ->pluck('id') ->map(fn ($id): int => (int) $id) ->all(); } /** * Create a logged-in session row for a user and return its id. * * Writing through the session store keeps the stored payload in exactly * the shape the framework's own session handler reads back, which is what * the load tests need in order to authenticate by cookie alone. * * The id is read back only after logging in, because SessionGuard::login() * calls migrate() internally and replaces it with a fresh one. */ private function createSession(User $user): string { $store = app('session.store'); $store->setId(Str::random(40)); $store->start(); Auth::guard('web')->login($user); $store->save(); return $store->getId(); } /** * Build the cookie header value a browser would present for a session. * * EncryptCookies wraps every cookie value in an HMAC prefix and then * encrypts it, so a bare session id is not accepted on the wire. The * seeder therefore has to produce the same envelope, using the same * serializer flag the middleware reports for itself. */ private function sessionCookie(string $sessionId): string { $name = (string) config('session.cookie'); $prefix = CookieValuePrefix::create($name, app('encrypter')->getKey()); return app('encrypter')->encrypt( $prefix.$sessionId, EncryptCookies::serialized($name), ); } /** * Remove previously seeded accounts, relying on cascades for their data. */ private function deleteSeededUsers(): void { $count = DB::table('users')->where('email', 'like', '%@'.self::EMAIL_DOMAIN)->count(); if ($count === 0) { $this->line('No previously seeded load-test users to remove.'); return; } $userIds = DB::table('users')->where('email', 'like', '%@'.self::EMAIL_DOMAIN)->pluck('id'); DB::table('sessions')->whereIn('user_id', $userIds)->delete(); User::query()->whereIn('id', $userIds)->delete(); $this->line("Removed {$count} previously seeded load-test user(s)."); } /** * Write the session manifest that the k6 script reads at init time. * * @param list $manifest */ private function writeManifest(array $manifest): string { $path = (string) ($this->option('sessions') ?: base_path('load-tests/sessions.json')); $directory = dirname($path); if (! is_dir($directory)) { mkdir($directory, 0755, recursive: true); } file_put_contents($path, json_encode($manifest, JSON_PRETTY_PRINT | JSON_THROW_ON_ERROR).PHP_EOL); return $path; } }