From 55000324bb4d6434fa147ce4c835a2c8cc76b757 Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Sun, 5 Dec 2021 15:24:58 +0200 Subject: feat: add placeholders for what needs to happen Sponsored-by: author --- Cargo.lock | 1 + Cargo.toml | 1 + src/bin/obnam-benchmark.rs | 34 ++++++++++++++++++++++ src/lib.rs | 72 +++++++++++++++++++++++++++++++++++++++++----- 4 files changed, 101 insertions(+), 7 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index c6da964..e71a6fb 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -587,6 +587,7 @@ dependencies = [ "structopt", "subplot-build", "subplotlib", + "thiserror", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index 9f1e1ef..2979f4d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,6 +12,7 @@ serde = { version = "1.0.101", features = ["derive"] } serde_json = "1.0.72" serde_yaml = "0.8.21" structopt = "0.3.25" +thiserror = "1.0.30" [build-dependencies] anyhow = "1.0.51" diff --git a/src/bin/obnam-benchmark.rs b/src/bin/obnam-benchmark.rs index bc9c7de..8b31a82 100644 --- a/src/bin/obnam-benchmark.rs +++ b/src/bin/obnam-benchmark.rs @@ -15,6 +15,7 @@ fn real_main() -> anyhow::Result<()> { let opt = Opt::from_args(); match opt.cmd { + Command::Run(x) => x.run()?, Command::Spec(x) => x.run()?, } @@ -29,9 +30,42 @@ struct Opt { #[derive(Debug, StructOpt)] enum Command { + Run(Run), Spec(Spec), } +#[derive(Debug, StructOpt)] +struct Run { + #[structopt(parse(from_os_str))] + spec: PathBuf, +} + +impl Run { + fn run(&self) -> anyhow::Result<()> { + let spec = Specification::from_file(&self.spec)?; + + for benchmark in spec.benchmarks() { + println!("benchmark {}", benchmark.name()); + println!("start obnam server with empty chunks dir"); + println!("create empty test data directory"); + for backup in benchmark.backups() { + println!("make changes to test data"); + for change in backup.changes() { + change.execute()?; + } + println!("run backup, measure time and chunks directory"); + } + for _ in benchmark.backups() { + println!("run restore, measure time") + } + println!("stop obnam server"); + println!("clean up test data directory"); + } + + Ok(()) + } +} + #[derive(Debug, StructOpt)] struct Spec { #[structopt(parse(from_os_str))] diff --git a/src/lib.rs b/src/lib.rs index 7f3c5e4..1122ada 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,35 +1,93 @@ use serde::{Deserialize, Serialize}; +use std::fs::File; +use std::path::{Path, PathBuf}; #[derive(Debug, Serialize, Deserialize)] +#[serde(deny_unknown_fields)] pub struct Specification { benchmarks: Vec, } +#[derive(Debug, thiserror::Error)] +pub enum SpecificationError { + #[error("Couldn't open {0}: {1}")] + Open(PathBuf, std::io::Error), + + #[error("Couldn't read YAML specification from {0}:\n {1}")] + Yaml(PathBuf, serde_yaml::Error), +} + #[derive(Debug, Serialize, Deserialize)] -struct Benchmark { +pub struct Benchmark { benchmark: String, backups: Vec, } #[derive(Debug, Serialize, Deserialize)] -struct Backup { - changes: Vec, +pub struct Backup { + pub changes: Vec, } #[derive(Debug, Serialize, Deserialize)] -enum Change { - Create(CreateFiles), +pub enum Change { + #[serde(rename = "create")] + Create(Create), + + #[serde(rename = "delete")] Delete(FileCount), + + #[serde(rename = "rename")] Rename(FileCount), } #[derive(Debug, Serialize, Deserialize)] -struct CreateFiles { +pub struct Create { files: u64, file_size: u64, } #[derive(Debug, Serialize, Deserialize)] -struct FileCount { +pub struct FileCount { files: u64, } + +impl Specification { + pub fn from_file(filename: &Path) -> Result { + let f = File::open(filename) + .map_err(|err| SpecificationError::Open(filename.to_path_buf(), err))?; + let spec = serde_yaml::from_reader(f) + .map_err(|err| SpecificationError::Yaml(filename.to_path_buf(), err))?; + Ok(spec) + } + + pub fn benchmarks(&self) -> impl Iterator { + self.benchmarks.iter() + } +} + +impl Benchmark { + pub fn name(&self) -> &str { + &self.benchmark + } + + pub fn backups(&self) -> impl Iterator { + self.backups.iter() + } +} + +impl Backup { + pub fn changes(&self) -> impl Iterator { + self.changes.iter() + } +} + +impl Change { + pub fn execute(&self) -> Result<(), SpecificationError> { + match self { + Change::Create(x) => println!("create {} {}", x.files, x.file_size), + Change::Rename(x) => println!("rename {}", x.files), + Change::Delete(x) => println!("delete {}", x.files), + } + Ok(()) + } +} -- cgit v1.2.1