2

我有一个 K6 测试,其中对流程进行负载测试,并且我正在对每个页面请求进行分组。我想要做的是检查每个组的持续时间,比如


  group('Home page', function(){
         http.get('localhost:8080')
   });
   //something like this 
   check(group.duration, function()=>{'performs check logic'})

   group('search page', function(){
    http.get('localhost:8080/Search?term=mysearch')
   })
   check(group.duration, function()=>{'performs check logic'})

文档说明 group 发出 group_duration 但没有说明如何监听它。JS不强,所以如果这很简单,我很抱歉。

4

1 回答 1

2

组目前不返回其持续时间。

在这种情况下发射意味着如果您使用 json output/influxdb/stastsd/load Impact 的云进行指标存储,而不是发送该组持续时间的指标,您可以稍后......从指标存储中做任何您想做的事情.

的语法checkcheck(variable_name_to_use, map_of_name_to_function_to_calls)很像

check(group.duraiton, {
  "checking duration is less than 20 as normal function": function(d){ return d<20}, 
  "checking duration is less than 20 as arrow function": (d)=>{ return d<20}, 
  "checking duration is less than 20 as arrow function without the brackets": (d)=>d<20, 
})

使用时,您还需要提供 url 的方案http.*

我还想指出,这个想法group是衡量给定组(!)的请求或操作需要多少。例如,它们可用于对包含“登录”、“将商品添加到购物车”、“结帐和付款”等所有操作进行分组。它们绝对有用,但是当您的所有组都是单个请求时将单个请求分组不会添加任何新数据:)

解决方案1: 您可以自己这样做:

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

export default function() {
        var group_duration;
    group('Home page', function(){
                var start = new Date();
        http.get('http://localhost:8080');
                group_duration = new Date() - start;
    });
    check(group_duration,  {
                "Name of group check": function(d){return d<200}
        })

    group('search page', function(){
                var start = new Date();
        http.get('http://localhost:8080/Search?term=mysearch');
                group_duration = new Date() - start;
    })
        console.log(group_duration);
    check(group_duration,  {
                "Name of group check": (d)=>d<200
        })
}

您当然可以将其移至一个函数,并将持续时间返回为:

function mygroup(name, f) {
        var start = new Date();
        group(name, f);
        return new Date() - start;
}

而不是打电话mygroup并获得持续时间

var group_duration = mygroup('Home page', function(){
    http.get('http://localhost:8080');
});

解决方案 2: 在您在每个组中仅使用一个请求的情况下,您可以使用请求确实返回他们花费了多少的事实。所以

resp = http.get("google.com");
check (resp.timings.duration, {
    "check response time", (d) => d<200
});

将检查给定请求的时间是否少于 200 毫秒。您可以在文档中查看响应中的更多内容

作为一名 k6 开发人员,我认为您提出的建议不是一个坏主意,如果您真的打开一个问题,我想

于 2019-05-23T05:58:39.787 回答