aboutsummaryrefslogtreecommitdiffhomepage
path: root/crates/shirabe/benches/packagist_fixture.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/shirabe/benches/packagist_fixture.rs')
-rw-r--r--crates/shirabe/benches/packagist_fixture.rs26
1 files changed, 26 insertions, 0 deletions
diff --git a/crates/shirabe/benches/packagist_fixture.rs b/crates/shirabe/benches/packagist_fixture.rs
new file mode 100644
index 00000000..86184879
--- /dev/null
+++ b/crates/shirabe/benches/packagist_fixture.rs
@@ -0,0 +1,26 @@
+//! Real packagist p2 metadata for the benchmarks, fetched on first run and cached under the
+//! target directory so later runs work offline.
+
+use std::path::PathBuf;
+
+pub fn fetch(package: &str) -> String {
+ let dir = PathBuf::from(env!("CARGO_TARGET_TMPDIR")).join("packagist-p2");
+ std::fs::create_dir_all(&dir).expect("failed to create the fixture directory");
+ let path = dir.join(format!("{}.json", package.replace('/', "-")));
+ if let Ok(fixture) = std::fs::read_to_string(&path) {
+ return fixture;
+ }
+
+ let url = format!("https://repo.packagist.org/p2/{package}.json");
+ let fixture = reqwest::blocking::get(&url)
+ .and_then(|response| response.error_for_status())
+ .and_then(|response| response.text())
+ .unwrap_or_else(|e| {
+ panic!(
+ "failed to fetch {url}: {e}; download it to {} by hand to run this benchmark offline",
+ path.display(),
+ )
+ });
+ std::fs::write(&path, &fixture).expect("failed to cache the fixture");
+ fixture
+}