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 Name TYpe Description
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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM