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

Refactoring printer code into functions and adding unit tests #303

Merged
merged 7 commits into from
Sep 18, 2023

Conversation

wjhoward
Copy link
Contributor

@wjhoward wjhoward commented Sep 10, 2023

@hatoo would it be helpful if I work through adding some unit tests? printer - percentiles and print_distribution tests included as examples of the approach I would use. Let me know what you think?

If so I can work through adding more tests as part of this PR.

@wjhoward wjhoward changed the title Add unit tests (printer - percentiles example) Add unit tests (printer - percentiles and print_distribution examples) Sep 10, 2023
Copy link
Owner

@hatoo hatoo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

Adding unit tests is good.

Regarding testing the printer, the original implementation was bad because test_percentiles and print_distribution are doing the same data process.

I added percentile_iter and please test it to test the numbers.
And if you want to test outputs, just test the format itself e.g. testing indent (no testing numbers).

src/printer.rs Outdated
use super::*;

#[tokio::test]
async fn test_percentiles() {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just #[test] and no async fn would be enough

src/printer.rs Outdated
#[tokio::test]
async fn test_print_distribution() {
let style = StyleScheme {
color_enabled: !false,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

true?

src/printer.rs Outdated
let values: [f64; 2] = [10.0, 100.0];
let mut output: Vec<u8> = Vec::new();
let res = print_distribution(&mut output, &values, style);
assert!(res.is_ok());
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I prefer unwrap at above

@wjhoward
Copy link
Contributor Author

wjhoward commented Sep 11, 2023

And if you want to test outputs, just test the format itself e.g. testing indent (no testing numbers).

@hatoo to clarify are you suggesting checking the full output rather than checking if the output contains substrings? The reason I was testing print_distribution for a specific set of substrings was so that if we changed the color scheme the test wouldn't immediately fail, but can simply check the entire string including colors and indentation etc if preferred? The checking of numbers was merely to check they were placed in the right format / position rather than aimed at testing the actual values generated.

My other thought is that it could be worth moving some of the calculations into functions to allow unit testing, e.g:

oha/src/printer.rs

Lines 208 to 213 in 21aa87d

slowest: res
.iter()
.filter_map(|r| r.as_ref().ok())
.map(|r| r.duration().as_secs_f64())
.collect::<average::Max>()
.max(),

to something like:


slowest: slowest(res)

fn slowest(res: &[Result<RequestResult, E>]){
    res
    .iter()
    .filter_map(|r| r.as_ref().ok())
    .map(|r| r.duration().as_secs_f64())
    .collect::<average::Max>()
    .max()
}

@hatoo
Copy link
Owner

hatoo commented Sep 12, 2023

@hatoo to clarify are you suggesting checking the full output rather than checking if the output contains substrings?

No, what I mean is you can test such like the "align"
e.g. checking outputs are not like

10% in 10.0000 secs
25% in 10.0000 secs
50%            in 100.0000 secs
75%        in 100.0000 secs
90%     in 100.0000 secs
95% in 100.0000 secs
99% in 100.0000 secs

by only using pure text processing.

However, since you think

The checking of numbers was merely to check they were placed in the right format / position rather than aimed at testing the actual values generated.

I feel that's OK 👍

My other thought is that it could be worth moving some of the calculations into functions to allow unit testing, e.g:

It looks good!

@wjhoward
Copy link
Contributor Author

This is still a work in progress, but I thought that it makes sense to push the work done so far for more immediate feedback. I still need to write more tests and possibly refactor more code into functions.

@wjhoward wjhoward requested a review from hatoo September 13, 2023 21:51
@wjhoward wjhoward changed the title Add unit tests (printer - percentiles and print_distribution examples) Refactoring code into functions and adding unit tests Sep 15, 2023
@wjhoward wjhoward changed the title Refactoring code into functions and adding unit tests Refactoring printer code into functions and adding unit tests Sep 15, 2023
@wjhoward
Copy link
Contributor Author

@hatoo I've now refactored a large chunk of printer code into functions and added corresponding unit tests, this also has the benefit of re-using the same code for the printer and JSON output. (The only missing component is the size_per_request calculation which I've not yet added a function for given it differs for the printer and JSON output).

I'm now thinking maybe it makes sense to remove the output testing (so far only the test_print_distribution function) and just stick to calculation tests) maybe easier to just test the output as part of the integration tests?

Let me know what you think?

@hatoo
Copy link
Owner

hatoo commented Sep 16, 2023

For size_per_request, I think printer's calculation is better. JSON's one should be replaced.

I'm now thinking maybe it makes sense to remove the output testing (so far only the test_print_distribution function) and just stick to calculation tests) maybe easier to just test the output as part of the integration tests?

Agree. In this PR, please only add calculation tests and add output another in PR if you think it's needed.

Copy link
Owner

@hatoo hatoo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good

@wjhoward
Copy link
Contributor Author

For size_per_request, I think printer's calculation is better. JSON's one should be replaced.

I'm now thinking maybe it makes sense to remove the output testing (so far only the test_print_distribution function) and just stick to calculation tests) maybe easier to just test the output as part of the integration tests?

Agree. In this PR, please only add calculation tests and add output another in PR if you think it's needed.

I've made these changes, overall I think this is probably enough changes for one PR, and covers the core printer calculations. What are your thoughts @hatoo ?

@hatoo hatoo merged commit c5fcd62 into hatoo:master Sep 18, 2023
37 of 38 checks passed
@hatoo
Copy link
Owner

hatoo commented Sep 18, 2023

LGTM, Thanks! 🚀

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.

2 participants