【发布时间】:2018-06-26 20:32:28
【问题描述】:
我试图模拟 300,000 名消费者访问服务器的场景。所以我试图通过从并发线程重复查询服务器来创建伪客户端。
但是要清除的第一个障碍是,是否可以在 PC 上运行 300,000 个线程?这是我用来初步查看我可以获得多少最大线程的代码,然后用实际函数替换测试函数:
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
现在我正在尝试的可能是不现实的,但是,如果有办法做到这一点并且你知道,那么请帮助我。
【问题讨论】:
-
看起来像 stackoverflow.com/questions/145312/… 的副本
-
如果“运行”是指线程之间的颠簸……呵呵
-
很抱歉我没有很好地阐明目的。我的设置是服务器-客户端设置。我想强调客户端而不是服务器。在设置中,进程向正在运行的客户端进程发出请求,而客户端进程将依次将请求转发给要服务的服务器。但我明白我们都在趋同的一点,在一台机器上创建这么多线程是不现实的,不推荐。此外,我打算让这些线程至少每 1 分钟执行一次来给客户端造成压力的动机似乎太过分了! :(
标签: c# multithreading stress-testing