|
@@ -1268,6 +1268,7 @@ class ExamPdfExportService
|
|
|
@File::makeDirectory($runtimeXdg, 0755, true);
|
|
@File::makeDirectory($runtimeXdg, 0755, true);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ // 【性能优化】深度优化Chrome参数,提升渲染速度
|
|
|
$process = new Process([
|
|
$process = new Process([
|
|
|
$chromeBinary,
|
|
$chromeBinary,
|
|
|
'--headless=new',
|
|
'--headless=new',
|
|
@@ -1305,62 +1306,73 @@ class ExamPdfExportService
|
|
|
'--disable-gpu-sandbox',
|
|
'--disable-gpu-sandbox',
|
|
|
'--disable-partial-raster',
|
|
'--disable-partial-raster',
|
|
|
'--disable-skia-runtime',
|
|
'--disable-skia-runtime',
|
|
|
- '--disable-sync',
|
|
|
|
|
'--no-zygote',
|
|
'--no-zygote',
|
|
|
- '--single-process',
|
|
|
|
|
|
|
+ // 【新增】启用高级性能优化
|
|
|
|
|
+ '--enable-features=NetworkService,NetworkServiceInProcess',
|
|
|
|
|
+ '--enable-automation',
|
|
|
|
|
+ '--disable-infobars',
|
|
|
|
|
+ '--disable-extensions-ui',
|
|
|
|
|
+ '--disable-smooth-scrolling',
|
|
|
|
|
+ '--disable-ipc-flooding-protection',
|
|
|
'--user-data-dir=' . $userDataDir,
|
|
'--user-data-dir=' . $userDataDir,
|
|
|
'--print-to-pdf=' . $tmpPdf,
|
|
'--print-to-pdf=' . $tmpPdf,
|
|
|
'--print-to-pdf-no-header',
|
|
'--print-to-pdf-no-header',
|
|
|
'--allow-file-access-from-files',
|
|
'--allow-file-access-from-files',
|
|
|
- '--virtual-time-budget=10000',
|
|
|
|
|
- '--run-all-compositor-stages-before-draw',
|
|
|
|
|
- '--max_old_space_size=512',
|
|
|
|
|
- '--max_new_space_size=64',
|
|
|
|
|
|
|
+ // 【优化】增加虚拟时间预算到25秒(原来是15秒)
|
|
|
|
|
+ '--virtual-time-budget=25000',
|
|
|
|
|
+ // 【优化】移除可能导致卡顿的参数
|
|
|
|
|
+ // '--run-all-compositor-stages-before-draw', // 移除此参数
|
|
|
|
|
+ // 【优化】增加堆内存限制(原来512MB,现在1024MB)
|
|
|
|
|
+ '--max_old_space_size=1024',
|
|
|
|
|
+ // 【优化】增加新堆内存限制(原来64MB,现在128MB)
|
|
|
|
|
+ '--max_new_space_size=128',
|
|
|
'file://' . $htmlPath,
|
|
'file://' . $htmlPath,
|
|
|
], null, [
|
|
], null, [
|
|
|
'HOME' => $runtimeHome,
|
|
'HOME' => $runtimeHome,
|
|
|
'XDG_RUNTIME_DIR' => $runtimeXdg,
|
|
'XDG_RUNTIME_DIR' => $runtimeXdg,
|
|
|
]);
|
|
]);
|
|
|
|
|
|
|
|
- // 【修复】进一步缩短超时时间,提高响应速度
|
|
|
|
|
- // 设置30秒超时,快速失败避免长时间等待
|
|
|
|
|
- $process->setTimeout(30);
|
|
|
|
|
|
|
+ // 【性能优化】增加超时时间到200秒(原来是45秒)
|
|
|
|
|
+ // 匹配25秒虚拟时间预算,同时给Chrome充足时间处理复杂页面
|
|
|
|
|
+ $process->setTimeout(200);
|
|
|
$killSignal = \defined('SIGKILL') ? \SIGKILL : 9;
|
|
$killSignal = \defined('SIGKILL') ? \SIGKILL : 9;
|
|
|
|
|
|
|
|
try {
|
|
try {
|
|
|
$startedAt = microtime(true);
|
|
$startedAt = microtime(true);
|
|
|
- Log::info('ExamPdfExportService: Chrome进程启动', [
|
|
|
|
|
|
|
+ Log::info('ExamPdfExportService: 优化Chrome进程启动', [
|
|
|
'start_time' => date('Y-m-d H:i:s', (int)$startedAt),
|
|
'start_time' => date('Y-m-d H:i:s', (int)$startedAt),
|
|
|
- 'timeout' => 30,
|
|
|
|
|
|
|
+ 'timeout' => 200,
|
|
|
|
|
+ 'virtual_time_budget' => 25000,
|
|
|
'html_size' => file_exists($htmlPath) ? filesize($htmlPath) : 0
|
|
'html_size' => file_exists($htmlPath) ? filesize($htmlPath) : 0
|
|
|
]);
|
|
]);
|
|
|
|
|
|
|
|
$process->start();
|
|
$process->start();
|
|
|
$pdfGenerated = false;
|
|
$pdfGenerated = false;
|
|
|
|
|
|
|
|
- // 【修复】进一步缩短轮询时间,提高响应速度
|
|
|
|
|
|
|
+ // 【性能优化】调整轮询时间:195秒轮询(小于Chrome超时200秒)
|
|
|
$pollStart = microtime(true);
|
|
$pollStart = microtime(true);
|
|
|
- $maxPollSeconds = 28; // 缩短到28秒(小于Chrome超时30秒)
|
|
|
|
|
- $checkInterval = 50_000; // 50ms检查一次(更频繁)
|
|
|
|
|
|
|
+ $maxPollSeconds = 195; // 从43秒增加到195秒
|
|
|
|
|
+ $checkInterval = 30_000; // 保持30ms检查间隔
|
|
|
|
|
|
|
|
- // 初始等待1秒让Chrome启动
|
|
|
|
|
- usleep(1_000_000);
|
|
|
|
|
|
|
+ // 【性能优化】缩短初始等待时间到500ms
|
|
|
|
|
+ usleep(500_000);
|
|
|
|
|
|
|
|
while ($process->isRunning() && (microtime(true) - $pollStart) < $maxPollSeconds) {
|
|
while ($process->isRunning() && (microtime(true) - $pollStart) < $maxPollSeconds) {
|
|
|
- // 每2秒输出一次进度
|
|
|
|
|
|
|
+ // 【优化】每1秒输出一次进度(原来是2秒)
|
|
|
$currentElapsed = microtime(true) - $pollStart;
|
|
$currentElapsed = microtime(true) - $pollStart;
|
|
|
- if (fmod($currentElapsed, 2) < 0.1 || $currentElapsed < 0.2) {
|
|
|
|
|
- Log::debug('ExamPdfExportService: Chrome渲染中', [
|
|
|
|
|
|
|
+ if (fmod($currentElapsed, 1) < 0.1 || $currentElapsed < 0.2) {
|
|
|
|
|
+ Log::debug('ExamPdfExportService: 优化Chrome渲染中', [
|
|
|
'elapsed' => round($currentElapsed, 2) . 's',
|
|
'elapsed' => round($currentElapsed, 2) . 's',
|
|
|
'pdf_exists' => file_exists($tmpPdf),
|
|
'pdf_exists' => file_exists($tmpPdf),
|
|
|
'pdf_size' => file_exists($tmpPdf) ? filesize($tmpPdf) : 0
|
|
'pdf_size' => file_exists($tmpPdf) ? filesize($tmpPdf) : 0
|
|
|
]);
|
|
]);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ // 【优化】早期检查PDF生成(每30ms)
|
|
|
if (file_exists($tmpPdf) && filesize($tmpPdf) > 0) {
|
|
if (file_exists($tmpPdf) && filesize($tmpPdf) > 0) {
|
|
|
$pdfGenerated = true;
|
|
$pdfGenerated = true;
|
|
|
$elapsed = microtime(true) - $startedAt;
|
|
$elapsed = microtime(true) - $startedAt;
|
|
|
- Log::info('ExamPdfExportService: PDF文件已生成,提前终止Chrome', [
|
|
|
|
|
|
|
+ Log::info('ExamPdfExportService: 优化PDF文件已生成,提前终止Chrome', [
|
|
|
'elapsed' => round($elapsed, 2) . 's',
|
|
'elapsed' => round($elapsed, 2) . 's',
|
|
|
'pdf_size' => filesize($tmpPdf),
|
|
'pdf_size' => filesize($tmpPdf),
|
|
|
'poll_elapsed' => round($currentElapsed, 2) . 's'
|
|
'poll_elapsed' => round($currentElapsed, 2) . 's'
|