当用户开始觉得你的网页缓慢,就可以着手考虑优化的问题了。
个人感觉,这应该分为两方面,一是代码架构方面的,看看有没有什么问题,二是系统级别的,是不是租用的服务器硬件不足了?
但是,真正做起来,不能只凭感觉,得借助于工具。慢,为什么慢?哪里慢?只有找出来,才可能做优化。
首先看一下系统检测工具,http://www.cyberciti.biz/tips/top-linux-monitoring-tools.html 这里列举了20个常用的linux检测工具,因为我的网站比较小,所以仅选择了一些基本的工具,比如top、ps、vmstat、iostat、netstat、iptraf、nethogs等。
安装方式就不说了,不外乎自己下载源码以及相关的依赖包,或者借助于yum、emerge之类的自动安装工具。
以下也是边摸索边记录,不一定连贯。据说,一般服务器的性能瓶颈可能为:内存大小、内存i/o、磁盘i/o、网络i/o、cpu等。
1、通过web server的status监控页面,我们的QPS其实不高。
2、top命令查看,cpu 也很空闲
3、vmstat -S m 5 查看内存使用:
flykobe@server nethogs $ vmstat -S m 5
procs ———–memory— —swap– —–io—- –system– —–cpu——
r b swpd free buff cache si so bi bo in cs us sy id wa st
0 0 36 90 186 2801 0 0 10 57 4 2 0 0 99 0 0
0 0 36 87 186 2801 0 0 0 386 1136 504 1 1 98 0 0
0 0 36 87 186 2801 0 0 0 497 1240 355 1 0 98 1 0
0 0 36 87 186 2801 0 0 0 134 1126 126 0 0 100 0 0
0 0 36 87 186 2801 0 0 0 108 1083 135 0 0 100 0 0
0 0 36 86 186 2801 0 0 0 302 1114 303 1 1 98 1 0
0 0 36 86 186 2801 0 0 0 317 1095 181 0 0 100 0 0
flykobe@server nethogs $ vmstat -S m 5 -f
522105 forks
其中,procs列出进程相关信息,r是run的进程数目,b是block的进程数目;memory列出内存信息,包括虚拟内存,swpd是已使用的虚拟内存大小,free是空闲内存大小,buff是用作buff的内存大小,cache是用作cache的内存大小,单位都是M,因为我使用了-S m命令指定了单位。swap si是每秒从硬盘交换到内存的数量,so是内存交换到硬盘的数量。io的bi是接收到的块,bo是发送的块个数,单位是块/每秒。system in是每秒中断的数目,cs是上下文交换的数目。具体如下:
Procs
r: The number of processes waiting for run time.
b: The number of processes in uninterruptible sleep.
Memory
swpd: the amount of virtual memory used.
free: the amount of idle memory.
buff: the amount of memory used as buffers.
cache: the amount of memory used as cache.
inact: the amount of inactive memory. (-a option)
active: the amount of active memory. (-a option)
Swap
si: Amount of memory swapped in from disk (/s).
so: Amount of memory swapped to disk (/s).
IO
bi: Blocks received from a block device (blocks/s).
bo: Blocks sent to a block device (blocks/s).
System
in: The number of interrupts per second, including the clock.
cs: The number of context switches per second.
CPU
These are percentages of total CPU time.
us: Time spent running non-kernel code. (user time, including nice time)
sy: Time spent running kernel code. (system time)
id: Time spent idle. Prior to Linux 2.5.41, this includes IO-wait time.
wa: Time spent waiting for IO. Prior to Linux 2.5.41, included in idle.
st: Time stolen from a virtual machine. Prior to Linux 2.6.11, unknown.
从cpu、内存角度考虑,我们服务器应该是没有问题的。
另外,查看具体进程的内存占用:
ps axu|grep ‘php-cgi\|PID’|less 其中RSS列是内存使用,单位是kB。
4、再利用iostat看下磁盘i/o是否有问题:
flykobe@server nethogs $ iostat -dx
Linux 2.6.18-194.el5 (localhost.localdomain) 2011年01月25日
Device: rrqm/s wrqm/s r/s w/s rsec/s wsec/s avgrq-sz avgqu-sz await svctm %util
sda 0.10 9.43 0.30 9.54 40.89 151.77 19.57 0.09 8.97 0.65 0.64
sdb 0.10 9.42 0.30 9.55 40.99 151.77 19.56 0.09 8.92 0.63 0.62
dm-0 0.00 0.00 0.78 18.97 81.77 151.77 11.82 0.79 39.97 0.41 0.80
dm-1 0.00 0.00 0.00 0.00 0.00 0.00 2.01 0.00 3.14 0.47 0.00
dm-2 0.00 0.00 0.78 18.97 81.77 151.77 11.82 0.79 39.97 0.41 0.80
dm-3 0.00 0.00 0.78 18.97 81.76 151.74 11.83 0.79 39.97 0.41 0.80
dm-4 0.00 0.00 0.00 0.00 0.01 0.03 8.00 0.00 53.95 2.36 0.00
需要关注的是r/s、w/s列,代表每秒read、write操作的次数,那么对于我们的服务器,为什么会是写操作反而大于读操作呢?
但是,从这里的数据显示,应该并不算是一个忙碌的磁盘i/o系统,所以暂时先不管它。
对磁盘读写测速:
flykobe@server ~ $ cat /etc/fstab
/dev/VolGroup00/LogVol00 / ext3 defaults 1 1
LABEL=/boot /boot ext3 defaults 1 2
tmpfs /dev/shm tmpfs defaults 0 0
devpts /dev/pts devpts gid=5,mode=620 0 0
sysfs /sys sysfs defaults 0 0
proc /proc proc defaults 0 0
/dev/VolGroup00/LogVol01 swap swap defaults 0 0
flykobe@server ~ $ sudo /sbin/hdparm -t /dev/VolGroup00/LogVol00
/dev/VolGroup00/LogVol00:
Timing buffered disk reads: 136 MB in 4.76 seconds = 28.55 MB/sec
5、再使用netstat命令,得到网络的基本情况:
flykobe@server nethogs $ netstat -Ieth0 -e
Kernel Interface table
eth0 Link encap:Ethernet HWaddr 00:15:17:B6:E0:B4
inet addr:202.85.221.240 Bcast:202.85.221.255 Mask:255.255.255.128
inet6 addr: fe80::215:17ff:feb6:e0b4/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:111512895 errors:0 dropped:0 overruns:0 frame:0
TX packets:153435934 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:100
RX bytes:16224517492 (15.1 GiB) TX bytes:185118605369 (172.4 GiB)
Memory:b8820000-b8840000
其中RX packets代表收到的包数目,TX packets代表发送的包数目。
而要查看当前的连接情况,可以使用sudo netstat -tulpn。
这里,我看不出问题,因为我们更应该关心的是网络带宽和流量,所以再借助于iptraf命令,它是一个图形化(命令行模式下的图形,蓝屏的)的监测网络状态的命令。它有5种模式:
| IP traffic monitor |
| General interface statistics |
| Detailed interface statistics |
| Statistical breakdowns… |
| LAN station monitor |
它可以获得每秒接收和发送的流量,像我们,针对80端口(web端口),in的带宽占用是64kbits/s,out的带宽占用是1270kbit/s。虽然并不大,但是鉴于我们的bandwidth目前仅2.4m,则很容易就造成带宽不足了。我测试了一下,当访问网站上的一个flash时,out的带宽轻易到达2m以上。
另外,到达是哪个进程占用了网络资源呢?我想当然的认为是web server,但是仍需要验证一下,这时就可以使用nethogs了。
NetHogs version 0.7.0
PID USER PROGRAM DEV SENT RECEIVED
14077 wwwrun /opt/lighttpd/sbin/lighttpd eth0 2.392 0.813 KB/sec
0 root ..0:39029-123.125.106.19:80 0.129 0.594 KB/sec
0 root ..240:80-117.87.238.87:3364 6.895 0.324 KB/sec
6195 yicheng sshd: yicheng@pts/1 eth0 1.172 0.188 KB/sec
0 root ..40:80-116.231.16.207:4425 0.119 0.131 KB/sec
0 root ..0:39026-123.125.106.19:80 0.108 0.108 KB/sec
0 root ..0:39028-123.125.106.19:80 0.108 0.108 KB/sec
0 root ..0:39027-123.125.106.19:80 0.108 0.108 KB/sec
6848 yicheng sshd: yicheng@pts/2 eth0 0.306 0.035 KB/sec
0 root ..0:80-58.216.156.134:14843 0.000 0.012 KB/sec
0 root ..240:80-222.247.56.14:1417 0.011 0.012 KB/sec
0 root ..40:80-116.231.16.207:4322 0.011 0.012 KB/sec
0 root unknown TCP 0.000 0.000 KB/sec
TOTAL 11.358 2.443 KB/sec
那么如果说瓶颈真的出在网络i/o上,解决方法是什么呢?