Click here to Skip to main content
15,902,114 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

I am trying to generate 1000 concurrent web request and then waiting for the response.So basically my requirement is to hit n numbers of request sententiously and wait for response of each.
I have written the below code to do so, but I am not able to generate those many request.

C#
static void Main(string[] args)
{

            ServicePointManager.DefaultConnectionLimit = 1000000;
            ServicePointManager.Expect100Continue = false;
            List<HttpWebRequest> request = CreateWebRequest(1000);
            TimeMultipleRequests(request);
}
static List<HttpWebRequest> CreateWebRequest(int no)
        {

            //reading an xml file from location which will be passed to WCF api as http post data
            var path = @"D:\PraveenUpadhyay\abc.xml";
            XmlDocument doc = new XmlDocument();
            doc.Load(path);

            List<HttpWebRequest> webRequest = new List<HttpWebRequest>();

            for (int i = 1; i <= no; i++)
            {
                var request = (HttpWebRequest)WebRequest.Create("http://localhost/TestService.svc/rest/agent/asset");
                request.Method = "POST"; request.ContentType = "application/xml;";
                var data = Encoding.ASCII.GetBytes(doc.InnerXml);
                request.ContentLength = data.Length;
                request.Timeout = 1000 * 60 * 10;
                //request.KeepAlive = true;

                using (var stream = request.GetRequestStream())
                {
                    stream.Write(data, 0, data.Length);
                }

                webRequest.Add(request);
            }

            return webRequest;
        }
private static void TimeMultipleRequests(List<HttpWebRequest> requests)
        {
            var stopWatch = new Stopwatch();
            stopWatch.Start();

            foreach (var request in requests)
            {

                request.GetResponse();
            }

            stopWatch.Stop();
            var timeSpan = stopWatch.Elapsed;
            string s = String.Format("{0:00} hours, {1:00} minutes, {2:00} seconds, {3:00} milliseconds", timeSpan.Hours, timeSpan.Minutes, timeSpan.Seconds, timeSpan.Milliseconds / 10);
            Console.WriteLine(s);
        }
Posted
Comments
Sergey Alexandrovich Kryukov 13-Jul-15 9:59am    
Does it work with at least one request? If it does, increasing of the number of request is not your problem, but rather the problem on the server side.
—SA
Praveen Kumar Upadhyay 13-Jul-15 22:59pm    
Yes it works for even 10 concurrent request, but does not work when I start increasing the count and it is not getting blocked at server side, client itself not able to initiate these many requests concurrently.
Sergey Alexandrovich Kryukov 13-Jul-15 23:35pm    
That said, this is the problem on your server side; and we have no information on that. So, case closed? or do you have something to add?
—SA
Praveen Kumar Upadhyay 13-Jul-15 23:44pm    
Thank you for digging into my case and removing time. What I am trying to ask is I am trying to hit 1000 concurrent connection to my server. Is my code wrong?
Sergey Alexandrovich Kryukov 13-Jul-15 23:48pm    
That's exactly my point. I don't see anything fundamentally wrong in your code and think it's irrelevant to the problem, which is on the server side.
Perhaps someone else will find some problem, but I cannot see it.
Do the simple thing: test your code on some well-known high-performance site, such as Google, Bing, MSDN.
—SA

1 solution

Actually, I can see only one problem in your code: you did not handle possible exceptions, so I'm not sure how exactly the problem appears. So, here are my suggestions:

  • Handle all exceptions on the top stack frame of all threads. Display comprehensive exception information. Just in case.
  • For experimental purpose, increase timeout, even make it infinite. No problem, for experimental purpose, it's would be acceptable even if you kill your process in case of prolonged hanging.
  • Test your code on some well-known high-performance site, such as Google, Bing, MSDN…


I would assume that the problem is your server part, not the code you've shown, but let's get more information and see what's going on in more detail.

—SA
 
Share this answer
 
v3
Comments
Praveen Kumar Upadhyay 17-Jul-15 3:54am    
Thank you for looking into my problem and suggesting your inputs.
Sergey Alexandrovich Kryukov 17-Jul-15 11:10am    
You are very welcome.
Good luck, call again.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900