Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

json output generates a non valid json file #2988

Closed
caperutxa opened this issue Mar 27, 2023 · 1 comment
Closed

json output generates a non valid json file #2988

caperutxa opened this issue Mar 27, 2023 · 1 comment

Comments

@caperutxa
Copy link

Brief summary

image: "grafana/k6"
command: "k6 run --out json=test.json /app/smoke.js"
version: "k6 v0.43.1 (2023-02-27T10:52:44+0000/v0.43.1-0-gaf3a0b89, go1.19.6, linux/amd64)"

Using the sample script in "https://k6.io/docs/test-types/smoke-testing/" the output is a non valid json file

{
    "type": "Metric",
    "data": {
        "name": "http_reqs",
        "type": "counter",
        "contains": "default",
        "thresholds": [],
        "submetrics": null
    },
    "metric": "http_reqs"
}
{
    "metric": "http_reqs",
    "type": "Point",
    "data": {
        "time": "2023-03-27T14:40:31.975467751Z",
        "value": 1,
        "tags": {
            "error_code": "1400",
            "expected_response": "false",
            "group": "",
            "method": "POST",
            "name": "https://fake.io/api/auth",
            "proto": "HTTP/2.0",
            "scenario": "default",
            "status": "400",
            "tls_version": "tls1.3",
            "url": "https://fake.io/api/auth"
        }
    }
}
{
    "type": "Metric",
    "data": {
        "name": "http_req_duration",
        "type": "trend",
        "contains": "time",
        "thresholds": [
            "p(99)<1500"
        ],
        "submetrics": [
            {
                "name": "http_req_duration{expected_response:true}",
                "suffix": "expected_response:true",
                "tags": {
                    "expected_response": "true"
                }
            }
        ]
    },
    "metric": "http_req_duration"
}
{
    "metric": "http_req_duration",
    "type": "Point",
    "data": {
        "time": "2023-03-27T14:40:31.975467751Z",
        "value": 100.866339,
        "tags": {
            "error_code": "1400",
            "expected_response": "false",
            "group": "",
            "method": "POST",
            "name": "https://fake.io/api/auth",
            "proto": "HTTP/2.0",
            "scenario": "default",
            "status": "400",
            "tls_version": "tls1.3",
            "url": "https://fake.io/api/auth"
        }
    }
}

k6 version

k6 v0.43.1 (2023-02-27T10:52:44+0000/v0.43.1-0-gaf3a0b89, go1.19.6, linux/amd64)

OS

linux

Docker version and image (if applicable)

grafana/k6

Steps to reproduce the problem

Run the sample script with "--out json=some-file.json"

Expected behaviour

Should be, at least, a valid json.

Nice to have is to join nodes under metric types or similar to allow parsing it

Actual behaviour

The json file is not valid

@caperutxa caperutxa added the bug label Mar 27, 2023
@na--
Copy link
Member

na-- commented Mar 27, 2023

This is not a bug, we are intentionally putting one JSON object per line. If we want to be technically correct, the output should have been called jsonl, since this type of file is called "JSON Lines", see https://jsonlines.org/

We do it like this because, for big k6 tests, this file could grow to be several gigabytes of data. When every object is on a separate line, it's much easier to process that data in a streaming fashion, without loading the whole file in memory.

For example, the easiest way to do that is probably a tool like jq, though it can also be done in any programming language you may want to use. True, people could have used a streaming JSON parser even if it was all one big JSON array, but you can also do that now, or read the file line by line and use a regular JSON parser. We have some example of how you can use jq to process the file in our docs, see https://k6.io/docs/results-output/real-time/json/#processing-json-output

And, if you really want an array, you can use jq to transform our format to that too, simply run jq -s '.' results-jsonlines.json > results-array.json (copied from this StackOverflow topic).

Finally, we have another open issue that is about adding an option to the k6 json output to emit a well-formed JSON array, see #737. I'll close this issue, since it's a duplicate, but please 👍 the other issue. We'd probably also accept a quality PR with the functionality, if you or someone else is willing to contribute that!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants