Skip to content

Commit befb523

Browse files
ci: download bench input from gcs
1 parent e94b7ad commit befb523

File tree

7 files changed

+99
-9
lines changed

7 files changed

+99
-9
lines changed

Cargo.lock

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

crates/bench_tools/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ apollo_infra_utils.workspace = true
1616
glob.workspace = true
1717
rstest.workspace = true
1818
serde_json.workspace = true
19+
tempfile.workspace = true
1920

2021
[[bench]]
2122
harness = false

crates/bench_tools/src/gcs.rs

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ pub const BENCHMARKS_BUCKET: &str = "apollo_benchmarks";
77
/// Uploads all files from a local directory to Google Cloud Storage.
88
///
99
/// Uses gcloud CLI to upload files. Before running, authenticate with:
10-
/// `gcloud auth application-default login`
10+
/// `gcloud auth login`
1111
///
1212
/// Files are uploaded to: `gs://{BENCHMARKS_BUCKET}/{benchmark_name}/input/`
1313
pub fn upload_inputs(benchmark_name: &str, input_dir: &Path) {
@@ -35,3 +35,35 @@ pub fn upload_inputs(benchmark_name: &str, input_dir: &Path) {
3535
println!("{}", String::from_utf8_lossy(&output.stdout).trim());
3636
println!("Input files uploaded successfully!");
3737
}
38+
39+
/// Downloads all input files for a benchmark from Google Cloud Storage.
40+
///
41+
/// Uses gcloud CLI to download files. Before running, authenticate with:
42+
/// `gcloud auth login`
43+
///
44+
/// Downloads from: `gs://{BENCHMARKS_BUCKET}/{benchmark_name}/input/` to the local input directory.
45+
pub fn download_inputs(benchmark_name: &str, local_input_dir: &Path) {
46+
println!(
47+
"Downloading inputs from gs://{}/{}/input/ to {}",
48+
BENCHMARKS_BUCKET,
49+
benchmark_name,
50+
local_input_dir.display()
51+
);
52+
53+
let source = format!("gs://{}/{}/input/*", BENCHMARKS_BUCKET, benchmark_name);
54+
let dest = local_input_dir.display().to_string();
55+
56+
// Use gcloud storage cp command to download files.
57+
let output = Command::new("gcloud")
58+
.args(["storage", "cp", "-r", &source, &dest])
59+
.output()
60+
.expect("Failed to cp inputs from GCS");
61+
62+
if !output.status.success() {
63+
let stderr = String::from_utf8_lossy(&output.stderr);
64+
panic!("Failed to download inputs from GCS: {}", stderr);
65+
}
66+
67+
println!("{}", String::from_utf8_lossy(&output.stdout).trim());
68+
println!("Input files downloaded successfully!");
69+
}

crates/bench_tools/src/gcs_test.rs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
use std::fs;
2+
3+
use tempfile::TempDir;
4+
5+
use crate::gcs::{download_inputs, upload_inputs};
6+
use crate::test_utils;
7+
8+
#[test]
9+
#[ignore] // Run with: cargo test -p bench_tools -- --ignored
10+
fn test_upload_and_download_inputs() {
11+
let benchmark_name = "dummy_benchmark";
12+
13+
// Get paths relative to bench_tools crate directory.
14+
let source_dir = test_utils::bench_tools_crate_dir().join("data/dummy_bench_input");
15+
16+
// Ensure source files exist.
17+
assert!(source_dir.exists(), "Source directory does not exist: {}", source_dir.display());
18+
19+
// Upload inputs.
20+
println!("Testing upload...");
21+
upload_inputs(benchmark_name, &source_dir);
22+
23+
// Create temp directory for download.
24+
let temp_dir = TempDir::new().unwrap();
25+
let download_dir = temp_dir.path();
26+
27+
println!("\nDownload directory: {}", download_dir.display());
28+
29+
// Download inputs to temp directory.
30+
println!("\nTesting download...");
31+
download_inputs(benchmark_name, download_dir);
32+
33+
// Verify files were downloaded.
34+
let small_input = download_dir.join("small_input.json");
35+
let large_input = download_dir.join("large_input.json");
36+
37+
assert!(small_input.exists(), "small_input.json was not downloaded");
38+
assert!(large_input.exists(), "large_input.json was not downloaded");
39+
40+
// Verify content matches original.
41+
let original_small = fs::read_to_string(source_dir.join("small_input.json")).unwrap();
42+
let downloaded_small = fs::read_to_string(&small_input).unwrap();
43+
assert_eq!(original_small, downloaded_small, "small_input.json content does not match");
44+
45+
let original_large = fs::read_to_string(source_dir.join("large_input.json")).unwrap();
46+
let downloaded_large = fs::read_to_string(&large_input).unwrap();
47+
assert_eq!(original_large, downloaded_large, "large_input.json content does not match");
48+
}

crates/bench_tools/src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
#[cfg(test)]
22
pub(crate) mod benches;
33
pub mod gcs;
4+
#[cfg(test)]
5+
pub mod gcs_test;
6+
#[cfg(test)]
7+
pub mod test_utils;
48
pub mod types;
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
use std::path::PathBuf;
2+
3+
use rstest::fixture;
4+
5+
/// Returns the bench_tools crate directory.
6+
#[fixture]
7+
pub fn bench_tools_crate_dir() -> PathBuf {
8+
std::env::var("CARGO_MANIFEST_DIR")
9+
.map(PathBuf::from)
10+
.unwrap_or_else(|_| std::env::current_dir().unwrap())
11+
}

crates/bench_tools/src/types/estimates_test.rs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,9 @@ use std::process::Command;
55
use apollo_infra_utils::path::project_path;
66
use rstest::{fixture, rstest};
77

8+
use crate::test_utils::bench_tools_crate_dir;
89
use crate::types::estimates::Estimates;
910

10-
/// Returns the bench_tools crate directory.
11-
#[fixture]
12-
fn bench_tools_crate_dir() -> PathBuf {
13-
std::env::var("CARGO_MANIFEST_DIR")
14-
.map(PathBuf::from)
15-
.unwrap_or_else(|_| std::env::current_dir().unwrap())
16-
}
17-
1811
/// Returns the directory where dummy benchmark estimate results are stored.
1912
#[fixture]
2013
fn dummy_bench_results_dir(bench_tools_crate_dir: PathBuf) -> PathBuf {

0 commit comments

Comments
 (0)