2015-04-14 150 views
2

我有一个csv文件,它定义了要测试的路由,并且每个路由的预期状态码应该返回。Symfony 1.4功能测试 - 减少内存使用量

我正在对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文件。

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

+0

这张票可能是相关的:http://trac.symfony-project.org/ticket/6621 – acleitner

+0

没有机会。可能的解决方案是将每个路由测试作为单独的php进程运行。 – Marek

+0

任何新闻? '也就是说,它已经在PHP 5.3中修复了。 '但我有PHP 5.3。 –

回答

0

我面临同样的内存不足问题。我需要抓取非常长的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的数量处理。如果你仍然有内存不足的问题,你可以减少它