c# - 我们能否在 C# 应用程序中创建 300,000 个线程并在 PC 上运行它?

标签 c# multithreading stress-testing

我正在尝试模拟 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/

相关文章:

c# - 如何使用 Newtonsoft.Json 将对象序列化为带有类型信息的 json?

c# - 退出(Quit)实现

C# 从序列化的 json 数组访问值

performance - 我应该对托管网站进行负载测试吗?怎么样?

testing - 在 JMeter 中顺序运行多个线程组

asp.net - 使用WCAT对ASP.NET/IIS进行压力测试

c# - 通过 SmtpClient 发送邮件

multithreading - 如何将对堆栈变量的引用传递给线程?

java - 使用线程 java 阻塞输出直到输入完成

java - Java中的多线程矩阵乘法