服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|

服务器之家 - 编程语言 - ASP.NET教程 - .net core如何利用ConcurrentTest组件对方法进行压力测试详解

.net core如何利用ConcurrentTest组件对方法进行压力测试详解

2020-06-04 14:27smark ASP.NET教程

这篇文章主要给大家介绍了关于.net core如何利用ConcurrentTest组件对方法进行压力测试的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面来一起看看吧

前言

工欲善其事,必先利其器!在编写服务中首先要有一个好的测试工具,在dontecore下性能测试有BenchmarkDotNet,只需要简单的配置一下就可以对方法的性能进行详细的测试。但有时候需要对不同并发下看其处理效率和延时统计查看,如HTTP服务对应着大量的测试工具如ab,bombardier等等。由于找不到类似于测试HTTP服务的工具来测试代码用例,于时就有了ConcurrentTest这个组件的实现.通过ConcurrentTest组件可以运行不同的测试用例,并可以实时查看具体的并发情况和延时分布数据。

以下介绍一下如何使用ConcurrentTest运行测试用例并统计运行结果,话不多说了,来一起看看详细的介绍吧

引用组件

Install-Package BeetleX.ConcurrentTest -Version 0.2.8

WebAPI服务

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[Route("api/[controller]")]
  [ApiController]
  public class EmployeeController : ControllerBase
  {
    [HttpGet("{count}")]
    public JsonResult Get(int count)
    {
      return new JsonResult(Employee.GetEmployees(count));
    }
    [HttpPost]
    public JsonResult Post([FromBody]Employee value)
    {
      return new JsonResult(value);
    }
  }

以上是一个简单的dotnet core WebApi服务,主要是提供了雇员获取和添加功能。

测试用例

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
public class FastHttpClientTest
  {
    public FastHttpClientTest()
    {
      httpApiClient = new HttpApiClient(Host);
      clientApi = httpApiClient.CreateWebapi<IHttpClientApi>();
    }
    private string Host = "http://localhost:8007";
    private BeetleX.FastHttpApi.HttpApiClient httpApiClient;
    private IHttpClientApi clientApi;
    [CTestCase]
    public void AddEmployee()
    {
      clientApi.AddEmployee(Employee.GetEmployee());
    }
    [CTestCase]
    public void ListEmployees()
    {
      clientApi.ListEmployees(2);
    }
    [JsonFormater]
    public interface IHttpClientApi
    {
      [Get(Route = "api/employee/{count}")]
      List<Employee> ListEmployees(int count);
      [Post(Route = "api/employee")]
      Employee AddEmployee(Employee item);
    }
  }

组件使用起来和BenchmarkDotNet差不多,通过CTestCase来标记,具体测试方法通过接口定义。使用接口来描述WebApi请求是FastHttpApi,在这里就不过多说明。

使用ConcurrentTest进行压力测试

当测试用例编写完成后,就可以使用ConcurrentTest对测试用例进行一个多线程并发测试;只需要简单运行以下代码即可

?
1
CTester.RunTest<FastHttpClientTest>(10, 500000);

以上代码是对FastHttpClientTest的所有测试方法进行一个测试,测试数据是使用10个线程,进行500000万次调用测试。

测试报表

在运行过程中组件会实时显示并发情况和区间响应数量,最终会针对每个测试用例形成一个简要的测试结果;具体结果如下:

***********************************************************************
* https://github.com/IKende/ConcurrentTest.git
* Copyright ? ikende.com 2018 email:henryfan@msn.com
* ServerGC:True
***********************************************************************
* AddEmployee test prepping completed
-----------------------------------------------------------------------
* [500000/500000]|threads:[10]
* Success:[ 0/s]|total:[ 500000][min:23448/s max:24561/s]
* Error:[ 0/s]|total:[ 0][min:0/s max:0/s]
-----------------------------------------------------------------------
* 0ms-0.1ms:[ ] 0.1ms-0.5ms:[ 435,604]
* 0.5ms-1ms:[ 59,863] 1ms-5ms:[ 4,356]
* 5ms-10ms:[ 142] 10ms-50ms:[ 35]
* 50ms-100ms:[ ] 100ms-1000ms:[ ]
* 1000ms-5000ms:[ ] 5000ms-10000ms:[ ]
***********************************************************************

***********************************************************************
* ListEmployees test prepping completed
-----------------------------------------------------------------------
* [500000/500000]|threads:[10]
* Success:[ 0/s]|total:[ 500000][min:28105/s max:28829/s]
* Error:[ 0/s]|total:[ 0][min:0/s max:0/s]
-----------------------------------------------------------------------
* 0ms-0.1ms:[ ] 0.1ms-0.5ms:[ 476,342]
* 0.5ms-1ms:[ 20,641] 1ms-5ms:[ 2,922]
* 5ms-10ms:[ 80] 10ms-50ms:[ 15]
* 50ms-100ms:[ ] 100ms-1000ms:[ ]
* 1000ms-5000ms:[ ] 5000ms-10000ms:[ ]
***********************************************************************

组件还具备什么功能

现有的ConcurrentTest的功能还相对简陋,不过应用者还是可以根据实际的需要来制定统计标签,延时区间等相关统计;由于组件的代码也非常少只有几个类,你也根据根据自己的需要来扩展它或在https://github.com/IKende/ConcurrentTest提上相应issues

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对服务器之家的支持。

原文链接:https://www.cnblogs.com/smark/p/9913339.html

延伸 · 阅读

精彩推荐