WebAPI服務
[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服務,主要是提供了雇員獲取和添加功能。
測試用例
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對測試用例進行一個多線程并發測試;只需要簡單運行以下代碼即可
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
總結
聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com