Use AJAX or Multithreading to Speed ​​Up Page Loading

I have a page with 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 does not slow down other parts of the web application, nor does it crash IIS.

There are several approaches:

 OrderDTO GetDataForViewOrder(int orderID) { } 

and use multiple threads in this dll. This approach appears to provide the best scalability, but also introduces user interface details into the application services layer.

Which approach do you think is the best and why?

+4
source share
3 answers

Although threads can help with loading a single page (assuming your server has at least 5 processor cores), this is not a scalable approach. What to do if 3 users immediately got into the application? Then you will need 15 cores on the server to improve performance.

AJAX may be a solution, but it suffers from the same scalability problem because each AJAX request will receive its own stream. On the bright side, AJAX provides preliminary speed improvements for the end user, because he can see that something is loading, even if the lagging parts of the page take the same time.

What you really need to see if, due to performance, a database appears, these are asynchronous database queries. You can run 5 asynchronous calls for 5 parts of a page and reduce load times by up to 5 times. This will make the code more complicated though. In addition, if you are trying to combine this with the AJAX approach, you need to look at asynchronous ASP.NET pages or asynchronous WCF services to avoid scalability issues when there are many users, because each user occupies 5 threads.

The code for asynchronous calls will look something 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 } 
+2
source

Ajax.

Threading does not help much, since the whole page must wait for all threads to complete. And it is unlikely that ASP.NET itself takes time. It is more likely that this is your database or something else. Themes will also add complexity to your application without getting much from it. Use only threads in web applications to perform maintenance tasks, etc. Everything else can be solved using ASP.Net.

Using ajax allows you to quickly return the main page, and all sections will be displayed as soon as they return the result from the ajax request.

+2
source

One option is to use asynchronous page loading. But this is all server side processing. Essentially, you register these tasks to be performed asynchronously and expect them to complete. Thus, the HTTP request that will be sent will wait for all tasks to complete.

The second option is ajax. The advantage here is that the page loads quickly. However, this does not work with update panels. Go for the concept of classic ajax, not asp.net ajax. Using libraries like jQuery will help a lot. You can make ajax calls in all five sections after loading the page. You will need to write code to return content for all of these 5 sections.

Of course, perfect ajax requests make sense if the urls are mvc style. But, in my opinion, asp.net handlers approach these mvc style queries in a very small way.

+1
source

Source: https://habr.com/ru/post/1337650/


All Articles