bheisler / criterion.rs

Statistics-driven benchmarking library for Rust
Apache License 2.0
4.28k stars 290 forks source link

Can't call a function for the benchmark from the crate inside a Rust workspace #764

Closed Benji377 closed 4 months ago

Benji377 commented 4 months ago

I am in a bit of an awkward situation. I am creating an application using the Tauri framework, and it requires me to have two Cargo.toml file. And because of this strange file structure, I seemingly can't call any function for the benchmarks.

File structure:

C:.
|   .dockerignore
|   .eslintrc.json
|   .gitignore
|   .tokeignore
|   Cargo.lock
|   Cargo.toml
|   CHANGELOG.md
|   codecov.yml
|   CODE_OF_CONDUCT.md
|   Dockerfile
|   LICENSE
|   Makefile
|   next-i18next.config.js
|   next.config.js
|   package-lock.json
|   package.json
|   postcss.config.js
|   README.md
|   renovate.json
|   snapcraft.yaml
|   tailwind.config.js
|        
+---components
|       DirectoryInput.js
|       DirectoryPickerButton.js
|       DriverDropdown.js
|       FlagIcon.js
|       IgnoredHashes.js
|       InformationCard.js
|       LanguagePicker.js
|       LanguageSwitchLink.js
|       SettingsCard.js
|       VirusCard.js
|                
+---lib
|       getStatic.js
|       languageDetector.js
|       redirect.js
|       
+---pages
|   |   clean.js
|   |   index.js
|   |   infected.js
|   |   info.js
|   |   loading.js
|   |   permission.js
|   |   settings.js
|   |   _app.js
|   |   _document.js
|   |   
|   \---[locale]
|           clean.js
|           index.js
|           infected.js
|           info.js
|           loading.js
|           permission.js
|           settings.js
|               
+---services
|       getConfigSettings.js
|       useLocalStorage.js
|       
+---src-tauri
|   |   .gitignore
|   |   build.rs
|   |   Cargo.toml
|   |   tauri.conf.json
|   |   
|   +---icons
|   |       icon.png
|   |       
|   \---src
|       |   main.rs
|       |   
|       +---backend
|       |   |   config_file.rs
|       |   |   db_ops.rs
|       |   |   downloader.rs
|       |   |   file_log.rs
|       |   |   mod.rs
|       |   |   scanner.rs
|       |   |   
|       |   \---utils
|       |           generic.rs
|       |           mod.rs
|       |           scanner_utils.rs
|       |           update_utils.rs
|       |           usb_utils.rs
|       |           
|       +---benches
|       |       testbench.rs
|       |       
|       \---tests
|               config_file_test.rs
|               db_ops_test.rs
|               file_log_test.rs
|               file_scanner_test.rs
|               mod.rs
|                

Cargo.toml:

[workspace]
resolver = "2"
members = ["src-tauri"]

[profile.release]
incremental = false
codegen-units = 1
panic = "abort"
opt-level = "s"
lto = true

src-tauri/Cargo.toml:

[package]
name = "raspirus"
...
default-run = "raspirus"
edition = "2021"
rust-version = "1.59"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

# generic dependencies
[dependencies]
...

[dev-dependencies]
...
criterion = { version = "0.5.1", features = ["html_reports"] }

[build-dependencies]
...

[features]
...

[[bench]]
name = "testbench"
path = "src/benches/testbench.rs"
harness = false

Example bench file:

use criterion::{criterion_group, criterion_main, Criterion};

use log::{info, error};
use crate::backend::utils::scanner_utils;

static PATH: &str = "Testpath here";

fn bench_scan(c: &mut Criterion) {
    c.bench_function("bench_scan", |b| {
        b.iter(|| {
            match scanner_utils::start_scanner(None, PATH.to_string()) {
                Ok(res) => {
                    info!("Result: {res}");
                }
                Err(err) => {
                    error!("Error: {err}");
                }
            }
        })
    });
}

criterion_group!(
    benches,
    bench_scan,
);
criterion_main!(benches);

Error: failed to resolve: could not findbackendin the crate root

Benji377 commented 4 months ago

Never mind, just read your limitations. Mine is a binary crate.