1

我为我的 http 和 websocket 请求创建了不同的模块。下面是一个示例结构。

import http from "k6/http";
	import {loginReq} from "./lib/login.js";
  import {onOpen, respMsg, close, searchItem} from "./lib/handler.js"

	export default function() {
  		let jsess = loginReq();
      
      let params = {
      "JSESSION":jsess
      };
      
      let reqMsg ={
      open: onOpen,
      message: respMsg,
      close: close,
      fn:{
      searchItem: searchItem
      }
      };
      
      let response = ws.connect(url, params,  reqMsg);
      
      check(response, { "status is 101": (r) => r && r.status === 101 });
	}

loginReq() 函数是一个 http 请求,而我的 searchItem 是一个 websocket 请求。

我只是想知道每当我执行命令时

k6 运行 --vus 10 --duration 10s

我的 loginReq 函数(它是一个 http)同时执行 10 次(正如预期的 10 个 vus),然后它开始一个接一个地执行我的 searchItem(websocket)。

示例日志记录:

.....
created user 11
searching for item...
search successful!
created user 12
searching for item...
search successful!
.
.
.
created user 20
searching for item...
search successful!

我的所有函数都在导出默认函数中调用,但是,VUS 似乎只同时执行“http”请求,而我的 websocket 请求没有。有没有人有同样的经历?关于如何解决执行行为的任何建议?

非常感谢您的帮助!

4

1 回答 1

1

我尝试使用以下脚本重现该错误,但它按预期工作。

赶紧跑k6 run --vus 5 -i 5 script.js

import ws from "k6/ws";
import http from 'k6/http';
import { check, sleep } from "k6";

export default function () {
    http.get("http://test.loadimpact.com");
    console.log(`exec http.get at ${new Date(Date.now()).toLocaleString()}`)

    sleep(1)

    var url = "ws://echo.websocket.org";
    var response = ws.connect(url, {}, function (socket) {
        socket.on('open', function open() {
            console.log(`VU ${__VU} ==> connected at ${new Date(Date.now()).toLocaleString()}`);
            socket.ping();
        });

        socket.on('ping', function () {
            console.log("PING!");
        });

        socket.on('pong', function () {
            console.log("PONG!");
        });

        socket.on('close', function close() {
            console.log('disconnected');
        });

        socket.on('error', function (e) {
            if (e.error() != "websocket: close sent") {
                console.log('An unexpected error occured: ', e.error());
            }
        });

        socket.setTimeout(function () {
            console.log('1 seconds passed, closing the socket');
            socket.close();
        }, 1000);
    });

    check(response, { "status is 101": (r) => r && r.status === 101 });
};

您会看到它们都同时连接到 WebSocket。

k6 run --vus 5 -i 5 script.js

          /\      |‾‾|  /‾‾/  /‾/
     /\  /  \     |  |_/  /  / /
    /  \/    \    |      |  /  ‾‾\
   /          \   |  |‾\  \ | (_) |
  / __________ \  |__|  \__\ \___/ .io

  execution: local
     output: -
     script: script.js

    duration: -,  iterations: 5
         vus: 5, max: 5

INFO[0001] exec http.get at 05/15/2018, 12:22:11
INFO[0001] exec http.get at 05/15/2018, 12:22:11
INFO[0001] exec http.get at 05/15/2018, 12:22:11
INFO[0001] exec http.get at 05/15/2018, 12:22:11
INFO[0001] exec http.get at 05/15/2018, 12:22:11
INFO[0002] VU 2 ==> connected at 05/15/2018, 12:22:12
INFO[0002] VU 1 ==> connected at 05/15/2018, 12:22:12
INFO[0002] VU 4 ==> connected at 05/15/2018, 12:22:12
INFO[0002] VU 5 ==> connected at 05/15/2018, 12:22:12
INFO[0002] VU 3 ==> connected at 05/15/2018, 12:22:12
INFO[0002] PONG!
INFO[0002] PONG!
INFO[0002] PONG!
INFO[0002] PONG!
INFO[0002] PONG!
INFO[0003] 1 seconds passed, closing the socket
INFO[0003] disconnected
INFO[0003] 1 seconds passed, closing the socket
INFO[0003] disconnected
INFO[0003] 1 seconds passed, closing the socket
INFO[0003] disconnected
INFO[0003] 1 seconds passed, closing the socket
INFO[0003] disconnected
INFO[0003] 1 seconds passed, closing the socket
INFO[0003] disconnected
于 2018-05-15T16:48:09.580 回答