C# .net 提升 asp.net mvc, asp.net core mvc 并发量

1.提升System.Net.ServicePointManager.DefaultConnectionLimit

2.提升最小工作线程数

使用ThreadPool.GetMinThreads(out workerThreads, out completePortsThreads); 方法可以得到当前“最小工作线程数”和“最小IO工作线程数”,这两个值默认等于CPU核心数,我这里等于6.

显然,这点并发处理能力是不够的。需要提升,但又不能超过“最大工作线程数”除以2,超过后,又被还原成默认值了。

方式一,在代码中加大(.net mvc 在Application_Start(),.net core mvc 在Program.Main ,只要在合适的时机调整即可):

StringBuilder scLog = new StringBuilder();

            try
            {

                 
                scLog.AppendLine("默认的 System.Net.ServicePointManager.DefaultConnectionLimit:" + System.Net.ServicePointManager.DefaultConnectionLimit.ToString());
                if (System.Net.ServicePointManager.DefaultConnectionLimit <= 300)
                {
                    System.Net.ServicePointManager.DefaultConnectionLimit = 300;
                    scLog.AppendLine("调整后 System.Net.ServicePointManager.DefaultConnectionLimit:" + System.Net.ServicePointManager.DefaultConnectionLimit.ToString());
                }

                int workerThreads;//工作线程数
                int completePortsThreads; //异步I/O 线程数
                ThreadPool.GetMinThreads(out workerThreads, out completePortsThreads);
                string thMin = string.Format("默认的 最小工作线程数{0},最小IO工作线程数{1}", workerThreads, completePortsThreads);
                scLog.AppendLine(thMin);

                int maxT;//最大工作线程数
                int maxIO;//最大IO工作线程数
                ThreadPool.GetMaxThreads(out maxT, out maxIO);
                thMin = string.Format("默认的 最大工作线程数 {0},最大IO工作线程数{1}", maxT, maxIO);
                scLog.AppendLine(thMin);

                int blogCnt = 300;
                if (workerThreads < blogCnt)
                {
                    ThreadPool.SetMinThreads(blogCnt, blogCnt); // MinThreads 值不要超过 (max_thread /2  ),否则会不生效。要不然就同时加大设置max_thread

                    ThreadPool.GetMinThreads(out workerThreads, out completePortsThreads);//确认是否修改成功
                    thMin = string.Format("调整后 最小工作线程数{0},最小IO工作线程数{1}", workerThreads, completePortsThreads);
                    scLog.AppendLine(thMin);
                }

                ThreadPool.GetMaxThreads(out maxT, out maxIO);
                thMin = string.Format("再看一次 最大工作线程数 {0},最大IO工作线程数{1}", maxT, maxIO);
                scLog.AppendLine(thMin);

            }
            catch (Exception ex)
            {
                scLog.AppendLine("index ex:" + ex.Message);
            }

     // mylog           scLog.ToString()

结果:

默认的 System.Net.ServicePointManager.DefaultConnectionLimit:2
调整后 System.Net.ServicePointManager.DefaultConnectionLimit:300
默认的 最小工作线程数6,最小IO工作线程数6
默认的 最大工作线程数 32767,最大IO工作线程数1000
调整后 最小工作线程数300,最小IO工作线程数300
再看一次 最大工作线程数 32767,最大IO工作线程数1000

其它:
asp.net mvc 也可以在machine.config 中加大。

猜你喜欢

转载自www.cnblogs.com/runliuv/p/11965530.html