使用Ajax或者多线程加快页面加载多线程、加载、页面、Ajax

2023-09-10 17:32:03 作者:柔情喂了狗

我有一个网页,有5个部分。 每个部分大约需要1秒渲染。

 的Page_Load()
{
   RenderSection1(); // 1秒
   RenderSection2(); // 1秒
   RenderSection3(); // 1秒
   RenderSection4(); // 1秒
   RenderSection5(); // 1秒
}
 

我想,以加快该页面的加载。但同时确保它不会减慢的Web应用程序的其他部分的性能也不会崩溃的IIS。

的几种方法:

使用AJAX请求。需要是MVC样式请求控制器或Web服务。 使用的UpdatePanel周围的每个部分将无法正常工作 - 因为如果我试图在同一时间使用的方法在这里提交刷新到多的UpdatePanel: http://encosia.com/2007/07/ 13 /轻易刷新-AN-的UpdatePanel - 使用JavaScript的/ , 最后一个请求将永远是赢家: 的http://www.$c$cproject.com/Tips/57035/Simultanious-Async-Requests-Using-Multiple-Update-.aspx

在回答描述 如何提取PDF页面 实用方法就在这里了

使用ASP.NET主题 right方法来创建线程在ASP.NET Web应用程序。所以,我会用每次调用一个单独的线程: RenderSection1,RenderSection2,等等...

移动,占用时间,通常是DB请求,为应用服务类的另一个DLL或外部Web服务的逻辑。类似于

  OrderDTO GetDataForViewOrder(INT的OrderID)
{
}
 

和使用多线程的DLL。这种做法似乎提供最佳的可扩展性,而且还引入了UI细节到应用服务层。

哪种方法,你认为是最好的,为什么?

解决方案

虽然线程可以帮助一个单一的页面加载(前提是你的服务器有至少5个CPU核心),它是不可扩展的方法。如果3个用户打在同一时间的应用程序?然后,你需要在服务器上的15内核,实现了性能的提升。

AJAX可以是一个解决方案,但它同样可扩展性问题受到影响,因为每个AJAX请求会得到自己的线程。在光明的一面AJAX给出了preceived速度提升最终用户,因为他能看到一些正在加载,即使页面的laggy部分同时服用。

什你真的需要看看,如果对性能的影响来自于一个数据库是异步的数据库查询。就可以开始为5份的页的5异步调用负荷降低时可能达5倍。这将使code以上虽然复杂。此外,如果你的艇员选拔与您需要查看异步ASP.NET页面或异步WCF服务,以避免可扩展性的问题时,有大量的用户,因为每个用户将占用5个线程的AJAX方法结合起来。

在$ C $下异步调用将大致是这样的:

 
的Page_Load()
{
     BeginDBRequest1();
     BeginDBRequest2();
     BeginDBRequest3();
     BeginDBRequest4();
     BeginDBRequest5();
     DATA1 = EndDBRequest1();
     数据2 = EndDBRequest2();
     数据3 = EndDBRequest3();
     DATA4 = EndDBRequest4();
     数据5 = EndDBRequest5();

     //上面所有的要求采取的最大一次通话的时间,而不是时间的总和

     RenderSection1(DATA1); // 1秒
     RenderSection2(数据2); // 1秒
     RenderSection3(数据3); // 1秒
     RenderSection4(DATA4); // 1秒
     RenderSection5(数据5); // 1秒
}
 

I have a page that has 5 sections. Each section takes about 1 second to render.

Page_Load()
{
   RenderSection1();  //1 sec
   RenderSection2();  //1 sec
   RenderSection3();  //1 sec
   RenderSection4();  //1 sec                  
   RenderSection5();  //1 sec
}

I would like to speed up the loading of this page. But at the same time make sure that it don't slow down performance of other parts of web application and also do not crash the IIS.

The are several approaches:

Use AJAX requests. Needs to be MVC style requests to Controller or Web Service. Using UpdatePanel around each section will not work - since if I try to submit refreshes to multiple UpdatePanels at the same time using approach here: http://encosia.com/2007/07/13/easily-refresh-an-updatepanel-using-javascript/, the last request will always win: http://www.codeproject.com/Tips/57035/Simultanious-Async-Requests-Using-Multiple-Update-.aspx

Use ASP.NET threads described in answer to right way to create thread in ASP.NET web application. So I would use a separate thread for each call: RenderSection1, RenderSection2, etc...

Move the logic that takes up time, usually DB requests, into Application Service class in another DLL or External Web Service. Something like

OrderDTO GetDataForViewOrder(int orderID)
{
}

and use multiple threads in that DLL. This approach seems to provide the best scalability, but also introduces UI details into Application Services layer.

Which approach do you think is the best and why?

解决方案

While threads can help with a single page loads (provided that your server has at least 5 CPU cores) it is not scalable approach. What if 3 users hit the app at the same time? Then you will need 15 cores on the server to achieve the performance boost.

AJAX can be a solution but it suffers from the same scalability issues because each AJAX request will get its own thread. On the bright side AJAX gives a preceived speed improvements for the end user because he can see something is loading even if the laggy parts of the page take the same time.

Wha you really need to look at if the performance hit comes from a database is asynchronous DB queries. You can start 5 asynchronous calls for the 5 parts of the page and reduce the load time potentially up to 5 times. It will make the code more complex though. Also if you are chosing to combine this with the AJAX approach you need to look at asynchronous ASP.NET pages or asynchronous WCF services to avoid scalability problems when there are a lot of users because every user will take up 5 threads.

The code for async calls would roughly look like this:


Page_Load()  
{
     BeginDBRequest1();
     BeginDBRequest2();
     BeginDBRequest3();
     BeginDBRequest4();
     BeginDBRequest5();
     data1 = EndDBRequest1();
     data2 = EndDBRequest2();
     data3 = EndDBRequest3();
     data4 = EndDBRequest4();
     data5 = EndDBRequest5();

     //all of the above calls take the time of the max time call and not the sum of the times

     RenderSection1(data1);  //1 sec
     RenderSection2(data2);  //1 sec
     RenderSection3(data3);  //1 sec
     RenderSection4(data4);  //1 sec
     RenderSection5(data5);  //1 sec  
}