Loheide-eu-mitarbeiterplatt.../scripts/lasttest.php
Friederich Loheide 9ff319f647
Some checks failed
CI / PHP 8.3 / mariadb:10.6 (pull_request) Successful in 4m10s
CI / PHP 8.4 / mariadb:10.6 (pull_request) Successful in 4m8s
CI / PHP 8.4 / mariadb:11.4 (pull_request) Successful in 4m26s
CI / PHP 8.3 / mariadb:11.4 (pull_request) Successful in 4m28s
CI / PHP 8.4 / mysql:8.0 (pull_request) Failing after 6m31s
CI / PHP 8.3 / mysql:8.0 (pull_request) Failing after 6m33s
CI / PHP 8.4 / mysql:8.4 (pull_request) Failing after 5m13s
CI / PHP 8.3 / mysql:8.4 (pull_request) Failing after 5m18s
CI / Build-Artefakte aktuell (pull_request) Successful in 1m16s
Lasttest 5.000 Personen: Skript, Ergebnis, N+1 in der Seitenliste behoben
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-09-27 04:40:54 +00:00

271 lines
14 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
/**
* Lasttest für große Installationen (Plan, Abschnitt 9: bis 5.000 Mitarbeitende).
*
* php scripts/lasttest.php seed # Datenbank „inhaus_load“ neu aufbauen und füllen
* php scripts/lasttest.php measure # wichtige Seiten messen: Zeit und Datenbankabfragen
*
* Läuft vollständig im Prozess (ohne Webserver), damit die Zahlen die Arbeit des
* Portals zeigen und nicht die des Testrechners. Nur für die Entwicklung.
*/
use App\Models\Role;
use App\Models\User;
use App\Plugins\PluginManager;
use Illuminate\Contracts\Console\Kernel as ConsoleKernel;
use Illuminate\Contracts\Http\Kernel as HttpKernel;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;
$mode = $argv[1] ?? 'measure';
$database = getenv('LOADTEST_DB') ?: 'inhaus_load';
putenv("DB_DATABASE={$database}");
$_ENV['DB_DATABASE'] = $_SERVER['DB_DATABASE'] = $database;
putenv('APP_DEBUG=false');
$_ENV['APP_DEBUG'] = $_SERVER['APP_DEBUG'] = 'false';
putenv('CACHE_STORE=array');
$_ENV['CACHE_STORE'] = $_SERVER['CACHE_STORE'] = 'array';
require is_file(__DIR__.'/../vendor-dev/autoload.php') ? __DIR__.'/../vendor-dev/autoload.php' : __DIR__.'/../vendor/autoload.php';
$app = require __DIR__.'/../bootstrap/app.php';
$app->make(ConsoleKernel::class)->bootstrap();
ini_set('memory_limit', '1024M');
$counts = [
'users' => 5000, 'roles' => 30, 'departments' => 40, 'locations' => 15,
'news' => 2000, 'comments' => 20000, 'events' => 500, 'pages' => 300,
'download_folders' => 100, 'downloads' => 3000, 'qm_folders' => 80, 'qm_entries' => 2000,
'audit' => 5000,
];
$words = explode(' ', 'Arbeit Team Projekt Werkstatt Pflege Verwaltung Sommerfest Fortbildung Dienstplan Sicherheit Hygiene Datenschutz Qualität Küche Garten Holz Metall Montage Fahrdienst Personal Zeiterfassung Urlaub Betriebsrat Mitarbeitervertretung Jubiläum Leitbild Strategie Digitalisierung Software Drucker Netzwerk Besprechung Einarbeitung Gesundheit Brandschutz Erste Hilfe Anleitung Formular Antrag Richtlinie');
$text = fn (int $n) => implode(' ', array_map(fn () => $words[array_rand($words)], range(1, $n)));
$now = now();
function chunked(string $table, iterable $rows, int $size = 1000): void
{
$buffer = [];
foreach ($rows as $row) {
$buffer[] = $row;
if (count($buffer) >= $size) {
DB::table($table)->insert($buffer);
$buffer = [];
}
}
if ($buffer) {
DB::table($table)->insert($buffer);
}
}
if ($mode === 'seed') {
$started = microtime(true);
echo "Datenbank {$database} neu aufbauen …\n";
Artisan::call('migrate:fresh', ['--force' => true]);
// Plugins wie in einer vollen Installation
$plugins = app(PluginManager::class);
foreach (array_keys($plugins->all()) as $slug) {
if (! $plugins->isEnabled($slug) && ! in_array($slug, ['ldap', 'oidc', 'saml', 'wordpress-import'], true)) {
try {
$plugins->activate($slug);
} catch (Throwable $e) {
echo " Plugin {$slug}: {$e->getMessage()}\n";
}
}
}
Artisan::call('migrate', ['--force' => true]);
$password = bcrypt('password');
$departments = array_map(fn ($i) => 'Bereich '.$i, range(1, $counts['departments']));
$locations = array_map(fn ($i) => 'Standort '.$i, range(1, $counts['locations']));
echo "Rollen und Personen …\n";
$admin = User::query()->forceCreate(['first_name' => 'Ada', 'last_name' => 'Admin', 'name' => 'Ada Admin', 'email' => 'admin@example.com', 'password' => $password, 'is_active' => true, 'department' => $departments[0], 'location' => $locations[0]]);
$admin->roles()->attach(Role::administration()->id, ['source' => 'manual', 'created_at' => $now]);
$roleIds = [];
foreach (range(1, $counts['roles']) as $i) {
$roleIds[] = DB::table('roles')->insertGetId(['name' => "Rolle {$i}", 'slug' => "rolle-{$i}", 'is_system' => false, 'created_at' => $now, 'updated_at' => $now]);
}
chunked('users', (function () use ($counts, $departments, $locations, $password, $now) {
foreach (range(2, $counts['users']) as $i) {
$first = 'Vorname'.$i;
$last = 'Nachname'.$i;
yield [
'name' => "{$first} {$last}", 'first_name' => $first, 'last_name' => $last,
'email' => "person{$i}@example.org", 'password' => $password,
'job_title' => 'Funktion '.($i % 60), 'department' => $departments[$i % count($departments)], 'location' => $locations[$i % count($locations)],
'phone' => '05482 72-'.str_pad((string) ($i % 1000), 3, '0', STR_PAD_LEFT),
'manager_id' => $i > 50 ? random_int(2, min($i - 1, 800)) : null,
'is_active' => $i % 50 !== 0, 'created_at' => $now->copy()->subDays(random_int(0, 2000)), 'updated_at' => $now,
];
}
})());
chunked('role_user', (function () use ($counts, $roleIds, $now) {
foreach (range(2, $counts['users']) as $i) {
foreach ((array) array_rand(array_flip($roleIds), random_int(1, 3)) as $roleId) {
yield ['role_id' => $roleId, 'user_id' => $i, 'source' => 'manual', 'created_at' => $now];
}
}
})());
echo "Neuigkeiten und Kommentare …\n";
$categories = [];
foreach (range(1, 10) as $i) {
$categories[] = DB::table('news_categories')->insertGetId(['name' => "Kategorie {$i}", 'slug' => "kategorie-{$i}", 'sort' => $i, 'created_at' => $now, 'updated_at' => $now]);
}
// Drei Kategorien nur für bestimmte Bereiche
foreach (array_slice($categories, 0, 3) as $n => $categoryId) {
DB::table('audiences')->insert(['resource_type' => 'news_category', 'resource_id' => $categoryId, 'rule_type' => 'department', 'rule_value' => $departments[$n]]);
}
chunked('news_posts', (function () use ($counts, $categories, $text, $now) {
foreach (range(1, $counts['news']) as $i) {
$body = '<p>'.$text(120).'</p><h2>'.$text(4).'</h2><p>'.$text(150).'</p>';
yield [
'category_id' => $categories[$i % count($categories)], 'author_id' => random_int(1, 500),
'title' => "Beitrag {$i}: ".$text(5), 'slug' => "beitrag-{$i}", 'teaser' => $text(20),
'body' => $body, 'body_html' => $body, 'reading_minutes' => 2, 'status' => 'published',
'is_featured' => $i > $counts['news'] - 5, 'comments_enabled' => true,
'published_at' => $now->copy()->subHours($counts['news'] - $i), 'created_at' => $now, 'updated_at' => $now,
];
}
})());
chunked('news_comments', (function () use ($counts, $text, $now) {
foreach (range(1, $counts['comments']) as $i) {
yield [
'post_id' => $counts['news'] - ($i % 300), 'parent_id' => null, 'user_id' => random_int(1, $counts['users']),
'body' => $text(25), 'created_at' => $now->copy()->subMinutes($i), 'updated_at' => $now,
];
}
})());
echo "Termine, Seiten, Downloads, QM, Protokoll …\n";
chunked('events', (function () use ($counts, $text, $now) {
foreach (range(1, $counts['events']) as $i) {
$start = $now->copy()->addHours(($i - 100) * 12);
yield ['author_id' => 1, 'title' => "Termin {$i}: ".$text(3), 'description' => '<p>'.$text(40).'</p>', 'description_html' => '<p>'.$text(40).'</p>', 'location' => 'Raum '.($i % 30), 'starts_at' => $start, 'ends_at' => $start->copy()->addHours(2), 'all_day' => false, 'is_published' => true, 'created_at' => $now, 'updated_at' => $now];
}
})());
foreach (range(1, $counts['pages']) as $i) {
$parent = $i > 20 ? random_int(1, min($i - 1, 120)) : null;
$slug = "seite-{$i}";
$path = $parent ? DB::table('pages')->where('id', $parent)->value('path').'/'.$slug : $slug;
$content = json_encode(['blocks' => [['id' => Str::random(10), 'type' => 'heading', 'data' => ['text' => $text(4), 'level' => 'h2']], ['id' => Str::random(10), 'type' => 'text', 'data' => ['html' => '<p>'.$text(200).'</p>']]]]);
DB::table('pages')->insert(['parent_id' => $parent, 'title' => "Seite {$i}: ".$text(3), 'slug' => $slug, 'path' => $path, 'sort' => $i, 'in_navigation' => $i <= 5, 'content' => $content, 'published_at' => $now, 'created_by' => 1, 'created_at' => $now, 'updated_at' => $now]);
}
foreach (range(1, $counts['download_folders']) as $i) {
DB::table('download_folders')->insert(['parent_id' => $i > 10 ? random_int(1, min($i - 1, 40)) : null, 'name' => "Ordner {$i}", 'sort' => $i, 'created_by' => 1, 'created_at' => $now, 'updated_at' => $now]);
}
foreach (range(1, $counts['downloads']) as $i) {
$id = DB::table('downloads')->insertGetId(['folder_id' => random_int(1, $counts['download_folders']), 'title' => "Datei {$i}: ".$text(3), 'description' => $text(10), 'created_by' => 1, 'download_count' => random_int(0, 500), 'created_at' => $now, 'updated_at' => $now]);
$version = DB::table('download_versions')->insertGetId(['download_id' => $id, 'version' => 1, 'name' => "datei-{$i}.pdf", 'path' => "downloads/datei-{$i}.pdf", 'mime' => 'application/pdf', 'size' => 12345, 'uploaded_by' => 1, 'created_at' => $now]);
DB::table('downloads')->where('id', $id)->update(['current_version_id' => $version]);
}
$qmCategories = [DB::table('qm_categories')->insertGetId(['name' => 'Führung', 'sort' => 1, 'created_at' => $now, 'updated_at' => $now]), DB::table('qm_categories')->insertGetId(['name' => 'Prozesse', 'sort' => 2, 'created_at' => $now, 'updated_at' => $now])];
foreach (range(1, $counts['qm_folders']) as $i) {
DB::table('qm_folders')->insert(['category_id' => $i <= 20 ? $qmCategories[$i % 2] : null, 'parent_id' => $i > 20 ? random_int(1, 20) : null, 'name' => "QM-Ordner {$i}", 'color' => 'teal', 'sort' => $i, 'created_by' => 1, 'created_at' => $now, 'updated_at' => $now]);
}
foreach (range(1, $counts['qm_entries']) as $i) {
$id = DB::table('qm_entries')->insertGetId(['folder_id' => random_int(1, $counts['qm_folders']), 'type' => 'file', 'title' => "QM-Dokument {$i}: ".$text(3), 'version' => '1.'.($i % 9), 'valid_from' => $now->toDateString(), 'sort' => $i, 'created_by' => 1, 'created_at' => $now, 'updated_at' => $now]);
DB::table('qm_files')->insert(['entry_id' => $id, 'kind' => 'reading', 'version' => '1', 'name' => "qm-{$i}.pdf", 'path' => "qm/qm-{$i}.pdf", 'mime' => 'application/pdf', 'size' => 54321, 'text' => $text(400), 'text_status' => 'ok', 'uploaded_by' => 1, 'created_at' => $now]);
}
chunked('audit_logs', (function () use ($counts, $now) {
foreach (range(1, $counts['audit']) as $i) {
yield ['user_id' => random_int(1, 100), 'action' => ['page.published', 'news.published', 'plugin.activated'][$i % 3], 'data' => json_encode(['title' => "Eintrag {$i}"]), 'created_at' => $now->copy()->subMinutes($i)];
}
})());
printf("Fertig in %.1f s.\n", microtime(true) - $started);
exit(0);
}
// ——— Messen ———
$kernel = $app->make(HttpKernel::class);
$employee = User::query()->where('email', 'person1234@example.org')->firstOrFail();
$admin = User::query()->where('email', 'admin@example.com')->firstOrFail();
$post = DB::table('news_posts')->where('slug', 'beitrag-'.($counts['news'] - 1))->value('slug');
$page = DB::table('pages')->whereNotNull('parent_id')->orderByDesc('id')->value('path');
$folder = DB::table('qm_folders')->where('id', 25)->value('id');
$downloadFolder = 45;
$targets = [
['Startseite', '/', $employee],
['Neuigkeiten', '/neuigkeiten', $employee],
['Beitrag mit 300 Kommentaren', "/neuigkeiten/{$post}", $employee],
['Termine', '/termine', $employee],
['Personen', '/personen', $employee],
['Personensuche „nachname12“', '/personen?q=nachname12', $employee],
['Kontaktkarten live', '/personen/karten?q=nachname12', $employee],
['Profil', '/personen/1234', $employee],
['Organigramm', '/personen/organigramm', $employee],
['Globale Suche live', '/suche/live?q=sommerfest', $employee],
['Suchseite', '/suche?q=sommerfest+team', $employee],
['Seite (tief im Baum)', "/seiten/{$page}", $employee],
['Downloads', '/downloads', $employee],
['Download-Ordner', "/downloads/ordner/{$downloadFolder}", $employee],
['QM Startseite', '/qm', $employee],
['QM Ordner', "/qm/ordner/{$folder}", $employee],
['QM Volltextsuche', '/qm/suche?q=hygiene+datenschutz', $employee],
['Gemeinsam', '/gemeinsam', $employee],
['Redaktion', '/redaktion', $admin],
['Admin: Übersicht', '/admin', $admin],
['Admin: Benutzer', '/admin/benutzer', $admin],
['Admin: Neuigkeiten', '/admin/neuigkeiten', $admin],
['Admin: Seiten', '/admin/seiten', $admin],
];
printf("%-32s %9s %9s %8s\n", 'Seite', 'erster', 'danach', 'Abfragen');
echo str_repeat('─', 62)."\n";
foreach ($targets as [$label, $url, $user]) {
$times = [];
$queries = 0;
foreach (range(1, 3) as $run) {
DB::flushQueryLog();
DB::enableQueryLog();
auth()->setUser($user);
$request = Request::create($url, 'GET');
if (str_contains($url, '/live') || str_contains($url, '/karten')) {
$request->headers->set('Accept', 'application/json');
}
$request->setLaravelSession($app['session']->driver());
$request->setUserResolver(fn () => $user);
$start = microtime(true);
$response = $kernel->handle($request);
$times[] = (microtime(true) - $start) * 1000;
$queries = count(DB::getQueryLog());
// LOADTEST_DUMP="Teil der Bezeichnung": häufigste Abfragen dieser Seite zeigen
if (($dump = getenv('LOADTEST_DUMP')) && str_contains($label, $dump) && $run === 3) {
$grouped = collect(DB::getQueryLog())->groupBy(fn ($q) => preg_replace('/\d+/', '?', $q['query']))->map->count()->sortDesc()->take(8);
foreach ($grouped as $sql => $n) {
echo " {$n}× ".mb_strimwidth($sql, 0, 160)."\n";
}
}
$kernel->terminate($request, $response);
if ($response->getStatusCode() >= 400 && $run === 1) {
echo " ! {$label}: HTTP {$response->getStatusCode()}\n";
}
}
printf("%-32s %7.0f ms %7.0f ms %8d\n", mb_strimwidth($label, 0, 32), $times[0], min($times[1], $times[2]), $queries);
}