这可能是基于意见的问题。
我想使用古斯HTTP客户端,因为许多人认为它比Symfony HTTP客户端更好,Cloudflare在其PHP API中也使用古斯HTTP客户端。但是,我使用Symfony HTTP客户端和古斯HTTP客户端执行了一个简单的测试。结果表明,古斯HTTP客户端比Symfony HTTP客户端慢得多。
我想知道/理解为什么拥有如此著名的声誉的HTTP客户端缺乏速度。还是我做错了什么?
composer.json
{
"require": {
"php": "7.4.*",
"symfony/http-client": "^5.0",
"guzzlehttp/guzzle": "^6.5"
}
}
测试.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
更新:
我在<code>测试中编辑了我的代码。phpto
发现:更改代码后,时间差异很大。然而,即使有人向我指出这一点,仍然对为什么会发生这种情况感到困惑。
但最终,由于Guzzle HTTP客户端的计时(这似乎是现实的)接近PHP Curl,并且由于Guzzle HTTP客户端处理HTTP错误的方式不同于Symfony HTTP客户端,并且在获取url而不是结果获取时抛出异常。
我认为Guzzle HTTP客户端比Symfony HTTP客户端更适合我的项目。
这是因为Symfony客户端不执行$超文本传输协议-
当您更改两个测试用例的代码时,您会看到时差消失
...
$response = $http->request($method, $url, $httpOpt);
return $response->getStatusCode();
在检查结果的合理性时,您应该注意到以前的时代是不可能的。
我的测试之前修复其中
Symfony: 0.00944495201
Guzzle: 0.18365287780
Symfony结果比任何简单的网络ping都快
我想在一些计算机之间建立点对点连接,这样用户就可以在没有外部服务器的情况下聊天和交换文件。我最初的想法如下: 我在服务器上制作了一个中央服务器插座,所有应用程序都可以连接到该插座。此ServerSocket跟踪已连接的套接字(客户端),并将新连接的客户端的IP和端口提供给所有其他客户端。每个客户端都会创建一个新的ServerSocket,所有客户端都可以连接到它。 换句话说:每个客户端都有一个Se
URI 方法 URI() string 返回当前客户端使用的服务器地址。 SetURI 方法 SetURI(uri string) 设置当前客户端使用的服务器地址。如果你想要设置多个服务器地址,请使用 SetURIList 方法代替该方法。 URIList 方法 URIList() []string 返回当前客户端可使用的服务器地址列表。 SetURIList 方法 SetURIList(uriL
客户端事件通过 SetEvent 方法进行设置。 客户端事件有两个,它们分别定义为: type onErrorEvent interface { OnError(name string, err error) } type onFailswitchEvent interface { OnFailswitch(Client) } 因为 go 语言不需要显式实现接口的特点,所以这两
创建客户端有两种方式,一种是直接使用特化的构造器函数,另一种是使用工厂构造器函数。 第一种方式返回的是具体的客户端结构体指针对象,第二种方式返回的是客户端接口对象。 使用特化的构造器函数创建客户端 特化的构造器函数有下面几个: func NewHTTPClient(uri ...string) (client *HTTPClient) func NewTCPClient(uri ...string
客户端下载地址: windows 32位安装包 windows 64位安裝包 mac 安装包 Android App iOS App
问题 你想使用网络上提供的服务。 解决方案 创建一个基本的 TCP 客户机。 在 Node.js 中 net = require 'net' domain = 'localhost' port = 9001 connection = net.createConnection port, domain connection.on 'connect', () -> console.log