压测工具k6

k6是什么?

js脚本写用例的压测工具,简单、快速、报告详细

 

使用方法

1. 安装(windows下)

choco install k6

2. 写测试脚本

import {check} from 'k6';
import http from 'k6/http';

export const options = {
    vus:10,
    duration:'1m',
    insecureSkipTLSVerify:true
};

export default ()=>{
    const url = 'http://localhost:7257/weather';
    const payload = JSON.stringify({fullname:"nick"});
    const params = {
        headers:{
            'Content-Type':'application/json'
        }
    }
    
    const res = http.get(url);
    
    check(res,{
        'is status 400':(r)=>r.status === 400,
    });
}

3.脚本所在目录 打开命令行,并执行

k6 run .\stresstest.js

4.报告结果

          /\      |‾‾| /‾‾/   /‾‾/
     /\  /  \     |  |/  /   /  /
    /  \/    \    |     (   /   ‾‾\
   /          \   |  |\  \ |  (‾)  |
  / __________ \  |__| \__\ \_____/ .io

  execution: local
     script: .\stresstest.js
     output: -

  scenarios: (100.00%) 1 scenario, 10 max VUs, 1m30s max duration (incl. graceful stop):
           * default: 10 looping VUs for 1m0s (gracefulStop: 30s)


running (1m00.0s), 00/10 VUs, 25657 complete and 0 interrupted iterations
default ✓ [======================================] 10 VUs  1m0s

     ✗ is status 400
      ↳  0% — ✓ 0 / ✗ 25657

     checks.........................: 0.00%  ✓ 0          ✗ 25657
     data_received..................: 4.3 MB 72 kB/s
     data_sent......................: 2.2 MB 37 kB/s
     http_req_blocked...............: avg=4.46µs  min=0s med=0s      max=7.84ms   p(90)=0s       p(95)=0s
     http_req_connecting............: avg=273ns   min=0s med=0s      max=1ms      p(90)=0s       p(95)=0s
     http_req_duration..............: avg=22.99ms min=0s med=21.14ms max=99.26ms  p(90)=34.6ms   p(95)=35.91ms
       { expected_response:true }...: avg=22.99ms min=0s med=21.14ms max=99.26ms  p(90)=34.6ms   p(95)=35.91ms
     http_req_failed................: 0.00%  ✓ 0          ✗ 25657
     http_req_receiving.............: avg=74.07µs min=0s med=0s      max=5.34ms   p(90)=249.16µs p(95)=608.73µs
     http_req_sending...............: avg=11.74µs min=0s med=0s      max=3.24ms   p(90)=0s       p(95)=0s
     http_req_tls_handshaking.......: avg=0s      min=0s med=0s      max=0s       p(90)=0s       p(95)=0s
     http_req_waiting...............: avg=22.9ms  min=0s med=21.03ms max=99.26ms  p(90)=34.5ms   p(95)=35.83ms
     http_reqs......................: 25657  427.448837/s
     iteration_duration.............: avg=23.38ms min=0s med=21.63ms max=111.07ms p(90)=34.98ms  p(95)=36.32ms
     iterations.....................: 25657  427.448837/s
     vus............................: 10     min=10       max=10
     vus_max........................: 10     min=10       max=10

 

 5. 附被测代码

[HttpGet("weather")]
   [BetterServiceFilter<LoggingFilter>(Order = 1)] 
    public IEnumerable<int> Index()
    {
        using (var _ = _logger.TimedOperation("begin {-1}", nameof(Index)))
        {
            return Enumerable.Range(1, 5);
        }
    }

 

posted @ 2022-11-12 10:16  那只狐狸  阅读(504)  评论(0编辑  收藏  举报