rob-revisited/ide/tools/bench-text-index.php

45 lines
1.9 KiB
PHP

<?php
// Benchmark of the IDE's search engine (JumPHP\Ide\Search\TextIndex) on a folder (default: the repository):
// bin/jumphp php . ide/tools/bench-text-index.php [folder]
require __DIR__ . '/../vendor/autoload.php';
use JumPHP\Ide\Search\Search;
use JumPHP\Ide\Search\TextIndex;
use JumPHP\Ide\Workspace\LocalWorkspace;
$root = $argv[1] ?? dirname(__DIR__, 2);
$ws = new LocalWorkspace($root);
gc_collect_cycles();
$before = memory_get_usage();
$t = microtime(true);
$index = new TextIndex($ws);
$slices = 0;
while (!$index->refresh(0.006)) {
$slices++;
}
$build = microtime(true) - $t;
gc_collect_cycles();
$memory = memory_get_usage() - $before;
$s = $index->status();
printf("files %d, tokens %d, build %.2f s (%d slices of 6 ms), memory %.1f MB (estimate %.1f MB), peak %.1f MB\n",
$s['files'], $s['tokens'], $build, $slices + 1, $memory / 1048576, $s['memory'] / 1048576, memory_get_peak_usage() / 1048576);
$queries = ['TextIndex', 'search engine', 'isSkipped', 'Paths::isSkipped', '"never block the frame"', 'render*', 'scene path:games/pong',
'function ext:js', 'collision -tile', 'the', 'fillRect color', 'sdl_*', 'NetSession send', 'xyzzynothing'];
foreach ($queries as $q) {
$index->query($q, 50); // warm (the prefix vocabulary)
$times = [];
for ($i = 0; $i < 5; $i++) {
$r = $index->query($q, 50);
$times[] = $r['ms'];
}
sort($times);
printf("%-28s %5d hits median %6.2f ms top: %s:%d\n", $q, $r['total'], $times[2], $r['results'][0]['path'] ?? '-', $r['results'][0]['line'] ?? 0);
}
$t = microtime(true);
$found = (new Search($ws))->find('isSkipped');
printf("Find in Files (scan) 'isSkipped': %d matches, %.1f ms\n", $found['matches'], (microtime(true) - $t) * 1000);
$t = microtime(true);
foreach (array_slice(array_keys($ws->files()), 0, 50) as $path) {
$index->update($path);
}
printf("50 files updated: %.1f ms\n", (microtime(true) - $t) * 1000);