我与 Kohana 建立的一个网站昨天因大量流量而受到猛烈抨击,这让我退后一步并评估了一些设计.我很好奇有哪些标准技术可以优化基于 Kohana 的应用程序?
我也对基准测试感兴趣.我是否需要为每个控制器方法设置 Benchmark::start()
和 Benchmark::stop()
以查看所有页面的执行时间,或者我是能否在全球范围内快速应用基准测试?
我将在未来更多地使用缓存库,但我愿意接受更多建议,因为我确信我可以做很多我目前不知道的事情.
我在这个回答中要说的不是 Kohana 特有的,可能适用于很多 PHP 项目.
在谈论性能、可扩展性、PHP 时,我想到了以下几点……
我在几个项目中使用了很多这样的想法——它们很有帮助;所以他们也可以在这里提供帮助.
首先,说到表演,有很多方面/问题需要考虑:
(顺便说一句,如果我没记错的话,第二个屏幕截图中显示的调用图通常是 WinCacheGrind 和 Webgrind 都无法做到的 ^^)
(感谢@Mikushi 的评论) 另一种我用得不多的可能性是 xhprof 扩展:它还有助于分析,可以生成调用图——但比 Xdebug 轻,这意味着你应该能够将它安装在生产服务器上.>
您应该可以单独使用它XHGui,它将帮助数据可视化.
既然我们已经谈到了 PHP,请注意您的瓶颈很可能不是 PHP 方面的事情,而是数据库方面的...
>这里至少有两三件事:
EXPLAIN
指令,如果你使用的是 MySQLlog_slow_queries
获取花费太多"时间的请求列表,并通过这些请求开始优化.不过,最重要的两件事是:
如果你还在阅读,还有什么可以优化的?
嗯,还有改进的空间......一些面向架构的想法可能是:
好吧,也许其中一些想法在您的情况下有点矫枉过正^^
但是,仍然......为什么不研究一下,以防万一?;-)
您最初的问题是关于优化使用 Kohana 的应用程序...好吧,我已经发布了一些适用于任何 PHP 应用程序的想法...这意味着它们也适用于 Kohana;-)
(即使不是特定于它^^)
我说:使用缓存;Kohana 似乎支持一些缓存内容 (你自己讨论过,所以没什么新鲜的这里...)
如果有什么可以快速完成的,请尝试;-)
我还说过你不应该做任何不必要的事情;Kohana 中是否默认启用了您不需要的任何功能?
浏览网络,似乎至少有一些关于XSS过滤的东西;你需要那个吗?
不过,这里有几个可能有用的链接:
最后,一个简单的想法:
我并不是说你不应该优化:你绝对应该!
但是首先要进行快速"优化,这会给您带来丰厚的回报:使用一些操作码缓存可能会帮助您将服务器的 CPU 负载降低 10% 到 50% ......而且它需要只需几分钟即可设置 ;-) 另一方面,花 3 天时间获得 2%...
哦,顺便说一句:在做任何事情之前:放置一些监控的东西,这样你就知道做了哪些改进,以及如何改进!
如果没有监控,您将不知道您所做的事情的效果......即使它是否是真正的优化也不知道!
例如,您可以使用类似 RRDtool + <强>仙人掌.
向你的老板展示一些 CPU 负载下降 40% 的漂亮图形总是很棒;-)
无论如何,最后得出结论:玩得开心!
(是的,优化很有趣!)
(呃,我不认为我会写那么多......希望至少其中的某些部分是有用的......我应该记住这个答案:可能在其他时候有用......)
A site I built with Kohana was slammed with an enormous amount of traffic yesterday, causing me to take a step back and evaluate some of the design. I'm curious what are some standard techniques for optimizing Kohana-based applications?
I'm interested in benchmarking as well. Do I need to setup Benchmark::start()
and Benchmark::stop()
for each controller-method in order to see execution times for all pages, or am I able to apply benchmarking globally and quickly?
I will be using the Cache-library more in time to come, but I am open to more suggestions as I'm sure there's a lot I can do that I'm simply not aware of at the moment.
What I will say in this answer is not specific to Kohana, and can probably apply to lots of PHP projects.
Here are some points that come to my mind when talking about performance, scalability, PHP, ...
I've used many of those ideas while working on several projects -- and they helped; so they could probably help here too.
First of all, when it comes to performances, there are many aspects/questions that are to consider:
The first thing that could be really useful is using a reverse proxy, like varnish, in front of your webserver: let it cache as many things as possible, so only requests that really need PHP/MySQL calculations (and, of course, some other requests, when they are not in the cache of the proxy) make it to Apache/PHP/MySQL.
About using a reverse-proxy as cache, for a PHP application, you can, for instance, take a look at Benchmark Results Show 400%-700% Increase In Server Capabilities with APC and Squid Cache.
(Yep, they are using Squid, and I was talking about varnish -- that's just another possibility ^^ Varnish being more recent, but more dedicated to caching)
If you do that well enough, and manage to stop re-generating too many pages again and again, maybe you won't even have to optimize any of your code ;-)
At least, maybe not in any kind of rush... And it's always better to perform optimizations when you are not under too much presure...
As a sidenote: you are saying in the OP:
A site I built with Kohana was slammed with an enormous amount of traffic yesterday,
This is the kind of sudden situation where a reverse-proxy can literally save the day, if your website can deal with not being up to date by the second:
About that, How can I detect and survive being "Slashdotted"? might be an interesting read.
First of all: are you using a recent version of PHP? There are regularly improvements in speed, with new versions ;-)
For instance, take a look at Benchmark of PHP Branches 3.0 through 5.3-CVS.
Note that performances is quite a good reason to use PHP 5.3 (I've made some benchmarks (in French), and results are great)...
Another pretty good reason being, of course, that PHP 5.2 has reached its end of life, and is not maintained anymore!
[apc.stat](https://php.net/manual/en/apc.configuration.php#ini.apc.stat)
can be good for system-load; but it means modifications made to PHP files won't be take into account unless you flush the whole opcode-cache; about that, for more details, see for instance To stat() Or Not To stat()?As much as possible, it is better to avoid doing the same thing over and over again.
The main thing I'm thinking about is, of course, SQL Queries: many of your pages probably do the same queries, and the results of some of those is probably almost always the same... Which means lots of "useless" queries made to the database, which has to spend time serving the same data over and over again.
Of course, this is true for other stuff, like Web Services calls, fetching information from other websites, heavy calculations, ...
It might be very interesting for you to identify:
And store these data/results in some kind of cache, so they are easier to get -- faster -- and you don't have to go to your SQL server for "nothing".
Great caching mechanisms are, for instance:
I'm pretty sure your framework comes with some cache-related stuff; you probably already know that, as you said "I will be using the Cache-library more in time to come" in the OP ;-)
Now, a nice thing to do would be to use the Xdebug extension to profile your application: it often allows to find a couple of weak-spots quite easily -- at least, if there is any function that takes lots of time.
Configured properly, it will generate profiling files that can be analysed with some graphic tools, such as:
For instance, here are a couple screenshots of KCacheGrind:
(source: pascal-martin.fr)
(source: pascal-martin.fr)
(BTW, the callgraph presented on the second screenshot is typically something neither WinCacheGrind nor Webgrind can do, if I remember correctly ^^ )
(Thanks @Mikushi for the comment) Another possibility that I haven't used much is the the xhprof extension : it also helps with profiling, can generate callgraphs -- but is lighter than Xdebug, which mean you should be able to install it on a production server.
You should be able to use it alonside XHGui, which will help for the visualisation of data.
Now that we've spoken a bit about PHP, note that it is more than possible that your bottleneck isn't the PHP-side of things, but the database one...
At least two or three things, here:
EXPLAIN
instruction, if you are using MySQL
log_slow_queries
to get a list of the requests that take "too much" time, and start your optimization by those.Still, the two most important things are:
If you are still reading, what else could be optimized?
Well, there is still room for improvements... A couple of architecture-oriented ideas might be:
Well, maybe some of those ideas are a bit overkill in your situation ^^
But, still... Why not study them a bit, just in case ? ;-)
Your initial question was about optimizing an application that uses Kohana... Well, I've posted some ideas that are true for any PHP application... Which means they are true for Kohana too ;-)
(Even if not specific to it ^^)
I said: use cache; Kohana seems to support some caching stuff (You talked about it yourself, so nothing new here...)
If there is anything that can be done quickly, try it ;-)
I also said you shouldn't do anything that's not necessary; is there anything enabled by default in Kohana that you don't need?
Browsing the net, it seems there is at least something about XSS filtering; do you need that?
Still, here's a couple of links that might be useful:
And, to conclude, a simple thought:
I'm not saying you shouldn't optimize: you definitely should!
But go for "quick" optimizations that will get you big rewards first: using some opcode cache might help you get between 10 and 50 percent off your server's CPU-load... And it takes only a couple of minutes to set up ;-) On the other side, spending 3 days for 2 percent...
Oh, and, btw: before doing anything: put some monitoring stuff in place, so you know what improvements have been made, and how!
Without monitoring, you will have no idea of the effect of what you did... Not even if it's a real optimization or not!
For instance, you could use something like RRDtool + cacti.
And showing your boss some nice graphics with a 40% CPU-load drop is always great ;-)
Anyway, and to really conclude: have fun!
(Yes, optimizing is fun!)
(Ergh, I didn't think I would write that much... Hope at least some parts of this are useful... And I should remember this answer: might be useful some other times...)
这篇关于优化基于 Kohana 的网站的速度和可扩展性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!