在最近新系统的设计中对于首页打开速度的问题做了一些考虑,由于页面上还是以动态数据为主,所以简单的静态化数据可能并不行,所以需要从业务和技术结合的角度去考虑这个问题。由于有些数据并不敏感,其实可以放入缓存并定时更新。另外就是有些本身是静态,能否加快并发相应速度。
这里对动态数据想到了2种办法:1)把Action设置成单例,请求的结果直接加在缓存里,再用一个定时线程去刷新这个缓存;2)让输出的结果页静态化,定时更新结果页。
前者控制起来非常灵活,利用双缓冲也能让人感觉不到缓冲时的速度下降,缺点就是要改动比较多的代码。后者正好相反,静态化要依靠外层的框架配置,代码完全不用改动。
选型
目前比较流行的反向代理服务器有squid和nginx,查了一下资料,发现nginx比较擅长反向代理和负载均衡,而squid强在自身的缓存机制,另外lightd貌似多用来做图片服务器。经过比较感觉nginx的配置文件简单一些,所以决定用这个先尝试一下。
性能测试
安装完成当然是先做一下性能测试了,这次同样使用ab来进行压力测试。为了做对比,这里也启动了一个tomcat,nginx配置成其的反向代理。我就在本机进行访问,但测试结果令我惊叹。
测试命令:ab -n 10000 -k -c 1000 :8080/pscms_hlj_web/images/logo.jpg
tomcat:
Server Software: Apache-Coyote/1.1 Server Hostname: localhost Server Port: 8080 Document Path: /pscms_hlj_web/images/logo.jpg Document Length: 17619 bytes Concurrency Level: 1000 Time taken for tests: 3.671 seconds Complete requests: 4254 Failed requests: 0 Write errors: 0 Keep-Alive requests: 4254 Total transferred: 76035996 bytes HTML transferred: 74951226 bytes Requests per second: 1158.92 [#/sec] (mean) Time per request: 862.874 [ms] (mean) Time per request: 0.863 [ms] (mean, across all concurrent requests) Transfer rate: 20228.99 [Kbytes/sec] received Connection Times (ms) min mean[+/-sd] median max Connect: 0 33 67.4 0 261 Processing: 52 255 60.1 272 315 Waiting: 15 129 61.7 127 261 Total: 52 288 108.0 272 559nginx:
Server Software: nginx/0.7.67 Server Hostname: localhost Server Port: 8000 Document Path: /pscms_hlj_web/images/logo.jpg Document Length: 17619 bytes Concurrency Level: 1000 Time taken for tests: 0.597 seconds Complete requests: 10000 Failed requests: 0 Write errors: 0 Keep-Alive requests: 10000 Total transferred: 179370000 bytes HTML transferred: 176190000 bytes Requests per second: 16744.67 [#/sec] (mean) Time per request: 59.721 [ms] (mean) Time per request: 0.060 [ms] (mean, across all concurrent requests) Transfer rate: 293309.69 [Kbytes/sec] received Connection Times (ms) min mean[+/-sd] median max Connect: 0 2 8.1 0 44 Processing: 0 49 19.7 48 97 Waiting: 0 44 19.8 42 93 Total: 0 51 19.4 52 99