php 类性能问题...我该怎么办?

发布于 2024-08-18 05:31:39 字数 1732 浏览 8 评论 0 原文

我正在使用 php、ajax、javascript、mysql 构建一个网络应用程序。我有一段时间一直担心某件事,但不确定这是否真的是一个问题。这是代码如何工作的基本概念...我想知道我现在是否应该更改它,或者是否可以按原样使用。性能对我来说很重要...超过 5,000 个用户应该能够使用该应用程序而不会出现太大的性能问题。我担心的部分原因只是作为自学开发人员的偏执,而不了解所有的最佳实践。

这是基本的 webapp 功能:

用户通过浏览器通过 onclick 事件执行调用 --> ajax 调用 phpPG1.php。 --> phpPG1.php 对数据库运行查询并将结果存储在数组中,然后包含另一个名为 HTMLphp.php 的 php 页面,从 HTMLphp.php 页面实例化新对象,这是一个类 -->调用类的函数并传递包含查询结果的数组 --> 类函数构建 HTML 表并传回字符串 --> phpPG1.php 页面将带有表数据的字符串发送回 ajax 调用,该调用在其所属的给定 DIV 标记中显示该字符串。

HTMLphp.php 包含用于返回整个 Web 应用程序的 HTML 表的所有函数。 HTMLphp.php 看起来像这样:

Class HTML_stuff
{
   function html_TABLE1($results_array)
   {

      $string = 'THE HTML TABLE WITH ITS DATA IN IT'
      return $string
   }

   function html_TABLE2($results_array)
   {

      $string = 'THE HTML TABLE WITH ITS DATA IN IT'
      return $string
   }

}

所以,这是我的问题。 HTMLphp.php 页面现在有 5,606 行代码,代表类中的 100 个左右的函数。基本上,我的 web 应用程序中的每个页面都“包含”此页面,以便能够使用类函数来显示 html 表。我的 web 应用程序已经完成了大约一半,因此还会有更多行代码添加到此文件中。我不太了解计算机如何执行代码,特别是在使用类对象时,但我了解像 php 这样的“解释性语言”的基础知识。

我想知道这是否是一个坏主意,我是否应该这样做:对于 HTML_stuff 类中的每个函数,只需删除每个函数的代码并将其放置在它自己的单独的 .php 页面中,该页面会像这样包含在内:

Class HTML_stuff
{
   function html_TABLE1($results_array)
   {
      include_once 'TABLE1.php'; //the html table for this function.
      return $string;
   }

   function html_TABLE2($results_array)
   {
      include_once 'TABLE2.php';
      return $string;
   }
}

我的基本假设是,我只包含调用特定函数时所需的 HTML,从而减少 HTMLphp.php 页面的整体大小,假设这将有助于提高网站的整体性能...我是否在左侧字段中思维?我的一部分想法是,这与第一个选项完全一样,只是组织方式不同,它会对整体性能产生任何影响。然而,我确实在某处读到,“包含”越少,性能越好。

其他人做了什么,或者是否有其他关于如何做此类事情的最佳实践?对于一个拥有 5,000 - 10,000 位用户的网站来说,这种担心可以忽略不计吗?

谢谢。

I'm building a webapp using php, ajax, javascript, mysql. I've been worring about something for a while, but not sure if it's really a problem or not. Here's the basic concept of how the code is working...I would like to know if I should change it now, or if it is o.k to use as is. Performance is important to me...over 5,000 users should be able to use the app without much of a performance problem. Part of my worrying is just paranoia of being self taught developer and not knowing all the best practices out there.

Here's the basic webapp functionality:

user executes a call via the browser through onclick event --> ajax call to phpPG1.php. --> phpPG1.php runs a query against the db and stores the results in array, then includes another php page called HTMLphp.php, instantiates new object from HTMLphp.php page, which is a class --> calles function of the class and passes the array that contains the results of a query-->the class functions builds the HTML table and passes back a string --> the phpPG1.php page sends back the string with the table data to the ajax call which displays the string in the given DIV tag it belongs in.

The HTMLphp.php contains all the functions that are used to return HTML tables for the whole webapp. HTMLphp.php would look something like this:

Class HTML_stuff
{
   function html_TABLE1($results_array)
   {

      $string = 'THE HTML TABLE WITH ITS DATA IN IT'
      return $string
   }

   function html_TABLE2($results_array)
   {

      $string = 'THE HTML TABLE WITH ITS DATA IN IT'
      return $string
   }

}

So, here's my question. the HTMLphp.php page has 5,606 lines of code in it right now, which represents 100 or so functions in the class. Basically, every page in my webapp "includes" this page into it in order to be able to use the class functions to display the html tables. I'm about half way done with the webapp, so there will be a lot more lines of code do add to this file. I'm not exactly up to par on how computers execute code, especially when using class objects, but I understand the basics if "interpretive languages" like php.

I'm wondering if this is a bad idea and if I should do something like this: For each function inside the HTML_stuff class just simply remove the code for each function and place it in it's own separate .php page that gets included like this:

