简体   繁体   English

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

[英]php class performance question…what should I do?

I'm building a webapp using php, ajax, javascript, mysql. 我正在使用php,ajax,javascript,mysql构建一个webapp。 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 ok 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. 性能对我来说很重要......超过5,000名用户应该能够使用该应用程序而不会出现太多性能问题。 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: 这是基本的webapp功能:

user executes a call via the browser through onclick event --> ajax call to phpPG1.php. 用户通过浏览器通过onclick事件 - > ajax调用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. - > phpPG1.php对数据库运行查询并将结果存储在数组中,然后包含另一个名为HTMLphp.php的php页面,从HTMLphp.php页面实例化新对象,这是类的一个类 - > calles函数并传递包含查询结果的数组 - >类函数构建HTML表并传回一个字符串 - > phpPG1.php页面将带有表数据的字符串发送回ajax调用,该调用显示字符串在它所属的给定DIV标签中。

The HTMLphp.php contains all the functions that are used to return HTML tables for the whole webapp. HTMLphp.php包含用于为整个webapp返回HTML表的所有函数。 HTMLphp.php would look something like this: 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
   }

}

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. HTMLphp.php页面现在有5,606行代码,它代表了类中100个左右的函数。 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. 基本上,我的webapp中的每个页面都“包含”此页面,以便能够使用类函数来显示html表。 I'm about half way done with the webapp, so there will be a lot more lines of code do add to this file. 我大约已经完成了一半的webapp,所以会有更多的代码行添加到这个文件中。 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. 我对计算机如何执行代码并不完全一致,特别是在使用类对象时,但我理解“解释性语言”如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: 我想知道这是不是一个坏主意,如果我应该做这样的事情:对于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;
   }
}

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? 我的基本假设是我只包含调用特定函数时所需的HTML,因此减少了HTMLphp.php页面的整体大小,假设这有助于整体网站性能...我在左边的字段中思维? 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? 用一个5000到10,000个用户的网站来担心这个问题可以忽略不计吗?

Thanks. 谢谢。

You really have over 100 functions in your class? 你班上真的有100多个功能吗?
Probably (no I am 110% sure) your class is not well designed then, especially if it is "only" for HTML table creation. 可能 (不是我110%肯定)你的课程设计不好,特别是如果它只是“仅”用于创建HTML表格。

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. 那不是OOP。 And it is hard to maintain the code. 并且很难维护代码。

100 methods in one class is way, way too much. 一个类中的100个方法太多了。 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 . PHP的上下文中学习OOP ,也可以阅读有关Design Patterns的内容

Edit: 编辑:

I just saw that you have a function to create every HTML in your application, is this correct? 我刚看到你有一个函数在你的应用程序中创建每个HTML,这是正确的吗? If so you really should consider to build your tables dynamically, which will result in less code and is probably easier to maintain. 如果是这样,你真的应该考虑动态构建表,这将导致更少的代码,并且可能更容易维护。

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. 在函数/方法中包含特定的PHP文件而不是open include肯定会有所帮助,因为只包含和解释所需的项目。 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. 但PHP的intrepreter仍然需要通过那个庞大的类的5000行代码,恕我直言,这是一个非常糟糕的方式,并将成为一个主要的瓶颈。

In reality, 5000-10000 users is not a whole lot, but it again depends on how the usage is going to be. 实际上,5000-10000用户并不是很多,但它又取决于使用方式。 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. 如果所有这些都可以同时登录,并在您的服务器上发出请求(类似于每分钟10k请求或大约166 req / s)并且几乎没有涉及缓存,那么它可能是一个严重的瓶颈但又一次取决于很多因素。 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. 相反,一个好的方法是使用某种负载测试工具,如abJMeter,并找到真实的。

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. 您可以使用APC或Memcache来实现缓存,或其他各种方法来提高性能。 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. 但是在黑暗中疯狂拍摄,我会说5k线类是你应该考虑拆分的东西,不仅仅是出于性能原因而且还有一个好的设计。 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. 或许,如果构建这些HTML片段的逻辑不是太复杂,您可以通过将数据作为JSON / XML发送到客户端,并让Javascript使用此数据构建表。

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. 现在它读取了几百行而不是5,000行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM