web 压力测试工具 K6

1、K6 官方地址  https://k6.io/

2、windows 下载安装包 k6-latest-amd64.msi 安装

DOS 下执行:k6 run D:\testjs\localhost.js >d:\testjs\localhost.txt

import check from "k6";
import http from "k6/http";

export let options = {
    duration:'1m', //持续运行时间
    vus: 10,       //并发数
    rps: 10        //每秒并发多少
};

/**
export let options = {
  stages: [
    { duration: '5m', target: 60 },  // simulate ramp-up of traffic from 1 to 60 users over 5 minutes.
    { duration: '10m', target: 60 }, // stay at 60 users for 10 minutes
    { duration: '3m', target: 100 }, // ramp-up to 100 users over 3 minutes (peak hour starts)
    { duration: '2m', target: 100 },  // stay at 100 users for short amount of time (peak hour)
    { duration: '3m', target: 60 },  // ramp-down to 60 users over 3 minutes (peak hour ends)
    { duration: '10m', target: 60 },  // continue at 60 for additional 10 minutes
    { duration: '5m', target: 0 },   // ramp-down to 0 users
  ],
  thresholds: {
    http_req_duration: ['p(99)<1500'], // 99% of requests must complete below 1.5s
  },
};
**/

export default function() {
    http.get("http://localhost/web?site=jysj&page=index");
    
    /**
    for (var id = 1; id <= 100; id++) {
      http.get(`http://example.com/posts/${id}`, {
           tags: { name: 'PostsItemURL' },
      });
    }
    **/
};

 

postdemo.js

import http from 'k6/http';

export let options = {
    duration:'1m', //持续运行时间
    vus: 10,       //并发数
    rps: 10        //每秒并发多少
};

export default function () {
  var url = 'http://test.k6.io/login';
  var payload = JSON.stringify({
    email: 'aaa',
    password: 'bbb',
  });

  var params = {
    headers: {
      'Content-Type': 'application/json',
    },
  };

  http.post(url, payload, params);
}

 

batchdemo.js

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

export let options = {
    duration:'1m', //持续运行时间
    vus: 10,       //并发数
    rps: 10        //每秒并发多少
};

export default function () {
  let responses = http.batch([
    ['GET', 'https://test.k6.io', null, { tags: { ctype: 'html' } }],
    ['GET', 'https://test.k6.io/style.css', null, { tags: { ctype: 'css' } }],
    [
      'GET',
      'https://test.k6.io/images/logo.png',
      null,
      { tags: { ctype: 'images' } },
    ],
  ]);
  check(responses[0], {
    'main page status was 200': (res) => res.status === 200,
  });
}

 

batchdemo2.js

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

export let options = {
    duration:'1m', //持续运行时间
    vus: 10,       //并发数
    rps: 10        //每秒并发多少
};

export default function () {
  let req1 = {
    method: 'GET',
    url: 'https://httpbin.org/get',
  };
  let req2 = {
    method: 'GET',
    url: 'https://test.k6.io',
  };
  let req3 = {
    method: 'POST',
    url: 'https://httpbin.org/post',
    body: {
      hello: 'world!',
    },
    params: {
      headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
    },
  };
  let responses = http.batch([req1, req2, req3]);
  // httpbin.org should return our POST data in the response body, so
  // we check the third response object to see that the POST worked.
  check(responses[2], {
    'form data OK': (res) => JSON.parse(res.body)['form']['hello'] == 'world!',
  });
}

 

HTTP-specific built-in metrics

Metric NameTYpeDescription
http_reqs Counter k6产生的总的请求数
http_req_blocked Trend 等待空闲的TCP连接槽的等待时间
http_req_connecting Trend 建立到远程主机的TCP连接所花费的时间
http_req_tls_handshaking Trend 与远程主机握手TLS会话花费的时间
http_req_sending Trend 发送数据到远程机的时间
http_req_waiting Trend 等待远程主机所花费的时间
http_req_receiving Trend 从远程主机接收响应数据所花费的时间
http_req_duration Trend 请求的总时间= http_req_sending + http_req_waiting + http_req_receiving
posted @ 2021-07-12 09:21  李小加  阅读(1056)  评论(0编辑  收藏  举报