首页 新闻 会员 周边 捐助

asp.net core ab并发测试连接超时?

0
悬赏园豆:50 [已解决问题] 解决于 2021-02-04 00:02

public class LocationController : Controller
{
MySqlConnection myConnnect;
MySqlCommand sqlCmd;

    public   LocationController()
    {
        string constructorString = "server=127.0.0.1;User Id=root;password=123456;Database=test;";
       
        myConnnect = new MySqlConnection(constructorString);
        sqlCmd = new MySqlCommand();
        sqlCmd.Connection = myConnnect;
        sqlCmd.CommandText = "select * from location where parent_id=0";
    }

    public async Task<List<Dictionary<string, string>>> Index()
    { 
        var list = new List<Dictionary<string, string>>();

        await myConnnect.OpenAsync();
        using (var reader = await sqlCmd.ExecuteReaderAsync())
        {
            while (await reader.ReadAsync())
            {
                var data = new Dictionary<string, string>();
                data["id"] = reader.GetInt32(0).ToString();
                data["spell_first"] = reader.GetString(2);
                data["spell_full"] = reader.GetString(3);
                data["name"] = reader.GetString(4);
                list.Add(data);
            }
        }
        await myConnnect.CloseAsync();

        return list;
    }
}

ab.exe -c 1000 -n 50000 http://192.168.2.104:5000/location
ubuntu20.04
压测的时候。这段代码 dotnet进程cpu很高 压测的时候出现以下错误(没出错的时候qps 才600):
Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware[1]
An unhandled exception has occurred while executing the request.
MySql.Data.MySqlClient.MySqlException (0x80004005): Unable to connect to any of the specified MySQL hosts.
---> MySql.Data.MySqlClient.MySqlException (0x80004005): Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding.
at MySql.Data.Common.StreamCreator.GetTcpStream(MySqlConnectionStringBuilder settings)
at MySql.Data.MySqlClient.NativeDriver.Open()
at MySql.Data.MySqlClient.NativeDriver.Open()
at MySql.Data.MySqlClient.Driver.Open()
at MySql.Data.MySqlClient.Driver.Create(MySqlConnectionStringBuilder settings)
at MySql.Data.MySqlClient.MySqlPool.CreateNewPooledConnection()
at MySql.Data.MySqlClient.MySqlPool.GetPooledConnection()
at MySql.Data.MySqlClient.MySqlPool.TryToGetDriver()
at MySql.Data.MySqlClient.MySqlPool.GetConnection()
at MySql.Data.MySqlClient.MySqlConnection.Open()
at System.Data.Common.DbConnection.OpenAsync(CancellationToken cancellationToken)
--- End of stack trace from previous location ---
at site.Controllers.LocationController.Index() in D:\project\hefei\site\site\Controllers\LocationController.cs:line 30
at lambda_method4(Closure , Object )
at Microsoft.AspNetCore.Mvc.Infrastructure.ActionMethodExecutor.AwaitableObjectResultExecutor.Execute(IActionResultTypeMapper mapper, ObjectMethodExecutor executor, Object controller, Object[] arguments)
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeActionMethodAsync>g__Awaited|12_0(ControllerActionInvoker invoker, ValueTask`1 actionResultValueTask)
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeNextActionFilterAsync>g__Awaited|10_0(ControllerActionInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Rethrow(ActionExecutedContextSealed context)
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeInnerFilterAsync()
--- End of stack trace from previous location ---
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeNextResourceFilter>g__Awaited|24_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Rethrow(ResourceExecutedContextSealed context)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.InvokeFilterPipelineAsync()
--- End of stack trace from previous location ---
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeAsync>g__Awaited|17_0(ResourceInvoker invoker, Task task, IDisposable scope)
at Microsoft.AspNetCore.Routing.EndpointMiddleware.<Invoke>g__AwaitRequestTask|6_0(Endpoint endpoint, Task requestTask, ILogger logger)
at Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context)
at Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware.<Invoke>g__Awaited|6_0(ExceptionHandlerMiddleware middleware, HttpContext context, Task task)

很久没用c#了。我打开方式不对?

泥菩萨123的主页 泥菩萨123 | 初学一级 | 园豆:11
提问于:2021-02-01 18:10
< >
分享
最佳答案
0

错误提示已经说明了情况:数据库连接池不可以再创建新的连接了 at MySql.Data.MySqlClient.MySqlPool.CreateNewPooledConnection()

考虑一下怎么合理的使用 DbConnection对象
或者
根据访问量更改一下 Max Pool Size
https://docs.microsoft.com/zh-cn/dotnet/api/system.data.sqlclient.sqlconnection.connectionstring?view=dotnet-plat-ext-5.0

收获园豆:50
jzblive | 菜鸟二级 |园豆:428 | 2021-02-02 10:27

试过。还是一样出错。

泥菩萨123 | 园豆:11 (初学一级) | 2021-02-02 22:15
其他回答(2)
0

thread.setminthreads() 改一下线程池最大和最小数量.

czd890 | 园豆:14488 (专家六级) | 2021-02-01 18:37
0

你这样写 我理解是 只有一个myConnnect

Tom.汤 | 园豆:3060 (老鸟四级) | 2021-02-02 08:17

mysqlconnection默认是使用连接池的。我把它放到Index里面每次都创建新的一样的结果。

支持(0) 反对(0) 泥菩萨123 | 园豆:11 (初学一级) | 2021-02-02 19:33
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册