Skip to content

Commit f080e80

Browse files
Merge pull request #73 from theseus-rs/add-magic-benchmark
chore: add magic crate benchmark
2 parents 43e70a6 + cde37b2 commit f080e80

File tree

6 files changed

+82
-2
lines changed

6 files changed

+82
-2
lines changed

.github/workflows/benchmarks.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ permissions:
1313
jobs:
1414
benchmark:
1515
name: Run Benchmarks
16-
runs-on: macos-14
16+
runs-on: ubuntu-latest
1717
permissions:
1818
pull-requests: write
1919
steps:
@@ -29,6 +29,9 @@ jobs:
2929
- name: Install benchmarking tools
3030
uses: bencherdev/bencher@main
3131

32+
- name: Install crate dependencies
33+
run: sudo apt-get install libmagic1 libmagic-dev
34+
3235
- name: Run benchmarks
3336
if: ${{ github.event_name == 'pull_request' }}
3437
env:

Cargo.lock

Lines changed: 43 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ csv = "1.3.1"
3636
file-format = "0.26.0"
3737
infer = "0.19.0"
3838
indoc = "2.0.5"
39+
magic = "0.16.2"
3940
mime_guess = "2.0.5"
4041
quick-xml = "0.37.2"
4142
rayon = "1.5.1"

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,9 +110,10 @@ Data is licensed under the following licenses:
110110

111111
If this crate does not meet your requirements, you may want to consider the following alternatives:
112112

113+
* [file-format](https://crates.io/crates/file-format)
113114
* [infer](https://crates.io/crates/infer)
115+
* [magic](https://crates.io/crates/magic)
114116
* [mime_guess](https://crates.io/crates/mime_guess)
115-
* [file-format](https://crates.io/crates/file-format)
116117

117118
## Contribution
118119

file_type/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ anyhow = { workspace = true }
2323
criterion = { workspace = true }
2424
file-format = { workspace = true }
2525
infer = { workspace = true }
26+
magic = { workspace = true }
2627
mime_guess = { workspace = true }
2728
tokio = { workspace = true, features = ["full"] }
2829

file_type/benches/file_type.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,37 @@ fn bench_lifecycle(criterion: &mut Criterion) -> anyhow::Result<()> {
102102
});
103103
});
104104

105+
//
106+
// Comparison testing to the magic crate
107+
//
108+
#[cfg(target_os = "linux")]
109+
{
110+
let cookie = magic::Cookie::open(Default::default())?;
111+
let cookie = cookie
112+
.load(&Default::default())
113+
.expect("failed to load magic database");
114+
115+
criterion.bench_function("magic::from_bytes", |bencher| {
116+
bencher.iter(|| {
117+
// human-readable description, more than a static name
118+
cookie.set_flags(magic::cookie::Flags::ERROR).unwrap();
119+
let _ = cookie.buffer(&bytes).unwrap();
120+
121+
// file type extensions
122+
cookie
123+
.set_flags(magic::cookie::Flags::ERROR | magic::cookie::Flags::EXTENSION)
124+
.unwrap();
125+
let _ = cookie.buffer(&bytes).unwrap();
126+
127+
// media type
128+
cookie
129+
.set_flags(magic::cookie::Flags::ERROR | magic::cookie::Flags::MIME_TYPE)
130+
.unwrap();
131+
let _ = cookie.buffer(&bytes).unwrap();
132+
});
133+
});
134+
}
135+
105136
//
106137
// Comparison testing to the mime_guess crate
107138
//

0 commit comments

Comments
 (0)