Guzzle HTTP Client 比 Symfony HTTP Client 慢
Guzzle HTTP Client is slower than Symfony HTTP Client
这可能是基于意见的问题。
我想使用 Guzzle HTTP Client as many suggest that its better than Symfony HTTP Client, also Cloudflare uses Guzzle HTTP Client in its PHP Api。但是,我使用 Symfony HTTP Client 和 Guzzle HTTP Client 执行了一个简单的测试。结果表明 Guzzle HTTP Client 比 Symfony HTTP Client 慢得多。
我想知道/理解为什么享有如此盛名的 Guzzle HTTP Client 缺乏速度。还是我做错了什么。
composer.json
{
"require": {
"php": "7.4.*",
"symfony/http-client": "^5.0",
"guzzlehttp/guzzle": "^6.5"
}
}
test.php
<?php
echo "<pre>\n";
require_once(__DIR__ . DIRECTORY_SEPARATOR . '../vendor/autoload.php');
$url = 'http://192.168.1.81';
$tSElaspedTotal = 0;
$tGElaspedTotal = 0;
$tCElaspedTotal = 0;
$iterations = 10;
for ($i = 1; $i <= $iterations; $i++) {
unset($tSElasped, $tSStart, $tSEnd, $httpS, $httpSOpt, $curlS);
unset($tGElasped, $tGStart, $tGEnd, $httpG, $httpGOpt, $curlG);
unset($tCElasped, $tCStart, $tCEnd, $httpC, $httpCOpt, $curlC);
$tSElasped = $tGElasped = $tCElasped = 0;
$httpS = new \Symfony\Component\HttpClient\CurlHttpClient();
$httpSOpt = [
'headers' => [
'User-Agent' => 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13',
],
'timeout' => 30,
];
echo "\nSymfony Http Client Start: " . $tSStart = microtime(true);
$curlS = $httpS->request('GET', $url, $httpSOpt);
echo "\nSymfony Http Client End: " . $tSEnd = microtime(true);
$httpG = new \GuzzleHttp\Client();
$httpGOpt = [
'headers' => [
'User-Agent' => 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13',
],
'force_ip_resolve' => 'v4',
'timeout' => 30,
];
echo "\nGuzzle Http Client Start: " . $tGStart = microtime(true);
$curlG = $httpG->request('GET', $url, $httpGOpt);
echo "\nGuzzle Http Client End: " . $tGEnd = microtime(true);
$httpC = curl_init();
curl_reset($httpC);
$httpCOpt = [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FRESH_CONNECT => true,
CURLOPT_FORBID_REUSE => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_MAXREDIRS => 5,
];
$httpCOpt[CURLOPT_URL] = $url;
curl_setopt_array($httpC, $httpCOpt);
echo "\nPHP Curl Start: " . $tCStart = microtime(true);
$curlC = curl_exec($httpC);
echo "\nPHP Curl End: " . $tCEnd = microtime(true);
$tSElasped = ($tSEnd - $tSStart);
$tSElaspedTotal += $tSElasped;
$tGElasped = ($tGEnd - $tGStart);
$tGElaspedTotal += $tGElasped;
$tCElasped = ($tCEnd - $tCStart);
$tCElaspedTotal += $tCElasped;
echo "\n({$i}) - Time Elasped\n";
echo "\nSymfony: \t" . $tSElasped . "\nGuzzle: \t" . $tGElasped . "\nCurl: \t\t" . $tCElasped . "<hr>";
}
echo "\nToal Time Elasped\n";
echo "\nSymfony: \t" . ($tSElaspedTotal / $iterations) . "\nGuzzle: \t" . ($tGElaspedTotal / $iterations) . "\nCurl: \t\t" . ($tCElaspedTotal / $iterations) . "<hr>";
My Results
Symfony Http Client Start: 1587377963.6117
Symfony Http Client End: 1587377963.6118
Guzzle Http Client Start: 1587377963.6119
Guzzle Http Client End: 1587377963.6302
PHP Curl Start: 1587377963.6302
PHP Curl End: 1587377963.6467
(1) - Time Elasped
Symfony: 0.00014400482177734
Guzzle: 0.018287897109985
Curl: 0.01648998260498
Symfony Http Client Start: 1587377963.6598
Symfony Http Client End: 1587377963.6599
Guzzle Http Client Start: 1587377963.6599
Guzzle Http Client End: 1587377963.6766
PHP Curl Start: 1587377963.6766
PHP Curl End: 1587377963.6911
(2) - Time Elasped
Symfony: 8.2015991210938E-5
Guzzle: 0.016661882400513
Curl: 0.014525175094604
Symfony Http Client Start: 1587377963.6978
Symfony Http Client End: 1587377963.6979
Guzzle Http Client Start: 1587377963.6979
Guzzle Http Client End: 1587377963.7114
PHP Curl Start: 1587377963.7114
PHP Curl End: 1587377963.7245
(3) - Time Elasped
Symfony: 9.0122222900391E-5
Guzzle: 0.013462066650391
Curl: 0.013139009475708
Symfony Http Client Start: 1587377963.7316
Symfony Http Client End: 1587377963.7317
Guzzle Http Client Start: 1587377963.7317
Guzzle Http Client End: 1587377963.7461
PHP Curl Start: 1587377963.7461
PHP Curl End: 1587377963.761
(4) - Time Elasped
Symfony: 8.2015991210938E-5
Guzzle: 0.014389991760254
Curl: 0.014890909194946
Symfony Http Client Start: 1587377963.7676
Symfony Http Client End: 1587377963.7677
Guzzle Http Client Start: 1587377963.7677
Guzzle Http Client End: 1587377963.7861
PHP Curl Start: 1587377963.7861
PHP Curl End: 1587377963.8006
(5) - Time Elasped
Symfony: 8.7976455688477E-5
Guzzle: 0.018366098403931
Curl: 0.014465093612671
Symfony Http Client Start: 1587377963.8074
Symfony Http Client End: 1587377963.8075
Guzzle Http Client Start: 1587377963.8075
Guzzle Http Client End: 1587377963.8244
PHP Curl Start: 1587377963.8244
PHP Curl End: 1587377963.8385
(6) - Time Elasped
Symfony: 7.9870223999023E-5
Guzzle: 0.016865968704224
Curl: 0.014086961746216
Symfony Http Client Start: 1587377963.8467
Symfony Http Client End: 1587377963.8468
Guzzle Http Client Start: 1587377963.8468
Guzzle Http Client End: 1587377963.8625
PHP Curl Start: 1587377963.8625
PHP Curl End: 1587377963.8773
(7) - Time Elasped
Symfony: 8.4877014160156E-5
Guzzle: 0.015748023986816
Curl: 0.014772891998291
Symfony Http Client Start: 1587377963.8842
Symfony Http Client End: 1587377963.8843
Guzzle Http Client Start: 1587377963.8843
Guzzle Http Client End: 1587377963.8971
PHP Curl Start: 1587377963.8971
PHP Curl End: 1587377963.9111
(8) - Time Elasped
Symfony: 8.4877014160156E-5
Guzzle: 0.012855052947998
Curl: 0.013998985290527
Symfony Http Client Start: 1587377963.9213
Symfony Http Client End: 1587377963.9214
Guzzle Http Client Start: 1587377963.9214
Guzzle Http Client End: 1587377963.9339
PHP Curl Start: 1587377963.9339
PHP Curl End: 1587377963.9464
(9) - Time Elasped
Symfony: 8.392333984375E-5
Guzzle: 0.012518882751465
Curl: 0.012485980987549
Symfony Http Client Start: 1587377963.9528
Symfony Http Client End: 1587377963.9529
Guzzle Http Client Start: 1587377963.9529
Guzzle Http Client End: 1587377963.9708
PHP Curl Start: 1587377963.9708
PHP Curl End: 1587377963.985
(10) - Time Elasped
Symfony: 7.7962875366211E-5
Guzzle: 0.017860889434814
Curl: 0.014163017272949
Toal Time Elasped
Symfony: 8.9764595031738E-5
Guzzle: 0.015701675415039
Curl: 0.014301800727844
OS and Other Details.
- Ubuntu Windows 10 版本 1909
HyperV 中的 18.04.4 LTS
- Curl 版本 7.58 (7.58.0-2ubuntu3.8)
- PHP版本7.4.5(7.4.5-1+ubuntu18.04.1+deb.sury.org+1)
- PHP FPM 版本7.4 (7.4.5-1+ubuntu18.04.1+deb.sury.org+1)
- PHPCurl版本7.4(7.4.5-1+ubuntu18.04.1+deb.sury.org+1)
Change 1
更新:
我已将 test.php
中的代码编辑为
- 将 PHP Curl 加入组合。
- 为多个并发请求添加了循环。
- 使用本地网络上另一台服务器的地址作为测试站点,因为许多站点会阻塞多个并发请求。
- 正确重置所有变量,而不是仅仅重新分配。
发现:
更改代码后的时间差异很大。但是,即使有人向我指出了 ,我仍然对为什么会发生这种情况感到困惑。
但最终 Guzzle HTTP 客户端的计时(这看起来很现实)接近 PHP Curl,并且 Guzzle HTTP 客户端处理 HTTP 错误的方式不同于 Symfony HTTP 客户端并抛出异常在获取 url 而不是在获取结果时。
我认为 Guzzle HTTP 客户端比 Symfony HTTP 客户端更适合我的项目。
那是因为 Symfony 客户端不在 $http->request() 上执行请求。
这是在第一次需要时完成的。
当您更改两个测试用例的代码时,您会发现时差消失了
...
$response = $http->request($method, $url, $httpOpt);
return $response->getStatusCode();
在检查结果的合理性时,您应该注意到以前的时间是不可能达到的。
修复前我的测试
交响乐:0.00944495201
Guzzle: 0.18365287780
Symfony 结果比任何简单的网络 ping 都快
这可能是基于意见的问题。
我想使用 Guzzle HTTP Client as many suggest that its better than Symfony HTTP Client, also Cloudflare uses Guzzle HTTP Client in its PHP Api。但是,我使用 Symfony HTTP Client 和 Guzzle HTTP Client 执行了一个简单的测试。结果表明 Guzzle HTTP Client 比 Symfony HTTP Client 慢得多。
我想知道/理解为什么享有如此盛名的 Guzzle HTTP Client 缺乏速度。还是我做错了什么。
composer.json
{
"require": {
"php": "7.4.*",
"symfony/http-client": "^5.0",
"guzzlehttp/guzzle": "^6.5"
}
}
test.php
<?php
echo "<pre>\n";
require_once(__DIR__ . DIRECTORY_SEPARATOR . '../vendor/autoload.php');
$url = 'http://192.168.1.81';
$tSElaspedTotal = 0;
$tGElaspedTotal = 0;
$tCElaspedTotal = 0;
$iterations = 10;
for ($i = 1; $i <= $iterations; $i++) {
unset($tSElasped, $tSStart, $tSEnd, $httpS, $httpSOpt, $curlS);
unset($tGElasped, $tGStart, $tGEnd, $httpG, $httpGOpt, $curlG);
unset($tCElasped, $tCStart, $tCEnd, $httpC, $httpCOpt, $curlC);
$tSElasped = $tGElasped = $tCElasped = 0;
$httpS = new \Symfony\Component\HttpClient\CurlHttpClient();
$httpSOpt = [
'headers' => [
'User-Agent' => 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13',
],
'timeout' => 30,
];
echo "\nSymfony Http Client Start: " . $tSStart = microtime(true);
$curlS = $httpS->request('GET', $url, $httpSOpt);
echo "\nSymfony Http Client End: " . $tSEnd = microtime(true);
$httpG = new \GuzzleHttp\Client();
$httpGOpt = [
'headers' => [
'User-Agent' => 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13',
],
'force_ip_resolve' => 'v4',
'timeout' => 30,
];
echo "\nGuzzle Http Client Start: " . $tGStart = microtime(true);
$curlG = $httpG->request('GET', $url, $httpGOpt);
echo "\nGuzzle Http Client End: " . $tGEnd = microtime(true);
$httpC = curl_init();
curl_reset($httpC);
$httpCOpt = [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FRESH_CONNECT => true,
CURLOPT_FORBID_REUSE => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_MAXREDIRS => 5,
];
$httpCOpt[CURLOPT_URL] = $url;
curl_setopt_array($httpC, $httpCOpt);
echo "\nPHP Curl Start: " . $tCStart = microtime(true);
$curlC = curl_exec($httpC);
echo "\nPHP Curl End: " . $tCEnd = microtime(true);
$tSElasped = ($tSEnd - $tSStart);
$tSElaspedTotal += $tSElasped;
$tGElasped = ($tGEnd - $tGStart);
$tGElaspedTotal += $tGElasped;
$tCElasped = ($tCEnd - $tCStart);
$tCElaspedTotal += $tCElasped;
echo "\n({$i}) - Time Elasped\n";
echo "\nSymfony: \t" . $tSElasped . "\nGuzzle: \t" . $tGElasped . "\nCurl: \t\t" . $tCElasped . "<hr>";
}
echo "\nToal Time Elasped\n";
echo "\nSymfony: \t" . ($tSElaspedTotal / $iterations) . "\nGuzzle: \t" . ($tGElaspedTotal / $iterations) . "\nCurl: \t\t" . ($tCElaspedTotal / $iterations) . "<hr>";
My Results
Symfony Http Client Start: 1587377963.6117
Symfony Http Client End: 1587377963.6118
Guzzle Http Client Start: 1587377963.6119
Guzzle Http Client End: 1587377963.6302
PHP Curl Start: 1587377963.6302
PHP Curl End: 1587377963.6467
(1) - Time Elasped
Symfony: 0.00014400482177734
Guzzle: 0.018287897109985
Curl: 0.01648998260498
Symfony Http Client Start: 1587377963.6598
Symfony Http Client End: 1587377963.6599
Guzzle Http Client Start: 1587377963.6599
Guzzle Http Client End: 1587377963.6766
PHP Curl Start: 1587377963.6766
PHP Curl End: 1587377963.6911
(2) - Time Elasped
Symfony: 8.2015991210938E-5
Guzzle: 0.016661882400513
Curl: 0.014525175094604
Symfony Http Client Start: 1587377963.6978
Symfony Http Client End: 1587377963.6979
Guzzle Http Client Start: 1587377963.6979
Guzzle Http Client End: 1587377963.7114
PHP Curl Start: 1587377963.7114
PHP Curl End: 1587377963.7245
(3) - Time Elasped
Symfony: 9.0122222900391E-5
Guzzle: 0.013462066650391
Curl: 0.013139009475708
Symfony Http Client Start: 1587377963.7316
Symfony Http Client End: 1587377963.7317
Guzzle Http Client Start: 1587377963.7317
Guzzle Http Client End: 1587377963.7461
PHP Curl Start: 1587377963.7461
PHP Curl End: 1587377963.761
(4) - Time Elasped
Symfony: 8.2015991210938E-5
Guzzle: 0.014389991760254
Curl: 0.014890909194946
Symfony Http Client Start: 1587377963.7676
Symfony Http Client End: 1587377963.7677
Guzzle Http Client Start: 1587377963.7677
Guzzle Http Client End: 1587377963.7861
PHP Curl Start: 1587377963.7861
PHP Curl End: 1587377963.8006
(5) - Time Elasped
Symfony: 8.7976455688477E-5
Guzzle: 0.018366098403931
Curl: 0.014465093612671
Symfony Http Client Start: 1587377963.8074
Symfony Http Client End: 1587377963.8075
Guzzle Http Client Start: 1587377963.8075
Guzzle Http Client End: 1587377963.8244
PHP Curl Start: 1587377963.8244
PHP Curl End: 1587377963.8385
(6) - Time Elasped
Symfony: 7.9870223999023E-5
Guzzle: 0.016865968704224
Curl: 0.014086961746216
Symfony Http Client Start: 1587377963.8467
Symfony Http Client End: 1587377963.8468
Guzzle Http Client Start: 1587377963.8468
Guzzle Http Client End: 1587377963.8625
PHP Curl Start: 1587377963.8625
PHP Curl End: 1587377963.8773
(7) - Time Elasped
Symfony: 8.4877014160156E-5
Guzzle: 0.015748023986816
Curl: 0.014772891998291
Symfony Http Client Start: 1587377963.8842
Symfony Http Client End: 1587377963.8843
Guzzle Http Client Start: 1587377963.8843
Guzzle Http Client End: 1587377963.8971
PHP Curl Start: 1587377963.8971
PHP Curl End: 1587377963.9111
(8) - Time Elasped
Symfony: 8.4877014160156E-5
Guzzle: 0.012855052947998
Curl: 0.013998985290527
Symfony Http Client Start: 1587377963.9213
Symfony Http Client End: 1587377963.9214
Guzzle Http Client Start: 1587377963.9214
Guzzle Http Client End: 1587377963.9339
PHP Curl Start: 1587377963.9339
PHP Curl End: 1587377963.9464
(9) - Time Elasped
Symfony: 8.392333984375E-5
Guzzle: 0.012518882751465
Curl: 0.012485980987549
Symfony Http Client Start: 1587377963.9528
Symfony Http Client End: 1587377963.9529
Guzzle Http Client Start: 1587377963.9529
Guzzle Http Client End: 1587377963.9708
PHP Curl Start: 1587377963.9708
PHP Curl End: 1587377963.985
(10) - Time Elasped
Symfony: 7.7962875366211E-5
Guzzle: 0.017860889434814
Curl: 0.014163017272949
Toal Time Elasped
Symfony: 8.9764595031738E-5
Guzzle: 0.015701675415039
Curl: 0.014301800727844
OS and Other Details.
- Ubuntu Windows 10 版本 1909 HyperV 中的 18.04.4 LTS
- Curl 版本 7.58 (7.58.0-2ubuntu3.8)
- PHP版本7.4.5(7.4.5-1+ubuntu18.04.1+deb.sury.org+1)
- PHP FPM 版本7.4 (7.4.5-1+ubuntu18.04.1+deb.sury.org+1)
- PHPCurl版本7.4(7.4.5-1+ubuntu18.04.1+deb.sury.org+1)
Change 1
更新:
我已将 test.php
中的代码编辑为
- 将 PHP Curl 加入组合。
- 为多个并发请求添加了循环。
- 使用本地网络上另一台服务器的地址作为测试站点,因为许多站点会阻塞多个并发请求。
- 正确重置所有变量,而不是仅仅重新分配。
发现:
更改代码后的时间差异很大。但是,即使有人向我指出了
但最终 Guzzle HTTP 客户端的计时(这看起来很现实)接近 PHP Curl,并且 Guzzle HTTP 客户端处理 HTTP 错误的方式不同于 Symfony HTTP 客户端并抛出异常在获取 url 而不是在获取结果时。
我认为 Guzzle HTTP 客户端比 Symfony HTTP 客户端更适合我的项目。
那是因为 Symfony 客户端不在 $http->request() 上执行请求。 这是在第一次需要时完成的。
当您更改两个测试用例的代码时,您会发现时差消失了
...
$response = $http->request($method, $url, $httpOpt);
return $response->getStatusCode();
在检查结果的合理性时,您应该注意到以前的时间是不可能达到的。
修复前我的测试
交响乐:0.00944495201
Guzzle: 0.18365287780
Symfony 结果比任何简单的网络 ping 都快