并发参数&优化

影响程序支撑并发量的几个因素

  1. 线程池的线程数
  2. 程序代码本身的性能
  3. IO密集操作是否使用异步
  4. 数据库连接池的大小
  5. 数据库MySQL的最大连接数
  6. SQL语句的性能,以及锁的使用
  7. 硬件&网络本身的性能



线程池参数

有4个本地参数可以调整

  • ThreadPool_MinWorker
  • ThreadPool_MaxWorker
  • ThreadPool_MinIOCP
  • ThreadPool_MaxIOCP

可参考: Nebula参数清单

具体代码可参考:

private static void SetThreadPool()
{
    // .net 默认值:
    // Min Worker Threads: {ProcessorCount}
    // Max Worker Threads: 32767
    //------------------ -
    // Min CompletionPort Threads: {ProcessorCount}
    // Max CompletionPort Threads: 1000

    int coreCount = System.Environment.ProcessorCount.Min(32);  // 最少32个线程

    int minWorker = LocalSettings.GetUInt("ThreadPool_MinWorker", coreCount);
    int maxWorker = LocalSettings.GetUInt("ThreadPool_MaxWorker", 2000);

    int minIOCP = LocalSettings.GetUInt("ThreadPool_MinIOCP", 256);
    int maxIOCP = LocalSettings.GetUInt("ThreadPool_MaxIOCP", 3000);

    if( ThreadPool.SetMaxThreads(maxWorker, maxIOCP) == false )
        Console2.Warnning($"SetMaxThreads({maxWorker}, {maxIOCP}) failed.");

    if( ThreadPool.SetMinThreads(minWorker, minIOCP) == false )
        Console2.Warnning($"SetMinThreads({minWorker}, {minIOCP}) failed.");
}

查看进程当前的线程状态,可以在Venus中查看:

xx



数据库连接池参数

与并发性能有关的参数有二个:

  1. 连接池最大值
  2. 连接超时时间

这二个参数都可以在连接字符串上指定,例如:Maximum Pool Size=300;Connect Timeout=5

  • 对于配置服务来说,它的连接字符串是通过环境变量来指定的,因此直接在环境变量中指定即可。
  • 对于应用库连接来说,可在 dbconfig表的args列中指定

例如:

xx



MySQL的并发控制参数

影响MySQL并发的参数有二个:

[mysqld]
# The maximum amount of concurrent sessions the MySQL server will
# allow. One of these connections will be reserved for a user with
# SUPER privileges to allow the administrator to login even if the
# connection limit has been reached.
max_connections=3000

# Number of threads allowed inside the InnoDB kernel. The optimal value
# depends highly on the application, hardware as well as the OS
# scheduler properties. A too high value may lead to thread thrashing.
innodb_thread_concurrency=128



SQL语句的性能,程序代码本身的性能

ClownFish会记录一些慢SQL,慢请求,可以在性能日志中找到它们。

下图是默认的性能阀值:

xx


可以在配置参数中重新指定它们:

ClownFish_Log_Performance_HttpExecute=1000
ClownFish_Log_Performance_HandleMessage=2000

关于性能日志的更多介绍,可以点击此处