【问题标题】:Symfony 1.4 functional test - reduce memory usageSymfony 1.4 功能测试——减少内存使用
【发布时间】:2015-06-20 10:05:43
【问题描述】:

我有一个 csv 文件,它定义了要测试的路线,以及每条路线应返回的预期状态代码。

我正在进行一项功能测试,它遍历 csv 文件并向每个路由发出请求,然后检查是否返回了正确的状态代码。

$browser = new sfTestFunctional(new sfBrowser());

foreach ($routes as $route)
{
    $browser->
        get($route['path'])->

        with('response')->begin()->
            isStatusCode($route['code'])->
        end()
    ;
    print(memory_get_usage());
}

/***************  OUTPUT:  *************************

ok 1 - status code is 200
97953280# get /first_path
ok 2 - status code is 200
109607536# get /second_path
ok 3 - status code is 403
119152936# get /third_path
ok 4 - status code is 200
130283760# get /fourth_path
ok 5 - status code is 200
140082888# get /fifth_path
...

/***************************************************/

这会一直持续到出现允许的内存耗尽错误。

我增加了允许的内存量,暂时解决了这个问题。这不是一个永久的解决方案,因为随着时间的推移,更多的路由将添加到 csv 文件中。

有没有办法减少这个测试使用的内存量?

【问题讨论】:

  • 这张票可能是相关的:trac.symfony-project.org/ticket/6621
  • 没有机会。可能的解决方案是将每个路由测试作为单独的 php 进程运行。
  • 有什么消息吗? That said, it has been fixed in PHP 5.3. 我有 PHP 5.3。
  • 很好奇,这是在哪个 PHP 版本中修复的? Laurent 的评论中缺少一个数字。

标签: php memory doctrine symfony-1.4


【解决方案1】:

我遇到了同样的内存不足问题。我需要爬取一个很长的 URI 列表(大约 30K)来生成 HTML 缓存。感谢Marek,我尝试分叉进程。还是有一点点泄漏,但是这微不足道。

作为输入,我有一个文本文件,每个 URI 有一行。当然,您可以使用 CSV 轻松调整以下脚本。

const NUMBER_OF_PROCESS = 4;
const SIZE_OF_GROUPS = 5;

require_once(dirname(__FILE__).'/../../config/ProjectConfiguration.class.php');
$configuration = ProjectConfiguration::getApplicationConfiguration('frontend', 'prod', false);
sfContext::createInstance($configuration);

$file = new SplFileObject(dirname(__FILE__).'/list-of-uri.txt');

while($file->valid())
{
    $count = 0;
    $uris = array();
    while($file->valid() && $count < NUMBER_OF_PROCESS * SIZE_OF_GROUPS) {
        $uris[] = trim($file->current());
        $file->next();
        $count++;
    }
    $urisGroups = array_chunk($uris, SIZE_OF_GROUPS);

    $childs = array();
    echo "---\t\t\t Forking ".sizeof($urisGroups)." process \t\t\t ---\n";
    foreach($urisGroups as $uriGroup) {
        $pid = pcntl_fork();
        if($pid == -1)
            die('Could not fork');
        if(!$pid) {
            $b = new sfBrowser();
            foreach($uriGroup as $key => $uri) {
                $starttime = microtime(true);
                $b->get($uri);
                $time = microtime(true) - $starttime;
                echo 'Mem: '.memory_get_peak_usage().' - '.$time.'s - URI N°'.($key + 1).' PID '.getmypid().' - Status: '.$b->getResponse()->getStatusCode().' - URI: '.$uri."\n";
            }
            exit();
        }
        if($pid) {
            $childs[] = $pid;
        }
    }

    while(count($childs) > 0) {
        foreach($childs as $key => $pid) {
            $res = pcntl_waitpid($pid, $status, WNOHANG);

            // If the process has already exited
            if($res == -1 || $res > 0)
                unset($childs[$key]);
        }
        sleep(1);
    }
}

const NUMBER_OF_PROCESS 定义了工作的并行进程的数量(因此,如果你有一个多核处理器,你可以节省时间)

const NUMBER_OF_PROCESS是定义每个进程中sfBrowser要爬取的URI个数。如果你仍然有内存不足的问题,你可以减少它

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-07-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-06
    • 1970-01-01
    相关资源
    最近更新 更多