Skip to content

Conversation

@K4YN5
Copy link

@K4YN5 K4YN5 commented Apr 15, 2025

Description: This PR introduces a new command-line option -Z to oha that allows reading request bodies line-by-line from a specified file.

Motivation: Needed to simulate different bodies to test my cache layer on my project.

Implementation: Adds a new command-line argument -Z (using clap). Also, made -D, -d and -Z incompatible from clap. When this argument is present, oha reads the specified file line by line. For each request made, the next line from the file is used as the request body. When the file ends, wraps around. Updated the README and command-line help text to include the new option.

Example Usage:
Assuming bodies.txt contains lines like {"url": "http://example.com/1"}, {"url": "http://example.com/2"}, etc.:
oha -n 10000 -c 100 -m POST -H "Content-Type: application/json" -Z bodies.txt http://target-endpoint/

Testing Done
Verified the functionality manually with a local server.
Ran the full test suite using cargo test.

Important Note on Test Failures:
During testing on my local machine, the tests test_google and test_proxy failed. These tests also fail consistently when running on the unmodified master branch in my environment, suggesting an environment problem not regarding my modifications.

All other tests in the suite pass successfully with these changes applied (verified using cargo test -- --skip test_google --skip test_proxy).

@reneleonhardt
Copy link
Contributor

Great feature, thank you!

I'm wondering, if every line is JSON, wouldn't that make the input file .jsonl? 🤔
https://jsonlines.org/

@riccardoforzan
Copy link

I'm also intrested in this feature. Would it be possible to implement a streaming reader? I was imagining using it with a JSONL file on the order of tens of GB, so reading the entire file at once would be very resource intensive

@reneleonhardt
Copy link
Contributor

Streaming (read 1 line into memory, not all lines) would be relatively easy, but you would have to rewrite this PR to not read everything upfront but on demand 😅

    let file = File::open("bodies.txt")?;
    let mut reader = BufReader::new(file);
    for line_result in reader.lines() {
        let line = line_result?;  // Propagate I/O errors if they happen
    }

@riccardoforzan
Copy link

riccardoforzan commented Sep 6, 2025

Yes, sure. I'm still new to Rust but I'd like to give it a shot. Any suggestions are very welcome 😄

@reneleonhardt
Copy link
Contributor

If you want to try, you could checkout this PR and modify it to read line-by-line.

But it could be easier to checkout master and copy only the lines you need from this PR because it hasn't been merged since April so it's 68 commits behind 😄

@riccardoforzan
Copy link

#787 any feedback would be greatly appreciated, if everything looks good I will mark it as ready for review

@hatoo
Copy link
Owner

hatoo commented Sep 6, 2025

I want to put all varying url/body/headers to RequestGenerator

#785

@riccardoforzan
Copy link

I want to put all varying url/body/headers to RequestGenerator

#785

Ok, I've aligned my PR

@hatoo
Copy link
Owner

hatoo commented Sep 13, 2025

Sorry for delayed response.
I was afraid to add variable body or etc feature in this fashion because it makes codes messy.
But I'm came up with RequestGenerator, we can add variable body/header/etc features just local to RequestGenerator.

I added the feature in this PR in #789. it slightly different because it picks random line not sequential though.

Thank you for your PR!

@hatoo hatoo closed this Sep 13, 2025
hatoo added a commit that referenced this pull request Sep 13, 2025
 feat: Add option to read request body line-by-line from file. #729
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.

4 participants