winzik/apps/winzik/tests/PlaylistBigTest.php
David Nurdin 3946de1022 Playlist : rapide avec 6 000 morceaux, clavier, Hasard sans répétition
- Plus de travail proportionnel à la liste à chaque image : recherche, dossiers et
  décalage des lignes longues gardés en cache (la liste comparée par identité, plus
  de md5 de toute la liste à chaque appel), tags pris au plus deux fois par seconde,
  recherche affinée lettre par lettre, sélection sans entrées fausses (count direct).
- Playlist::remove en un passage (ensemble des lignes) au lieu de in_array par ligne.
- Clavier (PlaylistWindow::keys) : flèches, Page préc./suiv., Début/Fin, Entrée,
  Suppr, Ctrl+A, Ctrl+F, Ctrl+J, Ctrl+flèche (déplacer le morceau), Maj+flèches en
  mode édition, une lettre ouvre la recherche ; ligne du clavier en pointillés.
- Double clic en mode édition : joue la ligne ; clic sur le compteur du pied :
  montre le morceau joué ; messages « Liste vide » et « Aucun morceau ne contient ».
- Hasard : jamais deux fois le même morceau de suite ; Précédent revient aux morceaux
  réellement joués (historique suivi par remove / move).
- Tests : PlaylistBigTest (mesures, 6 000 morceaux), PlaylistKeysTest.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-20 06:41:02 +01:00

189 lines
7.3 KiB
PHP

