分析 PHP 应用程序,50% 的时间忙于构建类——这样可以吗?
我有一个用 PHP 编写的 Web 应用程序。它使用MySQL进行数据存储。今天,我决定对其进行分析,以查找代码中的瓶颈,并找出其中哪些部分运行得比其他部分慢。平常的东西。我做了很多工作,现在我的页面在桌面上加载的时间不到 0.05 秒。
但现在我的分析器告诉我,我的应用程序有一半时间忙于构建类。 Front
控制器需要 Config
、Database
和 User
类,它们在 __construct 中有自己的东西要做()
,然后加载 Page
控制器,该控制器加载 Cache
和 View
,然后触发 main
方法。所有这些事情占用了总工作时间的 50%。另外 50% 用于查询数据库、处理查询结果并将其输出到 Page
的 ()View
中。
问题是:这样可以吗? “50%用于建设”的事情?这是否意味着我很好地优化了我的应用程序?我被告知网络应用程序最耗时的操作是数据库查询。我优化了它们,应用了一些缓存,现在它们完全受到控制。而我实际上不知道如何优化班级建设。我应该尝试优化这些 __construct() 方法还是保留它们?
I have a web application written in PHP. It uses MySQL for data storage. Today I decided to profile it to find bottlenecks in code and find which parts of it are running slower that the others. The usual stuff. I did a lot of work and now my page loads in less than 0.05 seconds on my desktop.
But now my profiler tells me that half of this time my app is busy constructing classes. Front
controller requires Config
, Database
and User
classes, they have their own stuff to do in __construct()
, then it loads Page
controller, which loads Cache
and View
, and after that it fires the main()
method of Page
. And all that stuff takes 50% of total working time. And the other 50% are used for querying db, juggling query results and outputting them in View
.
The question is: is this okay? The "50% for construction" thing? Does that mean that I optimized my app well? I've been taught that the most time-consuming operations for web app are database queries. I optimized them, applied some caches, and now they are totally under control. And I actually have no clue on how to optimize class construction. Should I try to optimize these __construct()
methods or leave them be?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我不会太担心;如果在完成渲染页面之前没有对数据做很多事情,那么大部分运行时间都会花在类的构造函数中,但这或多或少应该仍然是一个持续的惩罚。如果您在控制器中运行大量逻辑,并且您的分析器仍然告诉您它花费了大量时间来启动您的应用程序,那么您可能需要考虑优化;请记住,过早优化是一件非常糟糕的事情,您的时间最好花在开发新功能或修复错误上,除非应用程序的执行时间足够慢以被视为错误。
I wouldn't worry too much; if you're not doing a lot of stuff to the data before you finish rendering your page the majority of the time running it will be spent in the constructors of your classes, this should more or less remain a constant penalty though. If you have a lot of logic running in the controller and your profiler is still telling you that it's spending a lot of time starting your app then you might want to consider optimising; remember that premature optimisation is a very bad thing, your time is better spent developing new features or fixing bugs unless the app's execution time is slow enough to be considered a bug.
是构造函数还是构造函数正在调用?您是否使用可以查看这 50% 内容的工具?因为大部分工作可能是调用其他函数。
Is it the construct functionality or is it what construct is calling? Are you using a tool where you can see what those 50% are comprised of? Since most of that work could be calling other functions.
我曾经不得不在 Drupal 站点中集成一些 Web 服务。
所以我最终做了一些分析,结果发现 Drupal 引导程序占用了 90% 的时间,而我的代码占用了剩下的 10%。不过,Drupal 的设计很差:),所以它加载了很多很多它并不真正使用的东西。
我认为这取决于你根据你的情况来决定。
I once had to do some web services integrated in a Drupal site.
So I eventually did some profiling only to find out that the Drupal bootstrap was using 90% of the time and my code was taking the rest of 10%. Drupal is poorly engineered tho :), so it's loading lots and lots of stuff it doesn't really use.
I think it's up to you to decide in your case.