Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WebRequests are slow (taking 4 seconds) how do I speed them up?

I'm doing a web request that is too slow. It takes around 3.2 seconds to GetResponseStream() and .8 seconds to ReadToEnd()

If I run the same request in a web browser I get a response in less than a second... I'm wondering what I can do to speed up the request and what might cause it to be slow?

Based on other questions I saw here, I disabled the proxy in app.config + enabled max connections, just in case (it's still slow). The section I added was:

 <system.net>
<defaultProxy enabled="false">
  <proxy/>
  <bypasslist/>
  <module/>
</defaultProxy>
<connectionManagement>
  <add address="*" maxconnection="65000" />
</connectionManagement>

Here's a screenshot of what the code looks like and what is slow:

alt text

I'd appreciate any help... 4-8 seconds is way too long for a user to wait for an ajax request to complete.

Thanks!

like image 368
rksprst Avatar asked Oct 14 '22 08:10

rksprst


1 Answers

The line

responseReader = new StreamReader(webRequest.GetResponse().GetResponseStream());

does 3 things at once:

  1. It gets the response from the data.
  2. It gets the response stream from the WebResponse object.
  3. It opens the stream.

To improve timing analysis, and therefore understand where the bottleneck is, you first should recode the section to seperate the tasks.

HttpWebResponse response = WebRequest.GetResponse();
Stream s = response.GetResponseStream();
reader = new StreamReader(s);

In the finally block you just have to close the reader, as this also closes the underlying stream. Instead of explicitly closing the stream you could consider using an using block for the reader.

using (Stream s = response.GetResponseStream()) {
    StreamReader reader = new StreamReader(s);
    responseData = reader.ReadToEnd();
}

I know that this is not a solution but maybe it provides a bit more insight as to where the time is lost.

A final suggestion, consider using asynchroneous execution to reduce the duration of the execution as perceived by the user.

like image 148
AxelEckenberger Avatar answered Oct 19 '22 03:10

AxelEckenberger