我有一个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文件中。
有没有办法减少此测试使用的内存量?
答案 0 :(得分: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数。如果仍有内存不足问题,可以减少它