<?php
namespace Tests;
use App\Gui\PlaylistWindow;
/**
* A play list of 6 000 songs (fake paths: nothing is read): drawing it, scrolling, searching, sorting, choosing and
* removing lines stay cheap. Each test measures with microtime() and writes its numbers on the error output
* ("[perf] ..."), then checks a budget far above what is measured (a slow test machine passes, an O(n) per frame fails).
*/
final class PlaylistBigTest extends PlayerTestCase
{
private const SONGS = 6000;
/** @return list<string> */
private static function songs(int $n): array
{
$files = [];
for ($i = 0; $i < $n; $i++) {
$files[] = sprintf('/musique/Artiste %03d/Album %02d/Artiste %03d - Chanson numero %d.mp3', $i % 300, $i % 7, $i % 300, $i);
}
return $files;
}
private function bigList(): PlaylistWindow
{
$this->startPlayer();
$list = $this->desk->playlist;
self::assertNotNull($list);
$this->wz->playlist->clear();
$this->wz->playlist->addAll(self::songs(self::SONGS));
$this->game->run(3);
return $list;
}
/** Milliseconds per frame over $frames frames (the whole frame: update and drawing). */
private function frameMs(int $frames = 30): float
{
$t = microtime(true);
$this->game->run($frames);
return (microtime(true) - $t) * 1000 / $frames;
}
/** Milliseconds to draw the play list window alone (the mean of $times drawings). */
private function drawMs(PlaylistWindow $list, int $times = 50): float
{
$engine = $this->form->engineOrNull();
self::assertNotNull($engine);
$c = new \App\Gui\Canvas($engine->sdl->getRenderer(), $engine->sdl);
$c->skin = $this->wz->skin;
$list->render($c);
$t = microtime(true);
for ($i = 0; $i < $times; $i++) {
$list->render($c);
}
return (microtime(true) - $t) * 1000 / $times;
}
private static function report(string $what, float $ms): void
{
fwrite(STDERR, sprintf("[perf] %-46s %8.3f ms\n", $what, $ms));
}
public function testAFrameWithSixThousandSongsCostsWhatAFrameWithFiftySongsCosts(): void
{
$this->startPlayer();
$list = $this->desk->playlist;
self::assertNotNull($list);
$this->wz->playlist->clear();
$this->wz->playlist->addAll(self::songs(50)); // every line of the window filled
$this->game->run(5);
$small = $this->frameMs();
$smallDraw = $this->drawMs($list);
$this->wz->playlist->clear();
$this->wz->playlist->addAll(self::songs(self::SONGS));
$this->game->run(3);
$big = $this->frameMs();
$bigDraw = $this->drawMs($list);
self::report('frame, 50 songs', $small);
self::report('frame, ' . self::SONGS . ' songs', $big);
self::report('play list drawn, 50 songs', $smallDraw);
self::report('play list drawn, ' . self::SONGS . ' songs', $bigDraw);
self::assertLessThan($small * 2 + 5, $big, 'no work proportional to the list in a frame');
self::assertLessThan($smallDraw * 2 + 1, $bigDraw, 'only the lines shown are drawn');
}
public function testScrollingTheWholeListWithTheWheelStaysCheap(): void
{
$list = $this->bigList();
$t = microtime(true);
for ($i = 0; $i < 200; $i++) {
$list->wheel(-1);
$this->game->run(1);
}
$ms = (microtime(true) - $t) * 1000 / 200;
self::report('wheel notch + frame', $ms);
self::assertSame(600, $list->top(), '200 notches of 3 lines');
self::assertLessThan(40, $ms);
}
public function testSearchingSixThousandSongsThenDrawingTheResultIsCheap(): void
{
$list = $this->bigList();
$list->toggleSearch();
$t = microtime(true);
foreach (str_split('numero 12') as $char) {
$list->searchKey(null, $char);
}
$first = (microtime(true) - $t) * 1000;
self::report('search, 9 letters typed', $first);
self::assertSame(111, $list->lineCount(), '12, 120..129, 1200..1299 and 12xx: 1 + 10 + 100');
$frame = $this->frameMs();
self::report('frame while searching', $frame);
self::report('play list drawn while searching', $this->drawMs($list));
self::assertLessThan(10, $this->drawMs($list));
self::assertLessThan(40, $frame);
$list->searchKey(\JumPHP\Gui\Pointer::BACKSPACE, '');
self::assertGreaterThan(111, $list->lineCount());
}
public function testTheListByFoldersIsBuiltOnceThenDrawnCheaply(): void
{
$list = $this->bigList();
$this->wz->ui['playlist']['grouped'] = true;
$t = microtime(true);
$this->game->run(1);
self::report('by folders, first frame (built)', (microtime(true) - $t) * 1000);
self::assertSame(300, $list->lineCount(), 'the 300 artists');
$frame = $this->frameMs();
self::report('frame by folders', $frame);
self::report('play list drawn by folders', $this->drawMs($list));
self::assertLessThan(10, $this->drawMs($list));
self::assertLessThan(40, $frame);
}
public function testThePointerAtTheRightEdgeShiftsTheLinesWithoutReadingTheWholeListEachTime(): void
{
$list = $this->bigList();
$list->hover($list->width() - 5, PlaylistWindow::LIST_TOP + 5);
$list->update(0.1);
$t = microtime(true);
for ($i = 0; $i < 50; $i++) {
$list->hover($list->width() - 5, PlaylistWindow::LIST_TOP + 5);
$list->update(0.1);
}
$ms = (microtime(true) - $t) * 1000 / 50;
self::report('shift of the long lines (one step)', $ms);
self::assertGreaterThan(0, $this->wz->ui['playlist']['shift']);
self::assertLessThan(5, $ms);
}
public function testChoosingEveryLineThenRemovingHalfTheListIsCheap(): void
{
$list = $this->bigList();
$t = microtime(true);
$list->selectAll();
$select = (microtime(true) - $t) * 1000;
self::report('Ctrl+A (' . self::SONGS . ' lines chosen)', $select);
self::assertSame(self::SONGS, $list->chosenCount());
$frame = $this->frameMs();
self::report('frame, every line chosen', $frame);
self::assertLessThan(40, $frame);
$t = microtime(true);
$this->wz->removeLines(range(0, self::SONGS - 1, 2));
$remove = (microtime(true) - $t) * 1000;
self::report('remove ' . (self::SONGS / 2) . ' lines', $remove);
self::assertSame(self::SONGS / 2, $this->wz->playlist->count());
self::assertLessThan(500, $remove);
}
public function testSortingByPreferenceAndBackIsCheap(): void
{
$list = $this->bigList();
$t = microtime(true);
$this->wz->togglePreferenceOrder();
$sort = (microtime(true) - $t) * 1000;
$t = microtime(true);
$this->wz->togglePreferenceOrder();
$back = (microtime(true) - $t) * 1000;
self::report('sort by preference', $sort);
self::report('back to the order before', $back);
self::assertSame(self::songs(self::SONGS), $this->wz->playlist->items);
$frame = $this->frameMs();
self::report('frame after the sorts', $frame);
self::assertLessThan(40, $frame);
self::assertSame(0, $list->top());
}
}