Speed ​​up HTTP speed

I am writing an application to send text messages via HTTP messages in the Slooce Tech API. Since the application will have to send a large amount of text messages, I am trying to optimize its speed.

The second part of the code below is the method that I use to send messages. I wrote the first piece of code and left HTTPWebResponse to try to make it faster.

The problem is that the new method is actually slower and instead of executing 0.25 seconds to execute, it takes a second or more number and sometimes loops.

Does anyone know why he would do this or any other tips to improve the speed of this application? I added Request.Proxy=null and it speeds it up a bit.

Thanks.

Modified Code:

  public void QuickSend() { XML = "<message id=\"" + lMessageID + "\"><partnerpassword>" + PartnerPassword + "</partnerpassword><content>" + sMessage + "</content></message>"; URL = "http://sloocetech.net:****/spi-war/spi/" + PartnerID + "/" + sRecipient + "/" + Keyword + "/messages/mt"; HttpWebRequest Request = (HttpWebRequest)WebRequest.Create(URL); RequestBytes = System.Text.Encoding.ASCII.GetBytes(XML); Request.Method = "POST"; Request.ContentType = "text/xml;charset=utf-8"; Request.ContentLength = RequestBytes.Length; RequestStream = Request.GetRequestStream(); RequestStream.Write(RequestBytes, 0, RequestBytes.Length); RequestStream.Close(); } 

And here is the source code:

  public XDocument SendSMS() { XML = "<message id=\""+ lMessageID +"\"><partnerpassword>" + PartnerPassword + "</partnerpassword><content>" + sMessage + "</content></message>"; URL = "http://sloocetech.net:****/spi-war/spi/" + PartnerID + "/" + sRecipient + "/" + Keyword + "/messages/mt"; HttpWebRequest Request = (HttpWebRequest)WebRequest.Create(URL); RequestBytes = System.Text.Encoding.ASCII.GetBytes(XML); Request.Method = "POST"; Request.ContentType = "text/xml;charset=utf-8"; Request.ContentLength = RequestBytes.Length; RequestStream = Request.GetRequestStream(); RequestStream.Write(RequestBytes, 0, RequestBytes.Length); RequestStream.Close(); HttpWebResponse Resp = (HttpWebResponse)Request.GetResponse(); oReader = new StreamReader(Resp.GetResponseStream(), System.Text.Encoding.Default); string backstr = oReader.ReadToEnd(); oReader.Close(); Resp.Close(); Doc = XDocument.Parse(backstr); return Doc; } 
+4
source share
1 answer

First of all, I would be skeptical of any claims that you will see significant improvements, because you create your HttpWebRequest special way. The bottleneck for single-threaded requests like yours will be network latency and server response time. (Perhaps they do most of the server processing before responding to your request).

You are making a lock request, which means your processor is not doing anything while it is waiting for a response.

If you want to use a multi-threaded application, you can do something like the following:

 var tasks = new Task[10]; for (int i = 0; i < 10; i++) { tasks[i] = Task.Factory.StartNew(() => { int messages_sent_by_one_task = 0; while(messages_sent_by_one_task < 10) { QuickSend(); messages_sent_by_one_task++; } }); } while (tasks.Any(t => !t.IsCompleted)) { } //wait for tasks to complete 

This will trigger 10 tasks, each of which will send 10 messages. If one answer takes a long time, the remaining 9 threads will continue to rejoice.

I believe that you can probably improve this, you should have included asynchronous requests and HTTPClient , so each of your 10 threads has never been blocked. However, I do not feel ready to give an example, since I have never tried this.

You may be tempted to push the number of threads to some unholy number, but avoid the temptation. The overhead of creating and managing flows will soon catch up with you. I don’t know what the ideal number is, but you can experiment.

+2
source

All Articles