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
Add example of calling source code from src/
#301
Comments
src/
src/
Hey, thanks for the suggestion. That's a fair point. I'd initially assumed it would be obvious, but now I realize that it wouldn't be to new Rust programmers. I'll update the documentation next time I'm working on Criterion.rs. In the meantime, though - benchmarks in the benchmarks directory are compiled as though they were a separate crate, so all the usual limitations that implies are in force. The benchmarks can only call functions that are publically visible outside of the crate (so Edit: Right, you also asked about the main.rs file - for application crates that's the root of the crate so it should suffice to just |
I’m likewise of the opinion that crates shouldn’t need to reiterate basics for the language, but there’s no harm other than spending the maintainers time on something other than pure feature development. That being said, multiple people have asked the same thing on Stack Overflow: |
For fellow noobs finding this in a Google search for whom it is not obvious, since "extern crate" and various other things that you find in docs about doing testing don't work, try this:
use crate_name::{StructIWantToUse, OtherThingIWantToUse}; where No doubt obvious to others and if it's the wrong way to do it, sorry, but getting something to run that is bad and should be improved is better than not getting anything to run at all and having every combination of Also, don't worry if your editor puts a squiggle under the |
@fearofcode my project structure is the following:
The sample code for criterion worked in In
But all three fail to compile. Any idea what I'm doing wrong? Edit: Following the stackoverflow link, I tried this which partly worked:
Except that a |
You followed the part of my answer where I said in bold “don’t do this“ and also:
Instead, follow the rest of the answer that shows how to create a library. That’s the correct solution. |
Same trouble here, followed the starting guide but nothing seems to work.
Where The benchmark fails to compile:
In EDIT: Aaaah, now I fixed it, sorry for the hassle. My issue was that I was building a |
Huh, I didn't know that was a requirement. I'll add that to the documentation. |
I just ran into this same issue, caused some frustration, the |
Adding a project with rask's project structure in an examples directory would be helpful, it was the first thing I checked for. |
I should mention, the reason it wasn't working for me was because I'm using Neon, which seems to be incompatible with Criterion. |
I went through the example and didn't get it 😁 The comment and the SO answer from another comment helped though |
Coming here two years later, I was very glad to find this. It's bound to hit anybody who tries to benchmark a Python module written in Rust. May I respectfully request that this be put into the documentation? To sum up:If you develop a Python extension using PyO3 / maturin, and you want to use Criterion to benchmark your code (do, it's great!), add "rlib" to your crate-type in
This will allow Criterion to import the code, while not adding any build overhead (at least, not noticeably) |
I also have
My
Probably it would be just easier to separate the Rust core from the FFI using workspaces and add |
Running into the same issue as you @se7entyse7en. Did you ever resolve it? |
@sondrelg yes, I was able to overcome this by using two different
Then I used This is the project. As you can see here there's actually no
I hope it helps. |
I took a stab at splitting my project into two workspaces, but have concluded that it's not worth it for my use-case. It just adds too much complexity. Since I'm writing a Python package I think I'll just benchmark it with Python and call it a day. Running criterion did work in the rust-only workspace though, so this is definitely an option for others 🙂 |
The example has all code put in
benches/
. In practical use this will want to call functions fromsrc/
. It is non-trivial to call these functions (a simpleuse
wont work).The quickstart example should show:
crate::
,project_name::
, etc.)pub
orpub(crate)
etc).main.rs
.That is, start with
src/main.rs
like so:and explain what needs to be done to Criterion-bench that
fibonacci
function (without moving it).I always struggle to get this working when I want to use Criterion in a new project. It would help me if the quickstart example covers this.
The text was updated successfully, but these errors were encountered: