CodeBenchmark
是一款高性能可视化的并发测试组件,经过组件能够对任意逻辑代码或服务进行并发测试;组件最终经过可视化的方式来显示测试结果,在测试结果中能够看到具体的并发状况和处理延时的分布。组件不只能够对单个逻辑或服务进行并发测试,还能够同时对多个逻辑代码用例进行不一样并发分组压测,最终显示它们之间的性能差别和不一样并发下的最优结果。接下来介绍如何使用这一组件对逻辑代码或服务进行并发测试。vue
能够经过vs
或vscode
构建一个控制台项目而后引用组件(引用最新版本的BeetleX.CodeBenchmark
)git
Install-Package BeetleX.CodeBenchmark -Version 1.0.8
引用组件后就能够编写具体的测试用例,测试用例编写必须符合组件测试要求,因此须要实现一个接口来编写测试代码,接口描述以下:github
public interface IExample:IDisposable { void Initialize(Benchmark benchmark); Task Execute(); }
[System.ComponentModel.Category("TCP")] class HttpGet : IExample { public void Dispose() { } public async Task Execute() { var result = await _httpHandler.json(); } public void Initialize(Benchmark benchmark) { if(_httpApi==null) { _httpApi = new BeetleX.Http.Clients.HttpClusterApi(); _httpApi.DefaultNode.Add("http://192.168.2.19:8080"); _httpHandler = _httpApi.Create<IHttpHandler>(); } } static BeetleX.Http.Clients.HttpClusterApi _httpApi; static IHttpHandler _httpHandler; [BeetleX.Http.Clients.FormUrlFormater] public interface IHttpHandler { // http://host/json Task<string> json(); } }
在并发实例初始化的时候建立一个请求http
请求对象,HttpClusterApi
是一个线程安全对象因此只须要静态化构建一个便可;组件会针对第一个并发来构建一个实例。json
当测试用例写好后就须要进行测试,经过如下简单代码便可以打开测试管理windows
Benchmark benchmark = new Benchmark(); benchmark.Register(typeof(Program).Assembly); benchmark.Start();//在本地打开管理服务,服务端口默信是9090 if(Environment.OSVersion.Platform== PlatformID.Win32NT) benchmark.OpenWeb();
代码中作了一下判断,若是当前系统是windows
则自动打开浏览器访问服务(因为服务是基于vue实现,会存在一些旧浏览器兼容问题,建议使用新版浏览器)。浏览器
当服务打开后就能够经过浏览器管理测试用例,具体界面以下:安全
在管理界面中你只须要选择测试用例和添加并发测试便可以进行压测,如下是开启10个并发并运行10秒的测试状况:并发
运行后能看到并发完成的数量和平均的RPS
,点击测试的用例还能看到延时分布,能够知道大部分处理分布在那个时间区域。async
不少时候须要对一个服务进行不一样并发的测试,这样能够观察服务在那个并发量下的最并发处理能力;组件支持对同时添加多个测试并发组并进行测试和结果对比。工具
在这个硬件和测试场景之下,显然是8并发的测力测试结果最高
通这个测试就能够针对服务状况来规划并发控制设计。接下来换个环境试下一样的测试
经过工具能够很快就能测出不一样环境下最优的并发处理效果
组件也支持多用例压测对比,只须要在测试的时候选择多个测试用例便可。接下来简单地测试一下不一样json
组件序列化的性能对比。 测试分别有Newtonsoft.Json
,Utf8Json
,SwifterJson
和spanJson
.相应代码以下:
Newtonsoft.Json
[System.ComponentModel.Category("Serializer-Stream")] class Newtonsoft_Stream : CodeBenchmark.IExample { public void Dispose() { } public Task Execute() { memoryStream.Position = 0; var users = User.List(10); jsonSerializer.Serialize(jsonTextWriter, users); jsonTextWriter.Flush(); return Task.CompletedTask; } private System.IO.MemoryStream memoryStream; private System.IO.StreamWriter streamWriter; private Newtonsoft.Json.JsonSerializer jsonSerializer; private Newtonsoft.Json.JsonTextWriter jsonTextWriter; public void Initialize(Benchmark benchmark) { memoryStream = new System.IO.MemoryStream(); jsonSerializer = new Newtonsoft.Json.JsonSerializer(); streamWriter = new StreamWriter(memoryStream); jsonTextWriter = new Newtonsoft.Json.JsonTextWriter(streamWriter); } }
Utf8Json
[System.ComponentModel.Category("Serializer-Stream")] class Utf8Json_Stream : CodeBenchmark.IExample { public void Dispose() { } public async Task Execute() { MemoryStream.Position = 0; var users = User.List(10); await Utf8Json.JsonSerializer.SerializeAsync<List<User>>(MemoryStream, users); TextWriter.Flush(); } private System.IO.MemoryStream MemoryStream = new System.IO.MemoryStream(); private System.IO.TextWriter TextWriter; public void Initialize(Benchmark benchmark) { TextWriter = new System.IO.StreamWriter(MemoryStream, Encoding.UTF8); } }
SwifterJson
[System.ComponentModel.Category("Serializer-Stream")] class Swifter_Stream : CodeBenchmark.IExample { public void Dispose() { } public async Task Execute() { MemoryStream.Position = 0; var users = User.List(10); await Swifter.Json.JsonFormatter.SerializeObjectAsync<List<User>>(users, TextWriter); TextWriter.Flush(); } private System.IO.MemoryStream MemoryStream = new System.IO.MemoryStream(); private System.IO.TextWriter TextWriter; public void Initialize(Benchmark benchmark) { TextWriter = new System.IO.StreamWriter(MemoryStream, Encoding.UTF8); } }
SpanJson
[System.ComponentModel.Category("Serializer-Stream")] class Span_Stream : CodeBenchmark.IExample { public void Dispose() { } public async Task Execute() { var users = User.List(10); MemoryStream.Position = 0; await SpanJson.JsonSerializer.NonGeneric.Utf8.SerializeAsync(users, MemoryStream); } private System.IO.MemoryStream MemoryStream = new System.IO.MemoryStream(); public void Initialize(Benchmark benchmark) { } }
接下来对这几个逻辑进行2,4,8,16和32的并发压测(注意:在这里提醒一下,若是是压测内存运算数据的状况并发数最好不要超过CPU的逻辑核数,不然会致使组件计数Timer可能没机会触发或触发延时)
到这里CodeBenchmark
的使用就讲解完成,若是有兴趣能够关注https://github.com/IKende/CodeBenchmarkDoc