Files
chat/backend/tests/stability_1000_round.php
2026-07-22 10:18:59 +08:00

176 lines
5.3 KiB
PHP

<?php
declare(strict_types=1);
require dirname(__DIR__) . '/vendor/autoload.php';
$rounds = max(1, (int) ($_SERVER['argv'][1] ?? 1000));
$suite = strtolower(trim((string) ($_SERVER['argv'][2] ?? 'all')));
$allowedSuites = ['all', 'agent', 'image', 'llm'];
if (!in_array($suite, $allowedSuites, true)) {
fwrite(STDERR, 'Usage: php backend/tests/stability_1000_round.php [rounds] [all|agent|image|llm]' . PHP_EOL);
exit(2);
}
$scripts = [
'agent' => [
'rounds_per_run' => 100,
'command' => 'php tests/agent_100_round_regression.php',
],
'llm' => [
'rounds_per_run' => 100,
'command' => 'php tests/llm_100_round_regression.php',
],
];
$root = dirname(__DIR__);
$exitCode = 0;
$runCommand = static function (string $command, string $workingDir): array {
$descriptorSpec = [
0 => ['pipe', 'r'],
1 => ['pipe', 'w'],
2 => ['redirect', 1],
];
$process = proc_open($command, $descriptorSpec, $pipes, $workingDir);
if (!is_resource($process)) {
throw new RuntimeException('Unable to start command: ' . $command);
}
fclose($pipes[0]);
$stdout = stream_get_contents($pipes[1]);
fclose($pipes[1]);
$status = proc_close($process);
return [
'stdout' => $stdout === false ? '' : $stdout,
'status' => $status,
];
};
$runFixedSuite = static function (string $name, int $targetRounds, array $config) use ($root, $runCommand): int {
$completed = 0;
$runIndex = 0;
while ($completed < $targetRounds) {
$runIndex++;
$result = $runCommand($config['command'], $root);
$output = trim($result['stdout']);
if ($output !== '') {
echo $output . PHP_EOL;
}
if ((int) $result['status'] !== 0) {
throw new RuntimeException(sprintf(
'%s suite failed on batch %d after %d rounds',
strtoupper($name),
$runIndex,
$completed
));
}
$completed += (int) $config['rounds_per_run'];
printf(
"%s PROGRESS %d/%d rounds\n",
strtoupper($name),
min($completed, $targetRounds),
$targetRounds
);
}
return $completed;
};
$runImageSuite = static function (int $targetRounds) use ($root, $runCommand): int {
$bestRegressionCount = null;
$bestIntegrationCount = null;
for ($regressionCount = intdiv($targetRounds, 79); $regressionCount >= 0; $regressionCount--) {
$remaining = $targetRounds - ($regressionCount * 79);
if ($remaining < 0 || $remaining % 2 !== 0) {
continue;
}
$bestRegressionCount = $regressionCount;
$bestIntegrationCount = intdiv($remaining, 2);
break;
}
if ($bestRegressionCount === null || $bestIntegrationCount === null) {
throw new RuntimeException('Unable to build an image-suite plan for ' . $targetRounds . ' rounds');
}
$completed = 0;
for ($index = 1; $index <= $bestRegressionCount; $index++) {
$result = $runCommand('php tests/comfy_image_edit_regression.php', $root);
$output = trim($result['stdout']);
if ($output !== '') {
echo $output . PHP_EOL;
}
if ((int) $result['status'] !== 0) {
throw new RuntimeException(sprintf(
'IMAGE regression batch %d failed after %d rounds',
$index,
$completed
));
}
$completed += 79;
printf("IMAGE PROGRESS %d/%d rounds\n", min($completed, $targetRounds), $targetRounds);
}
for ($index = 1; $index <= $bestIntegrationCount; $index++) {
$result = $runCommand('php tests/comfy_image_edit_integration.php', $root);
$output = trim($result['stdout']);
if ($output !== '') {
echo $output . PHP_EOL;
}
if ((int) $result['status'] !== 0) {
throw new RuntimeException(sprintf(
'IMAGE integration batch %d failed after %d rounds',
$index,
$completed
));
}
$completed += 2;
printf("IMAGE PROGRESS %d/%d rounds\n", min($completed, $targetRounds), $targetRounds);
}
return $completed;
};
$targets = $suite === 'all' ? ['agent', 'image', 'llm'] : [$suite];
$summary = [];
foreach ($targets as $target) {
try {
if ($target === 'image') {
$completed = $runImageSuite($rounds);
} else {
$completed = $runFixedSuite($target, $rounds, $scripts[$target]);
}
$summary[$target] = ['completed' => $completed, 'failed' => false];
} catch (Throwable $exception) {
$summary[$target] = [
'completed' => $summary[$target]['completed'] ?? 0,
'failed' => true,
'message' => $exception->getMessage(),
];
fwrite(STDERR, strtoupper($target) . ' FAILURE: ' . $exception->getMessage() . PHP_EOL);
$exitCode = 1;
break;
}
}
foreach ($summary as $name => $item) {
printf(
"%s RESULT completed=%d failed=%s%s\n",
strtoupper($name),
(int) ($item['completed'] ?? 0),
($item['failed'] ?? false) ? 'true' : 'false',
!empty($item['message']) ? ' message=' . $item['message'] : ''
);
}
exit($exitCode);