我们可以在C#应用程序中创建300,000个线程并在PC上运行吗?

And*_*SFT 7 c# multithreading stress-testing

我试图模仿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
Run Code Online (Sandbox Code Playgroud)

现在我正在尝试的可能是不切实际的,但是,如果有办法实现它并且你知道,那么请帮助我.

Flo*_*yon 6

每个线程将创建自己的堆栈和本地存储,你在32位操作系统上看到每个线程大约512k的堆栈空间,我认为堆栈空间在64位操作系统上加倍.快速返回电子表格计算结果为您的300k客户提供了146.484375个堆栈空间.

所以,不,不要创建300k线程,而是使用线程池来模拟300k请求,尽管我认为你会因为几个测试客户端通过网络接口发送垃圾邮件而变得更好.

有很多Web负载测试工具可用.良好的起点:http://www.webperformance.com/library/reports/TestingAspDotNet/