与 WebRequest 相比,RestSharp 的性能问题
Performance issue for RestSharp compared with WebRequest
我在 restsharp 客户端中发现了一些性能问题,我在函数 PerformanceRequest2 中使用 RestSharp 向某些 restful API 发送请求,并在 PerformRequest[=11= 中使用 WebRequest 使用正常的 .net 请求]
PerformRequest 的平均响应时间为 75 毫秒,而使用 RestSharp 的 PerformRequest2 为 300 毫秒。是 restsharp 的某些限制还是我们在使用 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");
}
}
}
我从以下link得到答案:
http://romikoderbynew.com/2012/01/17/slow-httpwebrequest-getresponse/
应用修复后使用 restsharp 和 httpwebrequest 的性能相当
我在 restsharp 客户端中发现了一些性能问题,我在函数 PerformanceRequest2 中使用 RestSharp 向某些 restful API 发送请求,并在 PerformRequest[=11= 中使用 WebRequest 使用正常的 .net 请求]
PerformRequest 的平均响应时间为 75 毫秒,而使用 RestSharp 的 PerformRequest2 为 300 毫秒。是 restsharp 的某些限制还是我们在使用 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");
}
}
}
我从以下link得到答案:
http://romikoderbynew.com/2012/01/17/slow-httpwebrequest-getresponse/
应用修复后使用 restsharp 和 httpwebrequest 的性能相当