新的空asp.net core 项目,加入下面的代码,试过连接池,效果不明显。
只输出hello world的话php 6000左右 asp.net core 10000左右,加了数据访问,php 1000左右,asp.net core就600左右了,我想asp.net core绝不会这样。
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace webapp
{
public class Startup
{
string constructorString = "server=localhost;uid=root;pwd=123456;Database=test;";
// This method gets called by the runtime. Use this method to add services to the container.
// For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
public void ConfigureServices(IServiceCollection services)
{
services.AddRouting(configureOptions => {
configureOptions.LowercaseUrls = true;
});
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapGet("/", async context =>
{
var list = new List<Dictionary<string, string>>();
MySqlConnection myConnnect;
MySqlCommand sqlCmd;
myConnnect = new MySqlConnection(constructorString);
sqlCmd = new MySqlCommand();
sqlCmd.Connection = myConnnect;
sqlCmd.CommandText = "select * from location where parent_id=0";
await myConnnect.OpenAsync();
using (var reader = await sqlCmd.ExecuteReaderAsync(System.Data.CommandBehavior.CloseConnection))
{
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 context.Response.WriteAsync("Hello World!");
});
});
}
}
}
while (await reader.ReadAsync())
这里可以根据业务做下优化,现在的代码,每次只返回一条数据。N条数据,至少经过N次TCP传输数据。
可以一次返回多条数据(分页),减少数据传输的耗时。
吧sql的connection 池化. 马上能上来几个量级.
试过。没效果。而且connection默认就是使用连接池的。
@泥菩萨123:
@czd890: 试过了。没效果。难受。
@泥菩萨123: 那就vs里面看看性能监控, 看看什么地方耗时.
@czd890: 就是数据库访问那段
建议先去掉一些代码 测试QPS
再加上那段代码 如果QPS上来后 就说明那一段有性能问题
然后针对那一段去采取别的办法实现
这就难受了。就那么一点点代码。你看我去掉哪行合适?
@泥菩萨123: mysql连接那块
@ycyzharry: ......兄弟。就是这段慢。去掉了当然快了。而且我要优化的就是这段啊。