溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

如何在.net core中使用ConcurrentTest組件實現(xiàn)一個壓力測試功能

發(fā)布時間:2021-01-21 15:54:49 來源:億速云 閱讀:133 作者:Leah 欄目:開發(fā)技術(shù)

今天就跟大家聊聊有關(guān)如何在.net core中使用ConcurrentTest組件實現(xiàn)一個壓力測試功能,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。

引用組件

Install-Package BeetleX.ConcurrentTest -Version 0.2.8

WebAPI服務(wù)

[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服務(wù),主要是提供了雇員獲取和添加功能。

測試用例

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對測試用例進行一個多線程并發(fā)測試;只需要簡單運行以下代碼即可

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

以上代碼是對FastHttpClientTest的所有測試方法進行一個測試,測試數(shù)據(jù)是使用10個線程,進行500000萬次調(diào)用測試。

測試報表

在運行過程中組件會實時顯示并發(fā)情況和區(qū)間響應(yīng)數(shù)量,最終會針對每個測試用例形成一個簡要的測試結(jié)果;具體結(jié)果如下:

***********************************************************************
* 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:[ ]
***********************************************************************

看完上述內(nèi)容,你們對如何在.net core中使用ConcurrentTest組件實現(xiàn)一個壓力測試功能有進一步的了解嗎?如果還想了解更多知識或者相關(guān)內(nèi)容,請關(guān)注億速云行業(yè)資訊頻道,感謝大家的支持。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI