具有大量AJAX调用性能的网页 [英] web pages with a lot of AJAX calls performance

查看:93
本文介绍了具有大量AJAX调用性能的网页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的项目是一个网络移动应用程序,它使用大量 AJAX 调用服务器,刷新和检索数据,我使用php脚本从中获取数据服务器。

My project is a web mobile app that uses a lot of AJAX calls to the server, to refresh and retrieve data, I'm using php scripts to get data from the server.

我注意到,并且用户告诉我,有时应用程序性能非常慢。
我相信这是因为 AJAX 调用,我检查了我的服务器,并且在出现此问题时没有任何性能问题。

I noticed, and also users told me, that sometimes the application performance is very slow. I believe that it's because of the AJAX calls, I checked my server and it doesn't has any performance problems when this problem occurs.

这很奇怪,因为当问题发生时,应用程序向服务器发送 AJAX 请求并停止,直到出现超时错误,当再次发送时,它表现完美,并在不到一秒的时间内给出答案。

It is very strange, because when the problem occurs, the application send AJAX request to the server and get stuck until the timeout error appear, and when sending it again, it performs perfectly and give an answer in less than a second.

我检查了类似的问题,我看到使用 php sessions 可以创建文件锁,每个请求都需要等到前一个结束。
此问题取消了AJAX异步过程。
我没有使用会话但是我的情况可能相同而且 php脚本执行文件锁定或其他类型的锁定并取消异步。

I checked similar issues, and i saw that using php sessions can create file lock and every request needs to wait until the previous ends. This issue cancel the AJAX asynchronous process. I am not using sessions but maybe my case is the same and the php scripts perform file lock or other type of locking and cancel the asynchronous.

任何人都知道如何解决这个问题?

Anyone has any idea how can I solve this?

谢谢。

推荐答案

在创建包含大量 AJAX 请求的应用程序时,请记住以下几点:

While creating an application with a lot of AJAX requests, please keep the following in mind:


1。减少Ajax请求的数量

对于初学者来说,可以通过完全不发出Ajax请求来获得最佳性能。这似乎既明显又毫无意义:删除Ajax并不是真的更好 Ajax,是吗?但是有一些方法可以让你的Ajax蛋糕吃掉它。第一个目标不是删除所有Ajax请求,而是删除不必要的请求。或者,如果你想要积极进取,也要减少必要的要求。例如,如果您的脚本每分钟执行相同的Ajax请求,请查看每两分钟是否不合理。如果是这样,那么你只需要减少一半的请求!

For starters, the best performance can be had by not making an Ajax request at all. This may seem both obvious and pointless: dropping Ajax isn’t really better Ajax, is it? But there are ways you can have your Ajax cake and eat it too. The first goal isn’t to cut out all Ajax requests, but the unnecessary ones. Or, if you want to be aggressive, also cut the less necessary requests. If your script, for example, performs the same Ajax request every minute, see if every two minutes isn’t a reasonable alternative. If so, then you’ve just halved the number of requests!


2。选择明智地触发AJAX请求的事件

您需要注意何时何地发出AJAX请求。随着用户体验,数据管理和数据管理的增加,事件和用户动作的重新排序减少了AJAX请求。应密切分析流量。例如,假设您有一个页面,用户可以在该页面中动态地重新排序项目列表。另外,您可能会对订单中的每次更改执行Ajax请求(可能是请求会将新订单保存在数据库中)。但是,用户可能会进行多次更改,从而产生多个请求,而事实是它只是最终的订单才真正重要。一个简单的解决方案是添加一个提交按钮供用户单击并在那时执行单个Ajax请求。

You need to be careful where and when your AJAX request is made. The re-ordering of events and user actions to decrease the AJAX request with the increase in user experience, data management & flow should be closely analysed. For example, say you have a page in which the user can dynamically reorder a list of items. Offhand, you might be tempted to perform an Ajax request with each change in the order (presumably, the request would save the new order in the database). However, it’s likely the user would make multiple changes, resulting in multiple requests, and the fact is that it’s only the final order that really matters. One simple solution would be to add a submit button for the user to click and have the single Ajax request performed at that time.


3。在适当时使用GET请求

说到GET请求类型,您还应该知道GET请求往往比POST。公平地说,您使用的决定应主要基于请求的详细信息:

Speaking of GET request types, you should also know that GET requests tend to perform better than POST. To be fair, the decision as to which you use should mostly be based upon the particulars of the request:


  • GET请求旨在从服务器检索信息。

  • POST请求旨在引起服务器反应,例如更新数据库记录,发送电子邮件等等。

  • GET requests are intended to retrieve information from the server.
  • POST requests are intended to cause a server reaction, such as the updating of a database record, the sending of an email, and so forth.

但GET请求通常更快,如果有任何关于哪个是最合适的问题,那么错误地过度使用GET。

But GET requests are normally faster, so err on the side of overusing GET if there’s any question as to which is most appropriate.


4。减少传输的数据量

Ajax为网页带来的一个好处是它们可以最大限度地减少数据量需要在客户端和服务器之间来回传输。这只是因为完整的网页 - HTML,CSS,JavaScript和各种媒体 - 不需要由浏览器下载并重新绘制,只是为了确认用户名是否可用或者获取最新的股票报价。但是可以编写Ajax请求本身以发送更多或更少的数据。

One benefit that Ajax brings to web pages is that they can minimize the amount of data that needs to be transferred back and forth between the client and the server. This is simply because the complete web page—HTML, CSS, JavaScript, and various media—does not need to be downloaded by the browser, and redrawn, just to confirm that a username is available or to fetch the latest stock quote. But the Ajax request itself can be written to send more or less data.

对于初学者,您可以限制服务器端资源将实际数据传输回JavaScript 。接下来,您应该选择最佳数据格式:

For starters, you can limit what actual data gets transmitted back to JavaScript by your server-side resource. Next, you should choose the best data format:


  • 纯文本

  • JavaScript对象表示法(JSON)

  • 可扩展标记语言(XML)

  • Plain text
  • JavaScript Object Notation (JSON)
  • eXtensible Markup Language (XML)

与GET和POST一样,选择数据格式还有其他因素,但请注意纯文本通常会传输最少的数据,而XML则可能是冗长的。当然,JSON和XML能够表示比纯文本更复杂的数据,但不要将纯文本作为选项解除。

As with GET versus POST, there are other factors in selecting a data format, but do be aware that plain text will normally transmit the fewest bits of data, whereas XML can be verbose. Of course, JSON and XML are able to represent more complex data than plain text ever could, but don’t dismiss plain text as an option.

更高级别,您可以在返回之前压缩服务器上的数据。现代浏览器很好地处理 GZipped 数据,尽管在GZip和两端解压缩数据所需的额外处理方面需要权衡。传输 GZipped 数据是一个更先进的概念,但应该在你的雷达上。

On a more advanced level, you can compress the data on the server before returning it. Modern browsers handle GZipped data well, although there is a trade-off in the extra processing required to GZip and unzip the data on either end. Transmitting GZipped data is a bit more advanced a concept, but one that should be on your radar.


5。使用缓存重复数据

利用浏览器缓存。这仅适用于使用Ajax请求信息时,而不是用于向服务器发送数据时。

Take advantage of browser caching. This only applies when Ajax is being used to request information, not when it’s being used to send data to the server.

缓存的要点是:浏览器将存储站点资源的本地副本,以便在后续请求(在特定时间范围内)不需要重新下载它们。浏览器尝试缓存资源也适用于通过GET方法发出的Ajax请求(这可能会在调试过程中引起麻烦)。因此,如果您的GET请求是可缓存的,那么您将提高Ajax的性能。

The gist of caching is this: The browser will store local copies of site resources so that it won’t need to re-download them on subsequent requests (within a certain time frame). The browser’s attempts to cache resources also apply to Ajax requests made via the GET method (which can cause headaches during debugging). Thus, if your GET requests are cachable, you’ll improve the performance of the Ajax.

但是在使用缓存并处理可用资源的阈值时要非常谨慎。

But be very cautious while using caching and take care of the threshold of available resources.



另请阅读以下内容:


Also please read the following:

  • Making Ajax Elegantly Fast
  • Faster AJAX Web Services through multiple subdomain calls (The information given in this link is .NET based but the same concept can be used)
  • Are WebSockets faster than AJAX? ...with latency in mind? (Not related to the question. Just for consideration as another option wherever possible in your application)



我知道这是非常基本和普遍的信息,但希望这在某种程度上有所帮助......


I know this was very basic and generalized information but hope this helps in some way...

这篇关于具有大量AJAX调用性能的网页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