C# .net 提升 asp.net mvc, asp.net core mvc 並發量


1.提升System.Net.ServicePointManager.DefaultConnectionLimit

2.提升最小工作線程數

------

DefaultConnectionLimit在asp.net mvc4中默認就是int.max ,不需要調整,若是不放心,判斷下,然后加大。

DefaultConnectionLimit在asp.net core mvc 中默認比較小,需要加大。

使用ThreadPool.GetMinThreads(out workerThreads, out completePortsThreads); 方法可以得到當前“最小工作線程數”和“最小IO工作線程數”,這兩個值默認等於CPU核心數,我這里等於6.

顯然,這點並發處理能力是不夠的。需要提升,但又不能超過“最大工作線程數”除以2,超過后,又被還原成默認值了 (ThreadPool.SetMinThreads 任意一方超過,兩者都會被還原為默認值)。

方式一,在代碼中加大(.net mvc 在Application_Start(),.net core mvc 在Program.Main ,只要在合適的時機調整即可):

以下是.net core 中運行的實際情況:

 

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 中加大。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM