[PHP] stream, socket ´Ù¿î·Îµå º¥Ä¡¸¶Å· ¹× Æ©´× - ÀÛ¼ºÀÚ : ±èÄ¥ºÀ < san2(at)linuxchannel.net > - ÀÛ¼ºÀÏ : 2005.03.30 ³»¿ëÃß°¡ 2005.03.28 - ³» ¿ë : client stream(blocking), socket(blocking/non-blocking) download Benchmark with server KeepAlive - ¼ö ÁØ : ÃÊÁß±Þ ÀÌ»ó - Å°¿öµå : fsockopen(), feof(), fgets(), fread(), KeepAlive, Connection blocking, non-blocking, socket_read() *ÁÖ1) ÀÌ ¹®¼­¿¡ ´ëÇÑ ÃֽŠ³»¿ëÀº ¾Æ·¡ URL¿¡¼­ È®ÀÎÇÒ ¼ö ÀÖ½À´Ï´Ù. http://linuxchannel.net/docs/php-stream-socket-benchmark.txt *ÁÖ2) ÀÌ ¹®¼­¿¡¼­ »ç¿ëÇÑ `buf'´Â °ø½ÄÀûÀÎ ¿ë¾î°¡ ¾Æ´Ï°í, buffering ÇÑ µ¥ÀÌÅͳª ¶Ç´Â ±×°ÍµéÀ» ÁöĪÇÏ´Â PHP »ç¿ëÀÚ º¯¼ö¸¦ ¸»ÇÕ´Ï´Ù. *Á¶°Ç) - ¼­¹ö(¾ÆÆÄÄ¡) KeepAlive On/Off, ¾à 100 MBytes data - Ŭ¶óÀ̾ðÆ®(PHP cli) blocking/non-blocking mode (PHP ·Î ºñ±³Àû µ¢Ä¡Å« ÆÄÀÏÀ» ´Ù¿î·Îµå ¹ÞÀ» °æ¿ì¿¡ ÇØ´çµÊ) * benchmark source) http://ftp.linuxchannel.net/devel/php_download/ ¸ñÂ÷ ------------------------------------------------------------------------------------ 1. client blocking (fsockopen) benchmark 2. client blocking (socket) benchmark 3. client non-blocking (socket) benchmark 4. client non-blocking buf check (socket) benchmark ------------------------------------------------------------------------------------ 1. client blocking (fsockopen) benchmark ------------------------------------------------------------------------------------ clinet server KeepAlive On server KeepAlive Off HTTP header ------------------------------------------------------------------------------------ NA(feof) 3965.9 KB/sec* 10718.7KB/sec Connection 10882.1 KB/sec 10904.4KB/sec close timeout 4017.3 KB/sec* 10769.2KB/sec buf check 3984.5 KB/sec* 10811.5KB/sec ¡¡ Connection + timeout 11094.3 KB/sec 11382.6KB/sec close Connection + buf check 10700.1 KB/sec 11489.4KB/sec close timeout + buf check 10059.0 KB/sec 11073.0KB/sec Connection + timeout + buf check 10589.5 KB/sec 11497.5KB/sec close ------------------------------------------------------------------------------------ PHP guide ( Connection: close [+ ...] // <-- recommend or timeout + buf check [+ ...] ) example ( $req = "GET /path HTTP/1.1\r\nHost: hostname\r\nConnection: close\r\n\r\n"; $fp = fscokopen(...); stream_set_timeout($fp,0,500000); // some recommend fwrite($fp,$req); while($buf = fread($fp,4096)) { $rbuf .= $buf; ... } fclose($fp); ) 2. client blocking (socket) benchmark ------------------------------------------------------------------------------------ clinet server KeepAlive On server KeepAlive Off HTTP header ------------------------------------------------------------------------------------ NA(socket_read) 4014.9KB/sec* 11463.8KB/sec Connection 11491.0KB/sec 11465.4KB/sec close timeout 11372.2KB/sec 11482.3KB/sec buf(1M) 4007.5KB/sec* 11294.7KB/sec Connection + timeout 11373.5KB/sec 11496.8KB/sec close Connection + buf(1M) 11500.8KB/sec 11017.5KB/sec close timeout + buf(1M) 11373.2KB/sec 11499.9KB/sec Connection + timeout + buf(1M) 11369.1KB/sec 11378.3KB/sec close ------------------------------------------------------------------------------------ PHP guide ( Connection: close [+ ...] or timeout [+ ...] ) example ( $req = "GET /path HTTP/1.1\r\nHost: hostname\r\nConnection: close\r\n\r\n"; $timeout = array('sec'=>0,'usec'=>500000); $sock = socket_create(...); socket_set_option($sock,...,SO_RCVTIMEO,$timeout); // some recommend socket_connect(...); socket_write($sock,$req); while($buf = socket_read($sock,1048576)) { $rbuf .= $buf; ... } socket_close($sock); ) 3. client non-blocking (socket) benchmark ------------------------------------------------------------------------------------ clinet server KeepAlive On server KeepAlive Off HTTP header ------------------------------------------------------------------------------------ NA(socket_read) 11352.4KB/sec CLOSE_WAIT* Connection CLOSE_WAIT* CLOSE_WAIT* close timeout 11343.1KB/sec CLOSE_WAIT* buf(1M) 11361.0KB/sec CLOSE_WAIT* Connection + timeout CLOSE_WAIT* CLOSE_WAIT* close Connection + buf(1M) CLOSE_WAIT* CLOSE_WAIT* close timeout + buf(1M) 11359.1KB/sec CLOSE_WAIT* Connection + timeout + buf(1M) CLOSE_WAIT* CLOSE_WAIT* close ------------------------------------------------------------------------------------ PHP guide ( buf check [+ ...] ) example ( $req = "GET /path HTTP/1.1\r\nHost: hostname\r\nConnection: close\r\n\r\n"; $timeout = array('sec'=>0,'usec'=>500000); $sock = socket_create(...); socket_set_option($sock,...,SO_RCVTIMEO,$timeout); // some recommend socket_connect(...); socket_write($sock,$req); socket_set_nonblock($sock); // set to non-blocking mode $stream = array($sock); while(@socket_select($stream,$write=NULL,$except=NULL,0,500000) !== FALSE) { if(!in_array($sock,$stream)) break; if($buf = @socket_read($sock,1048576)) { $rbuf .= $buf; ... } else break; // good idea, EOF, buf check } socket_close($sock); ) 4. client non-blocking buf check (socket) benchmark ------------------------------------------------------------------------------------ clinet server KeepAlive On server KeepAlive Off HTTP header ------------------------------------------------------------------------------------ NA(socket_read) 10870.7KB/sec 11455.9KB/sec Connection 11488.1KB/sec 11481.9KB/sec close timeout 10869.7KB/sec 11165.4KB/sec buf(1M) 10872.9KB/sec 11494.5KB/sec Connection + timeout 10903.7KB/sec 11496.6KB/sec close Connection + buf(1M) 11500.6KB/sec 11500.2KB/sec close timeout + buf(1M) 10880.3KB/sec 11399.8KB/sec Connection + timeout + buf(1M) 11500.1KB/sec 11498.4KB/sec close ------------------------------------------------------------------------------------ PHP guide ( buf check [+ ...] ) example ( same as above example ) EOF