gpt4 book ai didi

PHP Phar 在功能强大的 PC 上创建缓慢,如何加快速度(加载/读取 ~3000 个文件)?

转载 作者:IT王子 更新时间:2023-10-28 23:47:54 30 4
gpt4 key购买 nike

我正在尝试使用 Phar 打包我的 Web 应用程序(Symfony 2 项目)。我已经在合理的时间内(1-2 分钟)成功打包了 Silex,这是一个包含数百个文件的微型框架。

问题出在我的开发机器上(i7 4770k,16GB,SSD Raid 0,RAM 磁盘上的项目)创建存档真的很慢,每个文件大约需要 1 秒。我真的需要找到一种方法来加快速度。

读取/加载文件的单次迭代很慢。我正在使用以下方式添加文件:

function addFile(Phar $phar, SplFileInfo $file)
{
$root = realpath(__DIR__.DIRECTORY_SEPARATOR.'..'.DIRECTORY_SEPARATOR);
$path = strtr(str_replace($root, '', $file->getRealPath()), '\\', '/');
$phar->addFromString($path, file_get_contents($file));
}

$phar = new Phar(/* ... */);
$phar->startBuffering();

// ...
foreach ($files as $file) {
addFile($phar, $file);
}

// ...
$phar->setStub(/* ... */);
$phar->stopBuffering();

如何加快读取/添加文件的速度?可能是我的操作系统 (Windows) 有问题吗?

编辑:禁用缓冲没有解决问题。从字符串添加的速度相同:

// This is VERY fast (~ 1 sec to read all 3000+ files)
$strings = array();
foreach ($files as $file) {
$root = realpath(__DIR__.DIRECTORY_SEPARATOR.'..'.DIRECTORY_SEPARATOR);
$path = strtr(str_replace($root, '', $file->getRealPath()), '\\', '/');

$strings[$path] = file_get_contents($file->getRealPath());
}

// This is SLOW
foreach ($strings as $local => $content) {
$phar->addFromString($local, $content);
}

编辑:完整的快速和脏脚本(可能有帮助)app/build:

#!/usr/bin/env php
<?php

set_time_limit(0);

require __DIR__.'/../vendor/autoload.php';

use Symfony\Component\Finder\Finder;
use Symfony\Component\Console\Input\ArgvInput;

$input = new ArgvInput();
$env = $input->getParameterOption(array('--env', '-e'), 'dev');

function addFile(Phar $phar, SplFileInfo $file)
{
$root = realpath(__DIR__.DIRECTORY_SEPARATOR.'..'.DIRECTORY_SEPARATOR);
$path = strtr(str_replace($root, '', $file->getRealPath()), '\\', '/');
$phar->addFromString($path, file_get_contents($file));
}

$phar = new Phar(__DIR__ . "/../symfony.phar", 0, "symfony.phar");
$phar->startBuffering();

$envexclude = array_diff(array('dev', 'prod', 'test'), array($env));

// App
$app = (new Finder())
->files()
->notPath('/cache/')
->notPath('/logs/')
->notName('build')
->notname('/._('.implode('|', $envexclude).')\.yml$/')
->in(__DIR__);

// Vendor
$vendor = (new Finder())
->files()
->ignoreVCS(true)
->name('*.{php,twig,xlf,xsd,xml}')
->notPath('/tests/i')
->notPath('/docs/i')
->in(__DIR__.'/../vendor');

// Src
$src = (new Finder())
->files()
->notPath('/tests/i')
->in(__DIR__.'/../src');

// Web
$web = (new Finder())
->files()
->in(__DIR__.'/../web')
->notname('/._('.implode('|', $envexclude).')\.php$/');

$all = array_merge(
iterator_to_array($app),
iterator_to_array($src),
iterator_to_array($vendor),
iterator_to_array($web)
);

$c = count($all);
$i = 1;
$strings = array();
foreach ($all as $file) {
addFile($phar, $file);
echo "Done $i/$c\r\n";
$i++;
}

$stub = <<<'STUB'
Phar::webPhar(null, "/web/app_phar.php", null, array(), function ($path) {
return '/web/app_phar.php'.$path;
});

__HALT_COMPILER();
STUB;

$phar->setStub($stub);
$phar->stopBuffering();

最佳答案

尝试使用 Phar::addFile($file)而不是 Phar::addFromString(file_get_contents($file))

function addFile(Phar $phar, SplFileInfo $file)
{
$root = realpath(__DIR__.DIRECTORY_SEPARATOR.'..'.DIRECTORY_SEPARATOR);
$path = strtr(str_replace($root, '', $file->getRealPath()), '\\', '/');
//$phar->addFromString($path, file_get_contents($file));
$phar->addFile($file,$path);
}

关于PHP Phar 在功能强大的 PC 上创建缓慢,如何加快速度(加载/读取 ~3000 个文件)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23963983/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com