bad performance for the code to send Http request

This is a discussion on bad performance for the code to send Http request within the C# Programming forums, part of the General Programming Boards category; Hi guys, I am new to Http in C#. Here is my code segment. My performance test result is 1st ...

  1. #1
    Registered User
    Join Date
    Aug 2008
    Posts
    27

    bad performance for the code to send Http request

    Hi guys,

    I am new to Http in C#. Here is my code segment. My performance test result is 1st time it takes long time, almost 30 seconds, and from the 2nd time to the 10th time, it takes very little time (expected).

    TestBlade is a machine which is located in local LAN. I have tried with IE, very quick response each time.

    Code:
                Uri server = new Uri("http://TestBlade:8080/monitor");
                WebClient myWebClient = new WebClient();
                // open file to read
                byte[] returnRawBytes;
                int counter = 10;
                try
                {
                    Console.WriteLine("Testing TestBlade performance... ");
                    while (counter > 0)
                    {
                        // post data
                        Console.WriteLine("Round " + counter + " Begin time: " + DateTime.Now);
                        returnRawBytes = myWebClient.UploadData(server.AbsoluteUri, System.Text.Encoding.ASCII.GetBytes("Hello server"));
                        Console.WriteLine(System.Text.Encoding.ASCII.GetString(returnRawBytes));
                        Console.WriteLine("Round " + counter + " Complete time: " + DateTime.Now);
                        Thread.Sleep (10000);
                        counter--;
                    }
                }
                catch (Exception e) // get any other error
                {
                    Console.WriteLine(e.ToString());
                    throw;
                }
    Any ideas why 1st time is so slow?

  2. #2
    Registered User
    Join Date
    Aug 2008
    Posts
    27
    One more idea, I found most of time is consumed on method UploadData. Is there any tools I could use to diagnose what is the method doing please?

    Quote Originally Posted by Checker1977 View Post
    Hi guys,

    I am new to Http in C#. Here is my code segment. My performance test result is 1st time it takes long time, almost 30 seconds, and from the 2nd time to the 10th time, it takes very little time (expected).

    TestBlade is a machine which is located in local LAN. I have tried with IE, very quick response each time.

    Code:
                Uri server = new Uri("http://TestBlade:8080/monitor");
                WebClient myWebClient = new WebClient();
                // open file to read
                byte[] returnRawBytes;
                int counter = 10;
                try
                {
                    Console.WriteLine("Testing TestBlade performance... ");
                    while (counter > 0)
                    {
                        // post data
                        Console.WriteLine("Round " + counter + " Begin time: " + DateTime.Now);
                        returnRawBytes = myWebClient.UploadData(server.AbsoluteUri, System.Text.Encoding.ASCII.GetBytes("Hello server"));
                        Console.WriteLine(System.Text.Encoding.ASCII.GetString(returnRawBytes));
                        Console.WriteLine("Round " + counter + " Complete time: " + DateTime.Now);
                        Thread.Sleep (10000);
                        counter--;
                    }
                }
                catch (Exception e) // get any other error
                {
                    Console.WriteLine(e.ToString());
                    throw;
                }
    Any ideas why 1st time is so slow?

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Perhaps a "network sniffer" would help here: it would show what packets go across the network, and you can then see if it's your application that isn't sending the packet for half a minute, or if it's the other end not replying, or such.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Registered User
    Join Date
    Aug 2008
    Posts
    27
    Good idea! I have used tcpdump before. BTW: I just want to make sure there is no profile tool provided by VS or any Windows built-in tools which could provide "better" analysis performance. If they can analyze based on CLR knowledge, the performance analysis will be better -- while TCPDump is only for general purpose analysis. Anyway, TCPDump is enough and fine. :-)

    Quote Originally Posted by matsp View Post
    Perhaps a "network sniffer" would help here: it would show what packets go across the network, and you can then see if it's your application that isn't sending the packet for half a minute, or if it's the other end not replying, or such.

    --
    Mats

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I have never written code for CLR (well, I wrote a program two steps beyond Hello, World, but I don't think that counts).

    I'd expect there are performance tools to analyze CLR code, but I also expect that this won't tell you what the problem is - at least, not unless you KNOW that the packet is sent late, rather than something else.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    You could also step through the code with a debugger and see if any line in particular hangs. Thirty seconds is a magnitude where you don't need a profiler.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  7. #7
    Registered User
    Join Date
    Aug 2008
    Posts
    27
    Thanks man, my code hangs at UploadData. You mean I stepped into this function to see assembly code?

    Quote Originally Posted by CornedBee View Post
    You could also step through the code with a debugger and see if any line in particular hangs. Thirty seconds is a magnitude where you don't need a profiler.

  8. #8
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,651
    If you check the MSDN page, you'll see that "this method supports Network Tracing", and links on how to do that. This would be a good place to investigate.

    Also, what your server is doing with the data you are sending is going to be important as well.

  9. #9
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    I didn't thoroughly read your code since these things are more an issue of forgetting to do something, rather than coding errors, so bear with me if I say anything that is clearly demonstrated in your code.

    Are you correctly following the HTTP protocol? These things are usually, in my experience, caused when you try to upload something while the server is not actually told to listen for a response. If any of this sounds plausible, just say so and I will mess with the code. Its been a while since I have messed with C# so to be honest... I didn't read the code.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  2. << !! Posting Code? Read this First !! >>
    By biosx in forum C++ Programming
    Replies: 1
    Last Post: 03-20-2002, 11:51 AM
  3. Replies: 0
    Last Post: 02-21-2002, 05:05 PM
  4. A send mail application with source code
    By Rizwan Rafique in forum Windows Programming
    Replies: 8
    Last Post: 01-11-2002, 09:22 AM
  5. Bad code or bad compiler?
    By musayume in forum C Programming
    Replies: 3
    Last Post: 10-22-2001, 09:08 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21