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

Stats in json to stdout (new command line option --json) #2269

Merged
merged 8 commits into from
Jan 3, 2023

Conversation

AndersSpringborg
Copy link
Contributor

I extended the cli with a --json argument. This argument prints the result of a run in json, after a run is finished. It blocks other INFO logs, so it's easy to interoperable with other programs. Below is an example with jq

❯ python -m locust -f examples/locustfile.py --host http://localhost:80 --headless -u 10 -t 4s --json
[
    {
        "name": "/",
        "method": "GET",
        "last_request_timestamp": 1670363796.9102132,
        "start_time": 1670363793.665749,
        "num_requests": 8,
        "num_none_requests": 0,
        "num_failures": 0,
        "total_response_time": 61.882097012130544,
        "max_response_time": 18.852641980629414,
        "min_response_time": 3.2629000197630376,
        "total_content_length": 4920,
        "response_times": {
            "19": 1,
            "9": 1,
            "4": 3,
            "10": 1,
            "3": 1,
            "8": 1
        },
        "num_reqs_per_sec": {
            "1670363793": 1,
            "1670363794": 2,
            "1670363795": 1,
            "1670363796": 4
        },
        "num_fail_per_sec": {}
    }
]
❯ python -m locust -f examples/locustfile.py --host http://localhost:80 --headless -u 10 -t 4s --json | jq .[0].response_times
{
  "11": 2,
  "10": 1,
  "4": 4,
  "9": 1
}

Locust: 2.13.2.dev11
Python: 3.10.8

@cyberw
Copy link
Collaborator

cyberw commented Dec 7, 2022

Nice!

I think it makes more sense to send the output directly to stdout using print() instead of using logging. That removes the need to disable logging too.

Can you also add a test case? (either an integration test in test_main.py, or in test_stats.py or whatever it is called)

@AndersSpringborg
Copy link
Contributor Author

AndersSpringborg commented Dec 7, 2022

Yeah I'll use print instead. That seems easier to maintain too.

I think INFO logging should still be disabled, as if you run locust in kubernetes (or other orchestration software, kubernetes combines stderr and stdout in their logging api. If you have a program that parses that log, still fails if locust fails (as it gets the error or warning log)
Screenshot 2022-12-07 at 09 32 58

I'll make some integrations tests too. 🤠

@cyberw
Copy link
Collaborator

cyberw commented Dec 8, 2022

I think the user explicitly specifying --skip-log-setup or --loglevel ERROR when you need to suppress stderr logging is better. Changing the log level as a side effect of setting --json is not what I would expect, as a user (even though it may make sense in certain cases, like yours)

@AndersSpringborg
Copy link
Contributor Author

AndersSpringborg commented Dec 27, 2022

I've made some integration tests, and deleted the part about skipping logging.

Merry Christmas 🎄

@cyberw
Copy link
Collaborator

cyberw commented Jan 3, 2023

Cool stuff, thanks!

@cyberw cyberw merged commit 468a67b into locustio:master Jan 3, 2023
@cyberw cyberw changed the title Stats in json to stdout Stats in json to stdout (new command line option --json) Jan 3, 2023
@meeech
Copy link

meeech commented Feb 27, 2023

@cyberw @AndersSpringborg out of curiosity, why not matching ENV_VAR for this option? Any particular reason? Would a pr to add that be welcome?

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

Successfully merging this pull request may close these issues.

3 participants