我正在尝试模拟 300,000 个消费者正在访问服务器的场景。所以我试图通过从并发线程重复查询服务器来创建伪客户端。
但首先要扫清的障碍是,是否有可能在一台PC上运行30万个线程?下面是一段代码,我用它来初步了解我可以获得多少个最大线程,然后用实际函数替换测试函数:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
namespace CheckThread
{
class Program
{
static int count;
public static void TestThread(int i)
{
while (true)
{
Console.Write("\rThread Executing : {0}", i);
Thread.Sleep(500);
}
}
static void Main(string[] args)
{
count = 0;
int limit = 0;
if (args.Length != 1)
{
Console.WriteLine("Usage CheckThread <number of threads>");
return;
}
else
{
limit = Convert.ToInt32(args[0]);
}
Console.WriteLine();
while (count < limit)
{
ThreadStart newThread = new ThreadStart(delegate { TestThread(count); });
Thread mythread = new Thread(newThread);
mythread.Start();
Console.WriteLine("Thread # {0}", count++);
}
while (true)
{
Thread.Sleep(30*1000);
}
} // end of main
} // end of CheckThread class
} // end of namespace
现在我正在尝试的可能不切实际,但是,如果有办法做到这一点并且您知道,请帮助我。
最佳答案
每个线程都会创建自己的堆栈和本地存储,您正在查看 32 位操作系统上每个线程大约 512k 的堆栈空间,我认为堆栈空间在 64 位操作系统上翻倍。快速回顾一下电子表格计算,我们为您的 30 万客户提供了 146.484375 GB 的堆栈空间。
所以,不,不要创建 300k 线程,而是使用线程池来模拟 300k 请求,尽管说实话我认为你最好让几个测试客户端通过网络接口(interface)向你的服务器发送垃圾邮件。
有很多 Web 负载测试工具可用。好的起点:http://www.webperformance.com/library/reports/TestingAspDotNet/
关于c# - 我们能否在 C# 应用程序中创建 300,000 个线程并在 PC 上运行它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6266573/