diff --git a/AGENTS.md b/AGENTS.md index ae36aef..cf44474 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -34,7 +34,7 @@ E-Mail anlegen, kein Login, Verwaltung über einen geheimen Admin-Link, Teilnahm | Coding Standard | `vendor/bin/phpcs` / Autofix `vendor/bin/phpcbf` | | Neue Migration | `bin/cake bake migration NameDerMigration` | | Verwaiste Folienbilder löschen | `bin/cake images_cleanup [--dry-run]` | -| Umfragen verwalten (Admin-CLI) | `bin/cake polls list\|show\|delete\|remind\|cleanup` (siehe [docs/operations.md](docs/operations.md)) | +| Umfragen verwalten (Admin-CLI) | `bin/cake polls list\|show\|delete\|remind\|cleanup\|seed` (siehe [docs/operations.md](docs/operations.md)) | **Vor jedem Commit:** `vendor/bin/phpunit` und `vendor/bin/phpcs` müssen grün sein (Warnungen zur Zeilenlänge bei langen deutschen Texten sind ok). @@ -70,7 +70,7 @@ src/ QaService, SlideControlService, ResetService, ReportService Model/ Polls → Slides → SlideOptions, Slides → Responses Middleware/ HostHeader, SecurityHeaders (CSP mit Nonce, Framing, Referrer-Policy) - Command/ images_cleanup, polls list|show|delete|remind|cleanup + Command/ images_cleanup, polls list|show|delete|remind|cleanup|seed Mailer/ PollMailer templates/ Deutsche UI; Layouts: default, present, error, email, fragment, pdf webroot/ css/plenario.css (eigenes CSS, Tokens), js/plenario.js (Vanilla JS) diff --git a/docs/development.md b/docs/development.md index 4a3caab..603275b 100644 --- a/docs/development.md +++ b/docs/development.md @@ -31,3 +31,20 @@ vendor/bin/phpcs # CakePHP Coding Standard (Autofix: vendor/bin/phpcbf) ``` Vor jedem Commit müssen beide grün sein. `main` ist geschützt: Änderungen auf einem eigenen Branch pushen und per Pull Request mergen. + +## Lasttest (JMeter) + +`tests/load/plenario.jmx` simuliert Teilnehmende: Seite `/p/{code}` laden, kurz lesen, einmal auf der aktuellen +Auswahlfrage abstimmen (CSRF-Token, Folien- und Options-ID werden aus der Seite gelesen) und danach im +`Plenario.pollIntervalMs`-Takt `/p/{code}/state` abfragen. Jeder Thread hat eigene Cookies, ist also ein eigener Teilnehmer. + +```bash +jmeter -n -t tests/load/plenario.jmx -Jhost=localhost -Jport=8765 -Jcode=ABCD \ + -Jusers=200 -Jrampup=60 -Jduration=300 -l tmp/load.jtl -e -o tmp/load-report +``` + +Parameter: `protocol` (http), `host`, `port`, `code` (Join-Code), `users`, `rampup` (s), `duration` (s), `pollms` (2000). +Passende Testdaten legt `bin/cake polls seed` an: jeder Aufruf erzeugt eine **neue** Umfrage (neue ID und neuer Code) +mit je einer Folie pro interaktivem Typ; die erste Folie (Auswahlfrage) ist live und offen. `--json` liefert Code und +Admin-Link für Skripte, `--title`/`--email` passen die Daten an. Aufräumen: `bin/cake polls delete -y`. Alle Last kommt von einer IP: bei +`THROTTLE_VOTES_PER_MINUTE` (Standard 600) antwortet die Abstimmung sonst mit 429. Nur gegen eigene Instanzen testen. diff --git a/src/Command/PollsSeedCommand.php b/src/Command/PollsSeedCommand.php new file mode 100644 index 0000000..d998148 --- /dev/null +++ b/src/Command/PollsSeedCommand.php @@ -0,0 +1,133 @@ +setDescription(__('Legt eine neue Demo-Umfrage mit Beispielfolien an (z. B. für den Lasttest). Jeder Aufruf erzeugt eine neue Umfrage.')) + ->addOption('title', [ + 'help' => __('Titel der Umfrage.'), + 'default' => 'Lasttest', + ]) + ->addOption('email', [ + 'help' => __('E-Mail-Adresse des Besitzers (es wird nichts verschickt).'), + 'default' => 'lasttest@example.org', + ]) + ->addOption('json', [ + 'help' => __('Ausgabe als JSON statt als Text (für Skripte), inkl. Admin-Link.'), + 'boolean' => true, + ]); + } + + /** + * @param \Cake\Console\Arguments $args Arguments + * @param \Cake\Console\ConsoleIo $io Console + * @return int + */ + public function execute(Arguments $args, ConsoleIo $io): int + { + $polls = $this->polls(); + $slides = $this->fetchTable('Slides'); + + $result = $polls->getConnection()->transactional(function () use ($polls, $slides, $args): array { + [$poll, $token] = $polls->createPoll([ + 'title' => (string)$args->getOption('title'), + 'email' => (string)$args->getOption('email'), + ]); + if ($token === null) { + return [$poll, null, 0]; + } + + $first = null; + $count = 0; + foreach ($this->definitions() as [$type, $question, $options, $correct]) { + $slide = $slides->newEntity([ + 'type' => $type, + 'question' => $question, + 'settings' => Slide::DEFAULT_SETTINGS[$type] ?? [], + ]); + $slide->poll_id = $poll->id; + $slides->saveOrFail($slide); + if ($options !== []) { + $slides->syncOptions($slide, $options, $correct); + } + $first ??= $slide; + $count++; + } + + $poll->showSlide($first->id); + $polls->saveOrFail($poll); + + return [$poll, $token, $count]; + }); + + [$poll, $token, $count] = $result; + if ($token === null) { + $io->err(__('Umfrage konnte nicht angelegt werden: {0}', (string)json_encode($poll->getErrors()))); + + return static::CODE_ERROR; + } + + $adminUrl = Router::url(['_name' => 'admin', 'token' => $token], true); + if ($args->getOption('json')) { + $this->printJson($io, $this->pollData($poll) + ['slides' => $count, 'adminUrl' => $adminUrl]); + + return static::CODE_SUCCESS; + } + + $io->out([ + __('Umfrage #{0} angelegt mit {1} Folien (erste Folie live, Abstimmung offen).', $poll->id, $count), + __('Beitrittscode: {0}', $poll->join_code), + __('Teilnahme-Link: {0}', $this->joinLink($poll)), + __('Admin-Link: {0}', $adminUrl), + __('Lasttest: jmeter -n -t tests/load/plenario.jmx -Jcode={0} …', $poll->join_code), + ]); + + return static::CODE_SUCCESS; + } + + /** + * @return list, 3: list}> Type, question, options, correct options + */ + protected function definitions(): array + { + return [ + [Slide::TYPE_CHOICE, 'Wie gefällt dir PlenarIO?', ['Super', 'Gut', 'Geht so', 'Gar nicht'], []], + [Slide::TYPE_WORDCLOUD, 'Was fällt dir zu Live-Umfragen ein?', [], []], + [Slide::TYPE_OPEN, 'Was wünschst du dir für heute?', [], []], + [Slide::TYPE_SCALE, 'Wie zufrieden bist du?', [], []], + [Slide::TYPE_QUIZ, 'Wie viele Kontinente gibt es?', ['7', '5', '6', '8'], ['7']], + [Slide::TYPE_QA, 'Stell deine Frage', [], []], + [Slide::TYPE_ORDER, 'Bringe die Planeten in die richtige Reihenfolge', ['Merkur', 'Venus', 'Erde', 'Mars'], []], + ]; + } +} diff --git a/tests/TestCase/Command/PollsCommandsTest.php b/tests/TestCase/Command/PollsCommandsTest.php index 4c6f90a..e7b4192 100644 --- a/tests/TestCase/Command/PollsCommandsTest.php +++ b/tests/TestCase/Command/PollsCommandsTest.php @@ -51,6 +51,24 @@ class PollsCommandsTest extends TestCase $this->assertTrue($this->pollsTable()->get($poll->id)->isExpired()); } + public function testSeedCreatesNewPollWithLiveFirstSlideEveryTime(): void + { + $this->exec('polls seed --json'); + $this->assertExitSuccess(); + $first = json_decode(implode('', $this->_out->messages()), true); + $this->exec('polls seed --json'); + $second = json_decode(implode('', $this->_out->messages()), true); + + $this->assertNotSame($first['id'], $second['id']); + $this->assertNotSame($first['joinCode'], $second['joinCode']); + $this->assertSame(7, $second['slides']); + + $poll = $this->pollsTable()->get($second['id']); + $slide = $this->slidesTable()->get($poll->current_slide_id); + $this->assertSame('choice', $slide->type); + $this->assertTrue($slide->isVotingOpen()); + } + public function testSettingsAreRespected(): void { [$poll] = $this->createPoll(); diff --git a/tests/load/plenario.jmx b/tests/load/plenario.jmx new file mode 100644 index 0000000..28bb3a8 --- /dev/null +++ b/tests/load/plenario.jmx @@ -0,0 +1,357 @@ + + + + + false + false + + + + PROTOCOL + ${__P(protocol,http)} + = + + + HOST + ${__P(host,localhost)} + = + + + PORT + ${__P(port,8765)} + = + + + CODE + ${__P(code,ABCD)} + = + + + POLL_MS + ${__P(pollms,2000)} + = + + + + + + + continue + + -1 + false + + ${__P(users,100)} + ${__P(rampup,60)} + true + ${__P(duration,300)} + + true + + + + + + + ${HOST} + ${PORT} + ${PROTOCOL} + UTF-8 + 10000 + 30000 + + + + + false + false + + + + + + User-Agent + JMeter-Lasttest + + + + + + + + PAGE_VERSION + NONE + = + + + VOTE_YES + 0 + = + + + + + + + + + + + /p/${CODE}/state + GET + false + true + + + + + + Accept + application/json + + + X-Requested-With + XMLHttpRequest + + + + + + + 200 + + + Assertion.response_code + false + 8 + + + + VERSION + $.version + 1 + NOT_FOUND + + + + ${POLL_MS} + 500 + + + + + + + ${__jexl3("${VERSION}" != "${PAGE_VERSION}" && "${VERSION}" != "NOT_FOUND")} + false + true + + + + + + + /p/${CODE} + GET + true + true + + + + + 200 + + + Assertion.response_code + false + 8 + + + + false + CSRF + name="csrf-token" content="([^"]+)" + $1$ + NOT_FOUND + 1 + + + + + groovy + + + true + def m = (html =~ re); m.find() ? m.group(1) : null } + +vars.put('PAGE_VERSION', first(/data-version="([^"]*)"/) ?: 'NOT_FOUND') + +def words = ['Zukunft', 'Teamgeist', 'Digital', 'Nachhaltig', 'Agil', 'Vertrauen', 'Mut', 'Tempo', 'Kommunikation', + 'Fortschritt', 'Respekt', 'Kreativ', 'Klarheit', 'Wandel', 'Zusammenhalt', 'Qualität'] +def texts = ['Mehr Praxisbeispiele bitte', 'Gerne kürzere Pausen', 'Alles gut so', 'Mehr Zeit für Fragen', + 'Das Thema ist spannend', 'Bitte langsamer sprechen'] + +def slideId = first(/name="slide_id"[^>]*value="(\d+)"/) +def parts = null +def kind = '' + +// Wer schon geantwortet hat, sieht kein Formular mehr (nur "Deine Antwort ist angekommen"). +if (slideId != null && !html.contains('class="done"')) { + def order = [] + def m = (html =~ /name="order\[\]" value="(\d+)"/) + while (m.find()) { order << m.group(1) } + def options = [] + m = (html =~ /name="(option_ids?(?:\[\])?)" value="(\d+)"/) + while (m.find()) { options << [m.group(1), m.group(2)] } + def values = [] + m = (html =~ /name="value" value="(\d+)"/) + while (m.find()) { values << m.group(1) } + + if (order) { + Collections.shuffle(order, rnd) + parts = order.collect { 'order%5B%5D=' + it } + kind = 'order' + } else if (options) { + def pick = options[rnd.nextInt(options.size())] + parts = [(pick[0].startsWith('option_ids') ? 'option_ids%5B%5D=' : 'option_id=') + pick[1]] + kind = 'choice' + } else if (values) { + parts = ['value=' + values[rnd.nextInt(values.size())]] + kind = 'scale' + } else if (html.contains('name="word"')) { + parts = ['word=' + words[rnd.nextInt(words.size())]] + kind = 'wordcloud' + } else if (html.contains('name="question"')) { + parts = ['question=' + java.net.URLEncoder.encode('Frage ' + rnd.nextInt(100000) + '?', 'UTF-8')] + kind = 'qa' + } else if (html.contains('name="text"')) { + parts = ['text=' + java.net.URLEncoder.encode(texts[rnd.nextInt(texts.size())], 'UTF-8')] + kind = 'open' + } +} + +if (parts != null) { + vars.put('VOTE_BODY', (['slide_id=' + slideId] + parts).join('&')) + vars.put('VOTE_KIND', kind) + vars.put('VOTE_YES', '1') +} else { + vars.put('VOTE_YES', '0') +} +]]> + + + + + + 1 + 0 + 0 + + + + 2000 + 6000 + + + + + + ${__jexl3("${VOTE_YES}" == "1")} + false + true + + + + true + + + + false + ${VOTE_BODY} + = + + + + /p/${CODE}/vote + POST + false + true + + + + + + Content-Type + application/x-www-form-urlencoded; charset=UTF-8 + + + X-CSRF-Token + ${CSRF} + + + X-Requested-With + XMLHttpRequest + + + + + + + + "ok":true + nicht mehr aktiv + + + Assertion.response_data + false + 34 + + + + + + + + + false + + saveConfig + + + true + true + true + + true + true + true + true + false + true + true + false + false + false + true + false + false + false + true + 0 + true + true + true + true + true + true + + + + + + + +