如何最大限度地缩短请求的响应时间
我使用 CakePHP 一段时间了。从事广泛的项目是非常容易和伟大的。我一直在控制器功能中进行大部分繁重的计算和数据库访问。并且仅使用视图来显示用户感兴趣的数据。到目前为止,它一直运行良好。并且没有出现任何问题。 但我想问的是,“这是使用 PHP 框架的正确方法吗?有没有其他方法可以最大限度地减少处理请求所需的时间。 谢谢。
I've been using CakePHP for a while now. It is very easy and great to work on projects of wide range. I've been doing most of heavy calculations and database access in controller function. And only using the views to display the data which user is interested in. It has been working great till now. And no problem has occured.
But I want to ask is that, "Is it a right way to work with a framework in PHP. Is there any other way i can minimize the time required to process the request.
Thank You.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
APC
提高性能的最简单方法是使用(安装)APC。 APC通过预编译字节码并将其存储在内存中(内存很快)来帮助您提高性能。如果您使用共享主机提供商,则您没有足够的能力这样做,但是如果您的网站变大并收到大量流量,您应该放弃共享主机。你应该购买一个 VPS,如果你的网站变得太大,你应该购买决定的机器来托管。最终的解决方案如果您变得非常大,您应该运行/购买云系统。
缓存
接下来,您应该在内存中缓存您的数据,以使用 APC 再次加速您的网站。如果您的网站变得很大并且一个盒子不再能满足您的需要,您应该将数据缓存在例如 redis 或 memcached。有些人也从一开始就开始使用redis/memcached。比如redis(FAST => KICKASS)也是一个非常强大的内存数据库。
容易实现的目标
之后,您应该使用诸如 siege 之类的工具对您的网站进行基准测试并解决容易实现的目标(不要浪费时间做 愚蠢的微优化)通过使用像 valgrind/calgrind 这样的工具。 Rasmus lerdorf(PHP 创建者)的这段“简单即困难”视频解释了他如何实现这一目标。
CakePHP 基准
作为旁注,我还想指出 Rasmus Lerdorf(PHP 创建者)执行了一些基准< /a> 过去在 CakePHP(CakePHP 1.2.0rc2) 上运行过,感觉速度慢得要命(可能已经改变了,因为基准测试是在 2008 年或其他时候执行的)。但我认为这些幻灯片非常有趣。根据幻灯片,我建议您切换到 Codeigniter(您的框架越轻,您的性能就越好)作为您的框架。
APC
The easiest way to improve your performance is to use(install) APC. APC helps you improve performance by precompiling bytecode and store it in memory(memory is fast). If you are on a shared hosting provider you haven't got the luxury to do this, but then again if your site gets big and receives a lot of traffic you should move away from shared hosting. You should buy a VPS and if your site gets even too big for this you should buy decided machine for hosting. The final solution if you are getting really big you should run/buy a cloud system.
CACHE
Next you should CACHE your data in memory to speed up your site again using APC. If your site gets big and one box doesn't cut it anymore you should cache your data in for example redis or memcached. Some people also start with using redis/memcached from the beginning. For example redis(FAST => KICKASS) is also a very powerful in memory database.
Low hanging fruit
After that you should benchmark your site using a tool like for example siege and tackle the low hanging fruit(don't wast your time doing silly micro-optimizations) by using a tool like for example valgrind/calgrind. This video "Simple is Hard" from Rasmus lerdorf(PHP creator) explains how he achieves this.
CakePHP Benchmarks
As a sidenote I would also like to point out that Rasmus Lerdorf(PHP creator) performed some benchmarks on CakePHP(CakePHP 1.2.0rc2) in the past and experienced it was slow as hell(Could have changed because the benchmarks have been performed in 2008 or something). but I thought these slides where really interesting. Based on the slides I would recommend you to switch to Codeigniter(the lighter your framework is the better your performance is going to be) as your framework.