Skip to content

Commit

Permalink
feat: add benches
Browse files Browse the repository at this point in the history
  • Loading branch information
cocool97 committed Nov 17, 2024
1 parent 6a2d652 commit 441c1ac
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 3 deletions.
15 changes: 12 additions & 3 deletions adb_client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ repository.workspace = true
version.workspace = true

[dependencies]
base64 = "0.22.1"
bincode = "1.3.3"
base64 = { version = "0.22.1" }
bincode = { version = "1.3.3" }
byteorder = { version = "1.5.0" }
chrono = { version = "0.4.38" }
homedir = { version = "0.3.4" }
Expand All @@ -24,5 +24,14 @@ regex = { version = "1.11.0", features = ["perf", "std", "unicode"] }
rsa = { version = "0.3.0" }
rusb = { version = "0.9.4", features = ["vendored"] }
serde = { version = "1.0.210", features = ["derive"] }
serde_repr = "0.1.19"
serde_repr = { version = "0.1.19" }
thiserror = { version = "2.0.1" }

[dev-dependencies]
anyhow = { version = "1.0.93" }
criterion = { version = "0.5.1" } # Used for benchmarks

[[bench]]
harness = false
name = "benchmark_adb_push"
path = "../benches/benchmark_adb_push.rs"
94 changes: 94 additions & 0 deletions benches/benchmark_adb_push.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
use adb_client::ADBServer;
use anyhow::Result;
use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion};
use rand::{thread_rng, Rng};
use std::fs::File;
use std::io::Write;
use std::process::Command;
use std::time::Duration;

const LOCAL_TEST_FILE_PATH: &str = "test_file.bin";
const REMOTE_TEST_FILE_PATH: &str = "/data/local/tmp/test_file.bin";


/// Generate random test file with given size
fn generate_test_file(size_in_bytes: usize) -> Result<()> {
let mut test_file = File::create(LOCAL_TEST_FILE_PATH)?;

let mut rng = thread_rng();

const BUFFER_SIZE: usize = 64 * 1024;
let mut buffer = [0u8; BUFFER_SIZE];
let mut remaining_bytes = size_in_bytes;

while remaining_bytes > 0 {
let bytes_to_write = remaining_bytes.min(BUFFER_SIZE);
rng.fill(&mut buffer[..bytes_to_write]);
test_file.write_all(&buffer[..bytes_to_write])?;
remaining_bytes -= bytes_to_write;
}

Ok(())
}

/// Use `adb_client` crate to push a file on device
fn bench_adb_client_push() -> Result<()> {
let mut client = ADBServer::default();
let mut device = client.get_device()?;
let f = File::open(LOCAL_TEST_FILE_PATH)?;
Ok(device.push(f, REMOTE_TEST_FILE_PATH)?)
}

/// Use standard `adb` command ti push a file on device
fn bench_adb_push_command() -> Result<()> {
let output = Command::new("adb")
.arg("push")
.arg(LOCAL_TEST_FILE_PATH)
.arg(REMOTE_TEST_FILE_PATH)
.output()?;

if !output.status.success() {
eprintln!("error while starting adb push command");
}
Ok(())
}

/// Main benchmark function
fn benchmark_adb_push(c: &mut Criterion) {
for (file_size, sample_size) in [
// (10 * 1024 * 1024, 100), // 10MB -> 100 iterations
// (500 * 1024 * 1024, 50), // 500MB -> 50 iterations
(1000 * 1024 * 1024, 20), // 1GB -> 20 iterations
] {
eprintln!(
"Benchmarking file_size={} and sample_size={}",
file_size, sample_size
);

generate_test_file(file_size).expect("Cannot generate test file");

let mut group = c.benchmark_group("ADB Push Benchmark");
group.sample_size(sample_size);

group.bench_function(BenchmarkId::new("adb_client", "push"), |b| {
b.iter(|| {
bench_adb_client_push().expect("Error while benchmarking adb_client push");
});
});

group.bench_function(BenchmarkId::new("adb", "push"), |b| {
b.iter(|| {
bench_adb_push_command().expect("Error while benchmarking adb push command");
});
});

group.finish();
}
}

criterion_group!(
name = benches;
config = Criterion::default().measurement_time(Duration::from_secs(1000));
targets = benchmark_adb_push
);
criterion_main!(benches);

0 comments on commit 441c1ac

Please sign in to comment.