Looking for scheme of serialization `check`

hi there, i would have some question abou K6, i wonder if you can help me .
I was trying to create some data chart,
here is the code :

import {check as k6Check } from "k6";
import { Counter } from 'k6/metrics';

let errors_metrics = new Counter("my_errors");
let success_metrics = new Counter("my_success");
export function check(res, conditionArray, tags) {
    let flag = k6Check(res, conditionArray, tags || {});
    let tag = {
        response: JSON.stringify(res),
        check: JSON.stringify(conditionArray)
    };
    if (flag) {
        success_metrics.add(1, tag);
    }else {
        errors_metrics.add(1, tag);
    }
    return flag;
}

What am i trying to do is collect conditionArray by using proxy check,
but i found that conditionArray can’t be serialized by JSON.stringify
So,i was wondering if there any way to gain the specific details about failing of the proxy check in K6 ?
or do you have better way to make it so ?

Hi @runstp,

I guess conditionArray is the map of string to function that check takes? In that case - yes you can’t serialize functions ;).

I guess you are trying to see which check of the map failed and which … didn’t?

If you just want them outside of k6 they get emitted as metrics, example:

import { check } from "k6";

export default function() {
    var conditionsArray = {
        "wrong": () => "wrong" === "right",
        "right": (r) => r == 2,
    };
    check(2, conditionsArray);
    check(3, conditionsArray);
}

would emit (with -o json) the following metrics(among many others):

{"type":"Point","data":{"time":"2020-07-29T11:47:01.468138364+03:00","value":0,"tags":{"check":"wrong","group":""}},"metric":"checks"}
{"type":"Point","data":{"time":"2020-07-29T11:47:01.468138364+03:00","value":1,"tags":{"check":"right","group":""}},"metric":"checks"}
{"type":"Point","data":{"time":"2020-07-29T11:47:01.468173215+03:00","value":0,"tags":{"check":"wrong","group":""}},"metric":"checks"}
{"type":"Point","data":{"time":"2020-07-29T11:47:01.468173215+03:00","value":0,"tags":{"check":"right","group":""}},"metric":"checks"}

as you cans see only one of the right checks passed and has value 1 instead of 0.

If you want to get this information inside k6 … I think this is currently … not … possible. But you can implement your own check and just call check to set the correct values with something like

var resultFromCheck = yourCheck(something)
k6Check(null, {something[label]: ()=> resultFromCheck})
2 Likes

Much appreciate ,
Indeed, i was tring to find out which check have fail in the map.
thanks for your thought, really helpful to me ,
I’m gonna implement my own check right now, seen if could solve the problem.