[原创中文翻译]symfony askeet24:第十九天,网站性能和缓存。
October 9, 2007 – 12:55 am[欢迎转载,转载请注名出处http://symfony.net.cn。本文英文版权归symfony官方网站所有]
Load testing tools
负载测试工具
Unit tests, described during the fifteenth day, can validate that the application works as expected if there is only one user connected to it at a time. But as soon as you release your application on the Internet - and that’s the least we can wish for you - hordes of hectic fans will rush to it simultaneously, and performance issues may occur. The web server might even fail and need a manual restart, and this is a really painful experience that you should prevent at all costs. This is especially important during the early days of your application, when the first users quickly draw conclusions about it and decide to spread the word or not.
第十五天我们讲解了单元测试,可以测试一个用户访问时你的网站是否成功运行。但是一旦网站在互联网上发布——保守的估计——成群结队的粉丝会蜂拥而至,性能问题可能会出现。web服务器会瘫痪并且需要手动重新启动,这都是真实痛苦经历,我们要不惜一切代价来避免发生。在早期程序设计中这是很重要的,要争取到第一批用户的口碑。
To avoid performance issues, it is necessary to simulate numerous concurrent access to your website to see how it reacts - before releasing it. This is called load testing. Basically, you program an automate to post concurrent requests to your web server, and measure the return time.
为了避免性能导致的问题,对网站进行并发测试,看看结果——测试在发布前进行。这叫负载测试。基本上,就是程序发送并发请求到你的网站,记录回应时间。
(译者:这里简单翻译一下大体意思就停止不翻译了。根据笔者实际使用,负载测试工具有很多,askeet教材选用的多为轻量级别开源产品,说实话实现功能有限,确实比不了商业软件。在这里推荐广大测试工程师使用——LoadRunner,虽然价格不菲(几万美金而已… …不要扔鼠标和键盘给我~!救命~!),负载工具手册对操作都写得简洁明了,其中最重要的是负载测试结果分析,以后笔者会联系几个系统工程师把LoadRunner测试结果分析方法发表上来以飨读者。)
Improve performances with the cache
用缓存提高网站性能
There are a lot of ways to increase the performance of a given page, including code profiling, database request optimization, addition of indexes, creation of an alternative light web server dedicated to the media of the website, etc. Existing techniques are either cross-language or PHP-specific, and browsing the web or buying a good book about it will teach you how to become a performance guru.
提高网站性能的办法有许多,包括代码整理,数据库优化,加索引,从服务器到网站的优化,等等。现存的技术要么是cross-language,要么是PHP-specific,上网或者买本书看看,学学怎么做网站性能优化。
Symfony adds a certain overload to web requests, since the configuration and the framework classes are loaded for each request, and because the MVC separation and the ORM abstraction result in more code to execute. Although this overhead is relatively low (as compared to other frameworks or languages), symfony also provides ways to balance the response time with caching. The result of an action, or even a full page, can be written in a file on the hard disk of the web server, and this file is reused when a similar request is requested again. This considerably boosts performance, since all the database accesses, decoration, and action execution are bypassed completely. You will find more information about caching in symfony in the cache chapter of the symfony book.
symfony提供了web负载方案,涉及到框架类、配置文件、MVC模型和ORM结果。尽管成本很低(和其他框架比),symfony还是提供了缩短回应时间的缓存。动作的结果,甚至是整个页,可以被写在服务器硬盘上的一个文件里,当有类似请求发送过来时,文件内容被重用。这有利于提高网站性能,因为所有数据库查询,页面代码和动作执行都被绕过而不执行。在symfony book的cache章节里有更多介绍。
We will try to use HTML cache to speed up the delivery of the popular tags page. As it includes a complex SQL query, it is a good candidate for caching. First, let’s see how long it takes to load it with the current code:
尽量用HTML缓存来提高主要人气页的访问速度。正因为这是一个复杂的sql查询,缓存起来很好。首先,先看看用当前代码载入页面需要多少时间:
$ ab -c 1 -n 30 http://askeet/popular_tags
…
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 0 0.0 0 0
Processing: 147 148 2.4 148 154
Waiting: 138 139 2.3 139 145
Total: 147 148 2.4 148 154
…
Put the result of the action in the cache
把动作结果放到缓存里
Warning: The following will not work on symfony 0.6. Please jump to the next section until this tutorial is updated.
警告:如果使用symfony 0.6版本,这些方法无法使用,在本篇教材未更新前就不要看了。
The action executed to display the list of popular tags is tag/popular. To put the result of this action in cache, all we have to do is to create a cache.yml file in the askeet/apps/frontend/modules/tag/config/ directory with:
tag/popular动作来显示人气标签列表。把动作结果放到缓存里,我们创建cache.yml文件在askeet/apps/frontend/modules/tag/config/下:
popular:
activate: on
type: slot
all:
lifeTime: 600
This activates the slot type cache for this action. The result of the action (the view) will be stored in a file in the cache/frontend/prod/template/askeet/popular_tags/slot.cache file, and this file will be used instead of calling the action for the next 600 seconds (10 minutes) after it has been created. This means that the popular tags page will be processed every ten minutes, and in between, the cache version will be used in place.
激活动作的slot缓存。动作的结果(视图)在cache/frontend/prod/template/askeet/popular_tags/slot.cache文件里储存,这文件被用来在下面的600秒内(10分钟)调用。这意味着流行标签页面涉及的动作每10分钟运行一次,之间的时间如果有人访问,用缓存显示页面。
The caching is done at the first request, so you just need to browse to:
缓存在第一次请求时执行,用浏览器访问:
…to create a cache version of the template. Now, all the calls to this page for the next 10 minutes should be faster, and we will check that immediately by running the Apache benchmarking tool again:
… …给模板创建缓存。现在,页面在下面10分钟里访问速度都会很快,让我们用apache benchmarking(就是apache AB… …)工具测试一下:
$ ab -c 1 -n 30 http://askeet/popular_tags
…
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 0 0.0 0 0
Processing: 137 138 2.0 138 144
Waiting: 128 129 2.0 129 135
Total: 137 138 2.0 138 144
…
We passed from an average of 148ms to 138ms, that’s a 7% increase in performance. The cache system improves the performance in a significant way.
我们的响应时间平均从148MS减少到138MS,提高了7%的性能。缓存提高了网站性能。
(译者:这里先到此为止,个人对缓存了解非常有限,笔者只使用过PHP smarty,呵呵!怕翻译出现重大失误,所以先不翻译后面章节了。如果调试中发现后面的知识能用,再把缓存部分全部翻译完。)