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); } }
以上是一個(gè)簡(jiǎn)單的dotnet core WebApi服務(wù),主要是提供了雇員獲取和添加功能。
測(cè)試用例
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來標(biāo)記,具體測(cè)試方法通過接口定義。使用接口來描述WebApi請(qǐng)求是FastHttpApi,在這里就不過多說明。
使用ConcurrentTest進(jìn)行壓力測(cè)試
當(dāng)測(cè)試用例編寫完成后,就可以使用ConcurrentTest對(duì)測(cè)試用例進(jìn)行一個(gè)多線程并發(fā)測(cè)試;只需要簡(jiǎn)單運(yùn)行以下代碼即可
CTester.RunTest<FastHttpClientTest>(10, 500000);
以上代碼是對(duì)FastHttpClientTest的所有測(cè)試方法進(jìn)行一個(gè)測(cè)試,測(cè)試數(shù)據(jù)是使用10個(gè)線程,進(jìn)行500000萬次調(diào)用測(cè)試。
測(cè)試報(bào)表
在運(yùn)行過程中組件會(huì)實(shí)時(shí)顯示并發(fā)情況和區(qū)間響應(yīng)數(shù)量,最終會(huì)針對(duì)每個(gè)測(cè)試用例形成一個(gè)簡(jiǎn)要的測(cè)試結(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:[ ]
***********************************************************************
組件還具備什么功能
現(xiàn)有的ConcurrentTest的功能還相對(duì)簡(jiǎn)陋,不過應(yīng)用者還是可以根據(jù)實(shí)際的需要來制定統(tǒng)計(jì)標(biāo)簽,延時(shí)區(qū)間等相關(guān)統(tǒng)計(jì);由于組件的代碼也非常少只有幾個(gè)類,你也根據(jù)根據(jù)自己的需要來擴(kuò)展它或在https://github.com/IKende/ConcurrentTest提上相應(yīng)issues
總結(jié)
聲明:本網(wǎng)頁內(nèi)容旨在傳播知識(shí),若有侵權(quán)等問題請(qǐng)及時(shí)與本網(wǎng)聯(lián)系,我們將在第一時(shí)間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com