这可能是基于意见的问题.
我想使用口吻HTTP客户端,因为很多人认为它比Symfony HTTP客户端更好,Cloudflare在PHP Api中也使用了Guzzle客户端。但是,我使用Symfony HTTP客户端和Guzzle客户端执行了一个简单的测试。结果表明,Guzzle客户端比Symfony HTTP客户端慢得多。
我想知道/理解为什么这么有名的口碑,口香糖HTTP客户端缺乏速度。或者我是不是做错了什么。
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>";
我的结果
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
操作系统和其他细节。
变化1
更新:
我在test.php
中编辑了我的代码
发现:在更改代码后的时间上有很大的差异。然而,即使向我指出了这,但仍然对为什么会发生这种情况感到困惑。
但最终,由于Guzzle客户端的时间(这似乎是现实的)接近PHP Curl,而且result客户端处理不同于Symfony HTTP客户端的HTTP错误,并在获取url而不是结果抓取时抛出异常。
我认为,与Symfony HTTP客户端相比,客户端更适合我的项目。
发布于 2020-04-20 10:01:20
这是因为Symfony客户机执行请求而不是在$http-> request ()上执行请求。这是在第一次需要时完成的。
当您更改两个测试用例的代码时,会看到时间差逐渐消失。
...
$response = $http->request($method, $url, $httpOpt);
return $response->getStatusCode();
当检查你的结果是否可信时,你应该注意到以前的时间是不可能实现的。
我的测试在修复之前
塞富尼: 0.00944495201
口吻: 0.18365287780
Symfony结果比任何简单的网络ping都快。
https://stackoverflow.com/questions/61317981
复制相似问题