Class HTML_stuff
{
   function html_TABLE1($results_array)
   {
      include_once 'TABLE1.php'; //the html table for this function.
      return $string;
   }

   function html_TABLE2($results_array)
   {
      include_once 'TABLE2.php';
      return $string;
   }
}

My basic assumption is that I only include the HTML needed when I make the call to the particular function, thus reducing the overall size of the HTMLphp.php page assuming this would help in overall site performance...am i in left field with this thinking? Part of me is thinking that this is simply the same as the first option, just organized differently and it would do anything to the overall performance. However, I did read somewhere that fewer "includes" is better for performance.

What do other people do, or are there other best practices on how to do this sort of thing? Is it negligible to worry about this with a site of say 5,000 - 10,000 users?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

秋凉 2024-08-25 05:31:39

在函数/方法中包含特定的 PHP 文件而不是开放包含肯定会有所帮助,因为只有所需的项目才会被包含和解释。但是 PHP 解释器仍然需要遍历那个巨大类的 5000 行代码,恕我直言,这是一个非常糟糕的方法,并且将成为一个主要瓶颈。

实际上,5000-10000 个用户并不是很多,但这又取决于使用情况。如果所有这些都可以同时登录,并在您的服务器上发出请求(大约每分钟 10k 个请求或大约 166 个请求/秒),并且很少或根本不涉及缓存,那么它可能会成为一个严重的瓶颈,但同样如此取决于很多因素。相反,一个好的方法是使用某种负载测试工具,例如 abJMeter 并真正找到它。

如果完成这些测试后结果看起来不太好,那么找出瓶颈所在。您可以使用 APC 或 Memcache 来实现缓存,或使用各种其他方法来提高性能。但在黑暗中大胆尝试一下,我想说 5k 线类是您应该考虑拆分的东西,不仅是出于性能原因,而且也是为了良好的设计。也许,如果构建这些 HTML 片段的逻辑不太复杂,您可以通过将数据作为 JSON/XML 发送,并让 Javascript 使用此数据构建表来将其卸载到客户端。

Including the specific PHP file inside a function/method rather than an open include will definitely help things as only the needed items will get included and interpreted. But the PHP intrepreter will still need to go through those 5000 lines code of that huge class, which IMHO is a really bad way to go about it, and will be a major bottleneck.

In reality, 5000-10000 users is not a whole lot, but it again depends on how the usage is going to be. If all of them could be logged in simultaneously, and shooting out requests at your server (something like 10k requests per minute or 166 req/s approx.) and there's little or no caching involved, then it could be a serious bottleneck but that again depends on a lot of factors. Instead, a good way to go about it would be to use some sort of load testing tool like ab or JMeter and find it out for real.

If the results don't look so good after doing these tests, then find out what the bottleneck is. You could use APC or Memcache to implement caching, or various other ways to improve performance. But taking a wild shot in the dark, I'd say that 5k line class is something that you should consider splitting, not just for performance reasons but also for a good design. And perhaps, if the logic of building those HTML pieces is not too complex, you could offload that to the client by sending the data as JSON/XML, and letting Javascript construct the table using this data.

寒冷纷飞旳雪 2024-08-25 05:31:39

你的类中真的有超过 100 个函数吗?
可能(不,我 110% 确定)你的类设计得不好,特别是如果它“仅”用于 HTML 表格创建。

面向对象编程是一种很好的技术,但你必须正确使用它。在我看来,您只是将所有程序功能放在一处。那不是面向对象编程。而且代码很难维护。

一个类中有 100 个方法实在是太多了。重构您的代码,将其放入多个类中,并仅导入您真正需要的那些。即使不了解您的应用程序的更多信息,我也可以说它(和您)将从中受益。

我不能让你凭良心使用这样的课程;)

你问你应该做什么?
OOP /manual/en/language.oop5.php" rel="nofollow noreferrer">PHP 或许可以阅读一些关于 设计模式

编辑:

我刚刚看到您有一个函数可以在您的应用程序中创建每个 HTML,这是正确的吗?如果是这样,您确实应该考虑动态构建表,这将减少代码并且可能更容易维护。

You really have over 100 functions in your class?
Probably (no I am 110% sure) your class is not well designed then, especially if it is "only" for HTML table creation.

Object Oriented Programming is a good technique but you have to use it correctly. It sounds to me that you just put all the procedural functions into one place. That is not OOP. And it is hard to maintain the code.

100 methods in one class is way, way too much. Refactor your code, put it into multiple classes and only import those you really need. And even without knowing more about your application I can say that it (and you) will benefit from it.

I cannot let you use such a class with good conscience ;)

You ask what you should do?
Learn OOP in the context of PHP and maybe read something about Design Patterns.

Edit:

I just saw that you have a function to create every HTML in your application, is this correct? If so you really should consider to build your tables dynamically, which will result in less code and is probably easier to maintain.

慵挽 2024-08-25 05:31:39

问题解决了。我将代码更改为仅读取调用时所需的函数。现在它可以读取几百行而不是 5,000 行。

Problem solved. I changed to code to only read in the function that was needed at the time of the call. Now it reads in a few hundred lines instead of 5,000.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文