我试图通过请求许多文件下载来测试另一个应用程序。
所以,我用下面的代码启动了10个WebClient
实例,但看起来我可以同时运行5个。
class Program
{
public static object locker = new object();
public static void Main(string[] args)
{
for (int i = 0; i < 10; i++)
start(i);
Console.ReadLine();
}
private static void start(object row)
{
DateTime start = DateTime.Now;
WebClient client = new WebClient();
client.Credentials = CredentialCache.DefaultNetworkCredentials;
client.DownloadProgressChanged += (sender, e) => {
lock (locker){
double throughput = e.BytesReceived /
(DateTime.Now - start).TotalSeconds / 1024 / 1024;
double error = 1 - (1 / throughput);
Console.SetCursorPosition(0, (int)row);
Console.WriteLine(
@"({0}) {1:HH\:mm\:ss.ffff} - {2:0.00}Mb - " +
@"{3:##0}% - {4:0.00}Mb/s ({5:+0.00%;-0.00%;0.00%}){6}",
row, DateTime.Now, e.BytesReceived / 1024 / 1024,
e.ProgressPercentage, throughput, error, " ");
}
};
client.DownloadFileAsync(
new Uri("http://site/Download.ashx?Id=123"),
String.Format("c:\\foo_{0}.xxx", row));
}
}
我得到了以下输出:
(0)14:51:07.1830-39,00MB-5%-0,94MB/s(-6,45%)
(1)14:51:06.8610-39,00MB-5%-1,00MB/s(+0,24%)
(2)14:51:06.5650-39,00MB-5%-0,99MB/s(-1,34%)
(3)14:51:07.2810-38,00MB-5%-0,95MB/s(-5,12%)
(4)14:51:06.5740-37,00MB-5%-0,95MB/s(-5,19%)
(5)14:50:30.4640-0,00MB-100%-0,01MB/s(-12690,64%)
(6)14:50:30.5390-0,00MB-100%-0,01MB/s(-12845,38%)
(7)14:50:30.8380-0,00MB-100%-0,01MB/s(-13909,70%)
(8)14:50:30.6150-0,00MB-100%-0,01MB/s(-12988,80%)
(9)14:50:30.9210-0,00MB-100%-0,01MB/s(-14079,53%)
我可以改变这个限制来模拟更多并发用户吗?
最佳答案
你是从同一台机器上发射10个吗?查看事件日志。当您添加其他可能保持连接的内容时,您可能会遇到xp及更高版本上对tcp/ip的10个连接限制。