Php Guzzle HTTP客户端比Symfony HTTP客户端慢
这可能是基于意见的问题。 我想使用尽可能多的建议,它比,也使用Guzzle HTTP客户端在其。但是,我使用SymfonyHTTP客户端和Guzzle HTTP客户端执行了一个简单的测试。结果表明Guzzle HTTP客户端比Symfony HTTP客户端慢得多 我想知道/理解为什么拥有如此著名声誉的HTTP客户端缺乏速度。或者是我做错了什么 composer.json test.php 操作系统和其他详细信息Php Guzzle HTTP客户端比Symfony HTTP客户端慢,php,symfony,guzzle,Php,Symfony,Guzzle,这可能是基于意见的问题。 我想使用尽可能多的建议,它比,也使用Guzzle HTTP客户端在其。但是,我使用SymfonyHTTP客户端和Guzzle HTTP客户端执行了一个简单的测试。结果表明Guzzle HTTP客户端比Symfony HTTP客户端慢得多 我想知道/理解为什么拥有如此著名声誉的HTTP客户端缺乏速度。或者是我做错了什么 composer.json test.php 操作系统和其他详细信息 Ubuntu18.04.4 LTS在Windows10版本1909的HyperV中
test.php
中将我的代码编辑为
我认为Guzzle HTTP客户端比Symfony HTTP客户端更适合我的项目。这是因为Symfony客户端不在$HTTP->request()上执行请求。 这是在第一次需要时完成的 当您更改两个测试用例的代码时,您会看到时间差消失了 当检查你的结果是否可信时,你应该注意到以前的时间是不可能实现的 修复前的我的测试,其中
Symfony:0.00944495201
狂饮:0.18365287780
Symfony结果比任何简单的网络ping都快当_curlG在_curlS之前时,结果是否相同?我已经交换了函数定义,测试代码也一样。最终的结果仍然是Guzzle比Symfony慢。vanilla curl的表现如何?基准测试很难获得正确的结果,而且结果通常是毫无用处的。使用这两种方法实现你的应用程序,然后分析你的日志。性能是否会有显著差异?这两种方法都在后台使用curl,因此,如果你在其中任何一种方法中获得的时间都比香草curl快,那么好的答案就有问题了。如果它对任何人都有帮助,请注意,如果不从
\u curlS
返回任何内容,您将看到相同的准确结果-Symfony响应类也会在超出范围时通过\u destruct
方法进行解析。
{
"require": {
"php": "7.4.*",
"symfony/http-client": "^5.0",
"guzzlehttp/guzzle": "^6.5"
}
}
<?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
...
$response = $http->request($method, $url, $httpOpt);
return $response->getStatusCode();