PHP & Others

[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

관련자료

댓글 0
등록된 댓글이 없습니다.
Today's proverb
행복해지고 싶다면, 잠시 동안만이라도 가슴에 손을 얹고 생각해 보라. 그러면 진정한 즐거움은, 발치에 돋아나는 잡초나 아침 햇살에 빛나는 꽃의 이술과 같이 우리 주변에 무수히 널려 있다는 것을 알 수 있을 것이다. 《하루 5분 생각이 인생을 결정한다 》 (이범준)