1

I am using this code to perform a simple REST request. (The code mostly comes from this q: How to post JSON to the server?).

Why is it so slow? I'm using VS 2013 and it takes about 15 secs on first try and then about 4 secs. on subsequent tries, yet in another language (Delphi) I can make a http request and it takes about 1 sec consistently.

        var request = (HttpWebRequest)WebRequest.Create("http://jsonplaceholder.typicode.com/posts");
        request.ContentType = "application/json";
        request.Method = "POST";
        request.ServicePoint.Expect100Continue = false;

        using (var streamWriter = new StreamWriter(request.GetRequestStream()))
        {
            string json = new JavaScriptSerializer().Serialize(new
            {
                title = "foo",
                body = "bar",
                userId = "1"
            }); 
            streamWriter.Write(json);
        }

        var response = (HttpWebResponse)request.GetResponse();
        using (var streamReader = new StreamReader(response.GetResponseStream()))
        {
            var result = streamReader.ReadToEnd();
            textBox1.Text = result;
        }

P.S. You can test this code for yourself, it is simply using a test REST server from the internet at above url.

Community
  • 1
  • 1
RaelB
  • 3,301
  • 5
  • 35
  • 55
  • 1
    What do you mean by first try? First try after the reboot? First try when your process is run? Or something else? – Sriram Sakthivel May 10 '15 at 12:17
  • Are you posting exactly the same thing each time? Normally most delays with APIs is waiting on the other sever, so your own code has little impact – kyrenia May 10 '15 at 13:05
  • @SriramSakthivel: Yes, good question :). It means the first try after I leave the computer for a while. I have not measured exactly. Maybe a few minutes. If I do the second try immediately after the first try, then it takes about 4 secs. So there are 2 questions here: 1) Why is there such a difference between first try and second try, and 2) Why is this so slow (i.e 4 secs) compared to other environment which takes 1 sec. – RaelB May 10 '15 at 13:06
  • @kyrenia: Yes. I do not think the delay is on the server. This takes consistently 1 sec from a different client (Delphi) – RaelB May 10 '15 at 13:08
  • Pause the debugger during the long pause and see what's on the stack. Should be framework code. The method names tell you what's going on. – usr May 10 '15 at 13:22

1 Answers1

2

What do you mean by first try? It means the first try after I leave the computer for a while

Before reaching the server, there is a process of finding the IP address of the server. This process is called Dns Resolution.

First time, your application has to go through the process of Dns Resolution in order to find the IP address. Once you resolved the IP address, the IP address will be cached in the local machine.

So, further calls doesn't go through the process of Dns Resolution; it can use the cached IP. After a while, the cache will be dropped and again you'll hit the DNS server for resolving the IP address.

This is the only explanation I can come up for the delay you're noticing. Whenever you're noticing a delay, that probably means that you're hitting the Dns Server just because it is either the first time or cache is expired.

Why it is faster in other environment(Delphi)?

I'm sorry I can't come up with a good reason for this.

Sriram Sakthivel
  • 72,067
  • 7
  • 111
  • 189
  • Thanks, this must be it. I did some further testing, which made things clearer: I put the code in a loop and saw that the first iteration was 15s, but all subsequent iterations were 0.5s. So the code is fine, just the first time that is slow. It is still somewhat peculiar that if I run the program a second time right after the first time, then the first iteration is 3s. It should be 15s (dns cleared) or 0.5s.... – RaelB May 10 '15 at 21:17
  • @RaelB There are multiple levels of caching involved there. One with your local machine, another in your router, your Dns Server itself maintains cache etc. So, it could be that one or more cache expired but some other cache was still valid. So that could answer why you see 3s instead of 15s. I'm just guessing; I could be wrong. But this can give you some more idea. – Sriram Sakthivel May 10 '15 at 21:20