奇怪的 Thrift/PHP 性能问题
我在 Thrift 0.8 for PHP 的服务器和客户端上都遇到了奇怪的性能问题。我使用简单的 echo() 服务器调用进行了两个测试,该调用仅返回您发送给它的字符串。
- 测试 1:open() 传输,调用 echo() 5 次并 close() 传输。
- 测试2:循环5次:open()传输,调用echo(),close()传输。
什么?测试 2 是否为每次调用打开和关闭套接字?那一定很慢!
这就是奇怪的地方:测试 2 比测试 1 快 40 倍,每次迭代大约 1 毫秒,而每次迭代大约 40 毫秒。
这是基本想法。
$socket = new TSocket($host);
$transport = new TBufferedTransport($socket, 1024, 1024);
$protocol = new TBinaryProtocol($transport);
// Test 2: Open and close the connection each time.
function testSingleCall($c, $transport, $protocol) {
$client = new \thrift\LocationsClient($protocol, $protocol);
$t1 = microtime(true);
for ($i = 0; $i < $c; $i++) {
$transport->open();
$v = $client->echoMessage($i);
$transport->close();
}
$t2 = microtime(true);
return (($t2 - $t1) * 1000) / $c; // miliseconds per call
}
// Test 1: multiple calls per transaction.
function testMultipleCall($c, $transport, $protocol) {
$client = new \thrift\LocationsClient($protocol, $protocol);
$t1 = microtime(true);
$transport->open();
for ($i = 0; $i < $c; $i++) {
$v = $client->echoMessage($i);
}
$transport->close();
$t2 = microtime(true);
return (($t2 - $t1) * 1000) / $c; // miliseconds per call
?
有什么想法会导致打开和关闭套接字比重新使用打开的套接字更快吗
更新:缓慢是在读取消息头 TBinaryProtocol::readMessageBegin() 期间。打开传输后的第一个调用很快(<1ms),而同一打开的传输上的所有后续调用都非常慢(38ms)
I've got a bizarre performance issue with Thrift 0.8 for PHP for both server and client. I've got two tests using a simple echo() server call that just returns the string you send to it.
- Test 1: open() the transport, Call echo() 5 times and close() the transport.
- Test 2: Loop 5 times: open() transport, call echo(), close() transport.
What? Test 2 is opening and closing the socket for each call? That must be really slow!
That's what is bizarre: Test 2 is 40x faster than Test 1, about 1ms per iteration vs 40ms per iteration.
Here is the basic idea.
$socket = new TSocket($host);
$transport = new TBufferedTransport($socket, 1024, 1024);
$protocol = new TBinaryProtocol($transport);
// Test 2: Open and close the connection each time.
function testSingleCall($c, $transport, $protocol) {
$client = new \thrift\LocationsClient($protocol, $protocol);
$t1 = microtime(true);
for ($i = 0; $i < $c; $i++) {
$transport->open();
$v = $client->echoMessage($i);
$transport->close();
}
$t2 = microtime(true);
return (($t2 - $t1) * 1000) / $c; // miliseconds per call
}
// Test 1: multiple calls per transaction.
function testMultipleCall($c, $transport, $protocol) {
$client = new \thrift\LocationsClient($protocol, $protocol);
$t1 = microtime(true);
$transport->open();
for ($i = 0; $i < $c; $i++) {
$v = $client->echoMessage($i);
}
$transport->close();
$t2 = microtime(true);
return (($t2 - $t1) * 1000) / $c; // miliseconds per call
}
Any ideas what would cause opening and closing sockets to be faster than re-using the open socket?
Update: The slowness is during reading the message header, TBinaryProtocol::readMessageBegin(). The first call after opening the transport is fast ( < 1ms) while all subsequent calls on the same opened transport are very slow ( 38ms )
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论