【发布时间】:2013-12-25 01:17:52
【问题描述】:
我正在运行一个从 API 服务器获取数据的专用服务器。我的机器在 Windows Server 2008 操作系统上运行。
我使用 PHP curl 函数通过 http 请求(并使用代理)获取数据。我为此创建的函数:
function get_http($url)
{
$proxy_file = file_get_contents("proxylist.txt");
$proxy_file = explode("
", $proxy_file);
$how_Many_Proxies = count($proxy_file);
$which_Proxy = rand(0,$how_Many_Proxies);
$proxy = $proxy_file[$which_Proxy];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_PROXY, $proxy);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
$curl_scraped_page = curl_exec($ch);
curl_close($ch);
return $curl_scraped_page;
}
然后我使用这个简单的代码将它保存在 MySQL 数据库中,该代码与 curl 并行运行 20-40-60-100 个版本(经过一定数量后,它不会提高性能,我想知道瓶颈在哪里?) :
function retrieveData($id)
{
$the_data = get_http("http://api-service-ip-address/?id=$id");
return $the_data;
}
$ids_List = file_get_contents("the-list.txt");
$ids_List = explode("
",$ids_List);
for($a = 0;$a<50;$a++)
{
$array[$a] = get_http($ids_List[$a]);
}
for($b = 0;$b<50;$b++)
{
$insert_Array[] = "('$ids_List[$b]', NULL, '$array[$b]')";
}
$insert_Array = implode(',', $insert_Array);
$sql = "INSERT INTO `the_data` (`id`, `queue_id`, `data`) VALUES $insert_Array;";
mysql_query($sql);
经过多次优化,我被困在每秒大约 23 行数据上检索/获取/保存。
MySQL 表非常简单,如下所示:
标识 | queue_id(AI) |数据
请记住,数据库似乎并不是瓶颈。当我检查 CPU 使用率时,mysql.exe 进程几乎没有超过 1%。
我通过 125 个代理获取数据。 我已经将测试的数量减少到 20 并且它没有任何区别(表明代理不是瓶颈?-因为当使用的代理数量减少 5 倍时,我获得了相同的性能?)
所以如果 MySQL 和 Proxies 不是限制的原因,还有什么可能是它,我该如何找出?
到目前为止,我所做的优化:
用 curl 函数替换了 file_get_contents 来检索 http数据
将 https:// 网址替换为 http:// 网址(这样更快吗?)
索引表
替换了纯IP调用的API域名(所以 DNS时间不是一个因素)
我只使用低延迟的私有代理。
我的问题:
性能限制的可能原因是什么?
如何找到限制原因?
这可能是由某些 TCP/IP 限制/配置不当的 apache/windows 引起的吗?
API 非常快,它向其他人提供的查询要多很多倍,所以我认为它的响应速度不会更快。
【问题讨论】:
-
你用 xdebug 和 webgrind 分析过它吗? slideshare.net/samkeen/profiling-php-with-xdebug-webgrind
标签: php mysql optimization curl proxy