fuller
fuller

Reputation: 41

Setup time Metrics for Webservice Call

I am calling a webservice to send json data by post method. Here is the code for that:

string sample_url = "mywebservice/createsample;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(sample_url);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.CookieContainer = new CookieContainer();
CookieCollection cookcol = new CookieCollection();
cookcol.Add(cookie);

string body= json data
byte[] postBytes = Encoding.UTF8.GetBytes(body);
request.ContentLength = postBytes.Length;
Stream postStream = request.GetRequestStream();
postStream.Write(postBytes, 0, postBytes.Length);

postStream.Close();

WebResponse response = request.GetResponse();
string satus = ((HttpWebResponse)response).StatusDescription;
postStream = response.GetResponseStream();
StreamReader reader = new StreamReader(postStream);

I need to get some timestamp around this webservice request and response as the service seems quite slow. How do I put the countdown timer around this? Thanks for your response.

Upvotes: 0

Views: 143

Answers (2)

Chris Ballard
Chris Ballard

Reputation: 3769

Beware that you aren't testing your own network latency as well. If you are interested specifically in how long each service call takes, you should probably use a Stopwatch as per @nvoigt answer, but put this round your service implementation method.

Upvotes: 0

nvoigt
nvoigt

Reputation: 77324

You can use the Stopwatch class:

Stopwatch sw = new Stopwatch();

sw.Start();

// your code to measure here

sw.Stop();

// print sw.Elapsed

Upvotes: 2

Related Questions