응용개발

PHP 성능 최적화를 위한 고려 사항

by 웁쓰 posted Jul 01, 2009

1 . strace를 통한 시스템콜 추적

테스트 방법

/usr/sbin/apache2 -X &
[1] 16367

% strace -p 16367 -o sys1.txt
Process 16367 attached - interrupt to quit

Process 16367 detached
% grep stat sys1.txt | grep -v fstat | wc -l 153

잘 고민 하지 않고 넘어 가는 부분이긴 하지만 다음의 실수는 strace를 통한 시스템콜에서 발견 할 수 있다.
여러 확장자를 디렉토리 인덱스에 적용하면 여러번의 시스템콜이 일어나며 성능에 악영향을 미친다.
stat64("/var/www/index.html", 0xbfd279ac) = -1 ENOENT (No such file or directory)
stat64("/var/www/index.cgi", 0xbfd27afc) = -1 ENOENT (No such file or directory)
stat64("/var/www/index.pl", 0xbfd27afc) = -1 ENOENT (No such file or directory)
stat64("/var/www/index.php", {st_mode=S_IFREG|0664, st_size=7198, ...}) = 0
TODO: DirectoryIndex 에 필요한 확장자만 등록 할것
<Directory /var/www>
    DirectoryIndex index.php
</Directory>

2. include 경로 설정 주의

다음의 두가지 경우를 테스트 해볼것
TODO:

;include_path = ".:/usr/local/lib/php" --worst
include_path = "/usr/local/lib/php:." -- best
 
% /usr/sbin/apache2 -X &
[1] 16381
% strace -p 16381 -o sys2.txt 
Process 16381 attached - interrupt to quit
Process 16381 detached
% grep stat sys2.txt | grep -v fstat | wc -l
36
All the syscalls
accept(3, {sa_family=AF_INET, sin_port=htons(52362), sin_addr=inet_addr("10.211.55.2")}, [16]) = 9
getsockname(9, {sa_family=AF_INET, sin_port=htons(80), sin_addr=inet_addr("10.211.55.3")}, [16]) = 0
fcntl64(9, F_GETFL)                     = 0x2 (flags O_RDWR)
fcntl64(9, F_SETFL, O_RDWR|O_NONBLOCK)  = 0
read(9, "GET /index.php?action=public HTT"..., 8000) = 230
gettimeofday({1216450700, 390225}, NULL) = 0

3. Cache를 적용 할 것

APC Cache 인스톨
$pecl install apc
vi php.ini
extension="apc.so"
apc.enabled=1
apc.shm_segments=1
apc.shm_size=256
apc.ttl=7200
apc.user_ttl=7200
apc.num_files_hint=1024
apc.mmap_file_mask=/tmp/apc.XXXXXX
apc.enable_cli=1
apc.include_once_override=1

4. 다 해봤는데 어렵다면 프로파일링을 해볼것


Articles

1 2 3