闽公网安备 35020302035485号
|
某API指标 |
指标名称 |
指标值 |
||
|
业务指标 |
TPS(2C2G) |
2000 |
||
|
响应时间 |
ART |
10 |
||
|
P95 |
12 |
|||
|
P99 |
15 |
|||
|
成功率 |
100% |
|||
|
并发线程 |
20 |
|||
|
稳定性指标 |
压力持续时间 |
>=8h |
||
|
压力阈值 |
CPU <80%,TPS≈2000 |
|||
|
内存泄露 |
无 |
|||
|
TPS波动 |
<5% |
|||
|
应用资源(2C2G) |
MEM |
<2G |
||
|
CPU最大使用率 |
<90% |
|||
|
DB资源(8C8G) |
MEM |
<8G |
||
|
CPU最大使用率 |
<90% |
|||
|
缓存(1C1G) |
MEM |
<80% |
||
|
CPU最大使用率 |
<90% |
|||

//同步方法
app.MapGet("/sync", () =>
{
using (var con = new MySqlConnection(connectionString))
{
var result = con.Query<int>("select sleep(6)");
Console.WriteLine($"sync:{DateTime.Now}");
return result;
}
});
//异步方法,没有CancellationToken
app.MapGet("/async", async () =>
{
using (var con = new MySqlConnection(connectionString))
{
var result = await con.QueryAsync<int>("select sleep(6)");
Console.WriteLine($"sync:{DateTime.Now}");
return result;
}
});
//异步,有CancellationToken
app.MapGet("/asyncwithtoken", async (CancellationToken token) =>
{
using (var con = new MySqlConnection(connectionString))
{
var result = await con.QueryAsync<int>(new CommandDefinition("select sleep(6)", cancellationToken: token));
Console.WriteLine($"sync:{DateTime.Now}");
return result;
}
});
通过postman请求三个api,然后取消,会发现前两个api始终会执行完成,并输出时间;第三个会在取消后停止响应。
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Jobs;
using BenchmarkDotNet.Running;
using PerformanceDemo;
using System.Collections.Generic;
using System.Xml.Linq;
var summary = BenchmarkRunner.Run(typeof(CustomTypeTest));
public class CustomTypeTest
{
[Benchmark]
[Arguments(2000)]
public Dictionary<string, SBook> GetBook1(int count)
{
var sbookDic = new Dictionary<string, SBook>();
for (var i = 0; i <= count; i++)
{
sbookDic.Add($"张{i}丰", new SBook()
{
Id = i,
Name = "C#从入门到精通",
Author = $"张{i}丰",
Title = "C#1.0",
Description = "这是一本书"
});
}
return sbookDic;
}
[Benchmark]
[Arguments(2000)]
public Dictionary<string, Book> GetBook2(int count)
{
var bookDic = new Dictionary<string, Book>();
for (var i = 0; i <= count; i++)
{
bookDic.Add($"张{i}丰", new Book()
{
Id = i,
Name = "C#从入门到精通",
Author = $"张{i}丰",
Title = "C#1.0",
Description = "这是一本书"
});
}
return bookDic;
}
[Benchmark]
[Arguments(2000)]
public List<Book> GetBook3(int count)
{
var bookList = new List<Book>();
for (var i = 0; i <= count; i++)
{
bookList.Add(new Book
{
Id = i,
Name = "C#从入门到精通",
Author = $"张{i}丰",
Title = "C#1.0",
Description = "这是一本书"
});
}
return bookList;
}
[Benchmark]
[Arguments(2000)]
public List<SBook> GetBook4(int count)
{
var sbookList = new List<SBook>();
for (var i = 0; i <= count; i++)
{
sbookList.Add(new SBook
{
Id = i,
Name = "C#从入门到精通",
Author = $"张{i}丰",
Title = "C#1.0",
Description = "这是一本书"
});
}
return sbookList;
}
}
public class Book
{
public int Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public string Title { get; set; }
public string Author { get; set; }
}
public struct SBook
{
public int Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public string Title { get; set; }
public string Author { get; set; }
}
下面是比较结果:
|
|
普通模式 |
R2R模式 |
AOT模式 |
|
大小 |
29.8 MB |
62.2 MB |
19.5M |
|
首次请求用时 |
360ms |
90ms |
20ms |
https://learn.microsoft.com/zh-cn/dotnet/core/diagnostics/