Skip to content

main_binary takes ~300ms per invocation #6

Closed
@epage

Description

@epage
Contributor

From @rcoh assert-rs/assert_cli#100

  • assert_cli version: 0.5
  • Rust version: 1.25.0
  • OS and version: Ubuntu 16.04
    Unfortunately, cargo run takes at minimum about 300ms on my computer:
➜  angle-grinder git:(master) ✗ time cargo run --bin agrind -- '* | json | count' --file test_files/empty
    Finished dev [unoptimized + debuginfo] target(s) in 0.0 secs
     Running `target/debug/agrind '* | json | count' --file test_files/empty`
No data
cargo run --bin agrind -- '* | json | count' --file test_files/empty  0.22s user 0.06s system 92% cpu 0.307 total

It makes it impractical to run hundreds of integration tests with assert_cli.

I'm not positive what a better way would be -- I guess running the binary in dist directly if it exists?

Workaround

See assert_cmd::cargo's docs

Activity

epage

epage commented on May 28, 2018

@epage
ContributorAuthor

From killercup: cargo issue: rust-lang/cargo#5315

epage

epage commented on May 29, 2018

@epage
ContributorAuthor

So a user can work around this in assert_cmd by accessing cargo_bin_path, etc. This causes the 300ms to only be hit once instead of on every run.

lazy_static! {
    const BIN_UNDER_TEST = assert-cmd::cargo_bin_path("bin_fixture");
}

#[test]
fn test_bin() {
    Command::new(BIN_UNDER_TEST).assert().success();
}

Should we provide an API on to simplify this?

Example

lazy_static!{
   const BIN_FIXTURE = assert_cmd::cargo_bin("bin_fixture");
}
...
println!("{:?}", BIN_FIXTURE.path());
BIN_FIXTURE.cmd().arg("stdout", 42).unwrap();
changed the title [-]Takes ~300ms to run 1 invocation[/-] [+]`main_binary` takes ~300ms to run 1 invocation[/+] on May 29, 2018
zetok

zetok commented on Jul 24, 2018

@zetok

Additions to API aren't needed, but docs that would point out this functionality would be really helpful. Since right now one needs to go to the issue tracker of the crate to find the solution, which is suboptimal.

As for why an additional API is not needed – e.g. regex crate doesn't have an additional API for creating a static regexp, but it has a great documentation where it shows on the "main" page of its docs an example on how to use lazy_static to reduce unnecessary computations. It's nice.

Also, a working example, freshly ~copypasted from a test for anyone else who's interested:

use std::path::PathBuf;

lazy_static! {
    static ref BIN_PATH: PathBuf = assert_cmd::cargo::main_binary_path().unwrap();
}

#[test]
fn test_bin() {
    Command::new(&*BIN_PATH).assert().success();
}
epage

epage commented on Jul 25, 2018

@epage
ContributorAuthor

As for why an additional API is not needed – e.g. regex crate doesn't have an additional API for creating a static regexp

The difference is the main purpose of this crate is running programs. Its a bit of a wart in the API to have functions that provide paths to programs you could run. It'd be nice if Command supported clone.

epage

epage commented on Oct 6, 2018

@epage
ContributorAuthor

I think at this point this is mostly kept open to track and be aware of the slow down. We have workarounds but thats all they are.

killercup

killercup commented on Oct 7, 2018

@killercup
Contributor

20 remaining items

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugNot as expected

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      Participants

      @killercup@epage@sourcefrog@rcoh@zetok

      Issue actions

        `main_binary` takes ~300ms per invocation · Issue #6 · assert-rs/assert_cmd