I find some performance issue in the restsharp client, I am sending request to some restful API using RestSharp in function PerformanceRequest2 and using normal .net request using WebRequest in PerformRequest
The average response time for PerformRequest is 75ms while the PerformRequest2 using RestSharp is 300ms. Is that some limitation of restsharp or we did something wrong the way we are using restsharp
private static void PerformRequest2(string requestData)
{
var request = JsonConvert.DeserializeObject<ComplexClass>(requestData);
var client2 = new RestClient("URL");
var restRequest = new RestRequest();
restRequest.Method = Method.PUT;
restRequest.AddHeader("Content-Type", "application/json");
restRequest.AddHeader("Authorization", string.Format("Bearer {0}", token));
restRequest.AddJsonBody(request);
var restResponse2 = client2.Execute(restRequest);
if (restResponse2.StatusCode != HttpStatusCode.OK)
{
throw new Exception("error");
}
}
private static void PerformRequest(string requestData)
{
var request = JsonConvert.DeserializeObject<ComplexClass>(requestData);
var webRequest =
WebRequest.Create("URL");
webRequest.ContentType = "application/json";
webRequest.Method = "PUT";
webRequest.Headers.Add(HttpRequestHeader.Authorization, string.Format("Bearer {0}", token));
using (var streamWriter = new StreamWriter(webRequest.GetRequestStream()))
{
streamWriter.Write(JsonConvert.SerializeObject(request));
streamWriter.Flush();
}
var httpResponse = (HttpWebResponse) webRequest.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
var result = streamReader.ReadToEnd();
if (string.IsNullOrEmpty(result))
{
throw new Exception("error");
}
}
}
I got the answer from the following link:
http://romikoderbynew.com/2012/01/17/slow-httpwebrequest-getresponse/
And the performance of using restsharp and httpwebrequest are comparable after applying the fix