如何在短时间内调用大量的http请求

时间:2015-09-01 15:09:46

标签: c# asp.net performance linq optimization

我必须每隔几秒发送100,000个http请求,并且使用下面提供的代码发送请求需要19秒,而且时间长于我的间隔。

在此期间,CPU使用率和以太网使用率为100%。我在带宽较高但结果相同的双处理器计算机上尝试过它。

还有其他方法可以获得更好的表现。

protected async void btnStartCallWhenAll_Click(object sender, EventArgs e)
{
    t1 = DateTime.Now;
    // Make a list of web addresses. 
    List<string> urlList = SetUpURLList(Convert.ToInt32(txtNoRecordsToAdd.Text));

    // One-step async call. 
    await ProcessAllURLSAsync(urlList);

    t2 = DateTime.Now;
    double spent = t2.Subtract(t1).TotalMilliseconds;
    txtTimeElapsed.Text = " Time Spent :" + spent.ToString() ;
}
private List<string> SetUpURLList(int No)
{
    List<string> urls = new List<string>
    {
    };

    for (int i = 1; i <= No; i++)
        urls.Add("http://msdn.microsoft.com/library/windows/apps/br211380.aspx");

    return urls;
}
private async Task ProcessAllURLSAsync(List<string> urlList)
{
    ServicePointManager.UseNagleAlgorithm = true;
    ServicePointManager.Expect100Continue = true;
    ServicePointManager.CheckCertificateRevocationList = true;
    ServicePointManager.MaxServicePointIdleTime = 10000;
    ServicePointManager.DefaultConnectionLimit = 1000;

    IEnumerable<Task> CallingTasksQuery =
        from url in urlList select CallURLAsync(url);

    Task[] CallingTasks = CallingTasksQuery.ToArray();

    await Task.WhenAll(CallingTasks);
}

private async Task CallURLAsync(string url)
{
    var content = new MemoryStream();
    var webReq = (HttpWebRequest)WebRequest.Create(url);
    using (WebResponse response = await webReq.GetResponseAsync())
    {
    }
}

1 个答案:

答案 0 :(得分:0)

参考HttpWebRequest is extremely slow!

确保您已设置:

webReq.Proxy = null;

似乎WebRequest的启动将查找默认代理。