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

How to benchmark a function from my crate #353

Closed
lucidBrot opened this issue Nov 12, 2019 · 3 comments
Closed

How to benchmark a function from my crate #353

lucidBrot opened this issue Nov 12, 2019 · 3 comments

Comments

@lucidBrot
Copy link

lucidBrot commented Nov 12, 2019

I got the fibonacci example to work with the function within my_benchmark.rs. But I cannot figure out how I can benchmark a function that I defined in my own crate.

My folder structure is like this:

mycratename
| - benches
    | - my_benchmark.rs
| - src
    | - main.rs
| - target
| - Cargo.toml
| - Cargo.lock

How could I benchmark a function that is defined in main.rs?
Or is that already documented somewhere? In that case, I think a more obvious link is needed

@bheisler
Copy link
Owner

bheisler commented Nov 13, 2019

Hey, thanks for trying Criterion.rs!

Unfortunately, it's not currently possible to use Criterion.rs with binary crates, only with library crates (see this issue for more details). If you have a library crate, you can take a look at the Getting Started page of the user guide, which shows an example of benchmarking a function from the lib.rs file.

Edit: I almost forgot; this is documented in the Known Limitations section of the user guide.

Edit2: It's fairly common for binary crates in Rust to contain only a thin layer of glue code (parsing command-line arguments, etc.) around a library crate that implements the application logic. I don't actually remember just off-hand why that's done, but it does handily solve this problem.

@lucidBrot
Copy link
Author

Thanks for that reply! I'll have to read up on the difference between creating a lib vs binary crate (I'm kinda new to rust), but it does sound like I should be able do do what your Edit2 suggests

@lucidBrot
Copy link
Author

Okay. Turns out I would have needed to use a library instead of a binary crate anyways, because I want to use my crate in a different crate and it is not possible to use binary crates in other crates. So thank you for the suggestion!

I don't see the point of binary crates then... because libraries can have executables with cargo run --example.

For anybody else with the same problem though: All it takes is renaming main.rs to lib.rs

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

No branches or pull requests

2 participants