From 278f2df9e72afcd163c45948a12dc38a936bbc11 Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Fri, 10 Dec 2021 16:10:54 +0200 Subject: add an abstraction for managing an Obnam system Sponsored-by: author --- src/obnam.rs | 9 +++------ src/suite.rs | 31 +++++++++++++++++-------------- 2 files changed, 20 insertions(+), 20 deletions(-) diff --git a/src/obnam.rs b/src/obnam.rs index 8100797..e6badb9 100644 --- a/src/obnam.rs +++ b/src/obnam.rs @@ -6,7 +6,6 @@ use tempfile::{tempdir, TempDir}; const SERVER_PORT: u16 = 8888; - /// An Obnam system. /// /// Manage an Obnam server and run the Obnam client. @@ -26,7 +25,7 @@ pub enum ObnamError { impl Obnam { pub fn new() -> Result { - let o = Self{ + let o = Self { configs: tempdir()?, root: tempdir()?, chunks: tempdir()?, @@ -69,13 +68,11 @@ impl Obnam { Ok(()) } - pub fn backup(&mut self) -> Result<(), ObnamError> - { + pub fn backup(&mut self) -> Result<(), ObnamError> { Ok(()) } - pub fn restore(&mut self) -> Result<(), ObnamError> - { + pub fn restore(&mut self) -> Result<(), ObnamError> { Ok(()) } } diff --git a/src/suite.rs b/src/suite.rs index 2e4cede..31d5fcc 100644 --- a/src/suite.rs +++ b/src/suite.rs @@ -1,3 +1,4 @@ +use crate::obnam::{Obnam, ObnamError}; use crate::result::{Measurement, OpMeasurements, Operation}; use crate::specification::{Create, FileCount}; use crate::step::Step; @@ -5,7 +6,6 @@ use log::{debug, info}; use std::fs::File; use std::path::{Path, PathBuf}; use std::time::Instant; -use tempfile::{tempdir, TempDir}; use walkdir::WalkDir; /// A running benchmark suite. @@ -34,6 +34,10 @@ pub enum SuiteError { /// Error looking up file metadata. #[error("Error looking up file metadata: {0}: {1}")] FileMeta(PathBuf, walkdir::Error), + + /// Error managing an Obnam system. + #[error(transparent)] + Obnam(#[from] ObnamError), } impl Suite { @@ -96,14 +100,17 @@ impl Suite { struct Benchmark { name: String, - tempdir: Option, + // We store an Obnam in an Option so that we can destroy the + // Obnam, and thereby delete any temporary files. We want to do + // that intentionally, so that it can be measured. + obnam: Option, } impl Benchmark { fn new(name: &str) -> Result { Ok(Self { name: name.to_string(), - tempdir: Some(tempdir()?), + obnam: Some(Obnam::new()?), }) } @@ -111,12 +118,8 @@ impl Benchmark { &self.name } - fn tempdir(&self) -> &TempDir { - self.tempdir.as_ref().unwrap() - } - - fn path(&self) -> &Path { - self.tempdir().path() + fn obnam(&self) -> &Obnam { + self.obnam.as_ref().unwrap() } fn start(&mut self) -> Result { @@ -126,17 +129,17 @@ impl Benchmark { fn stop(&mut self) -> Result { info!("ending benchmark {}", self.name); - self.tempdir.take().unwrap().close()?; + self.obnam.take().unwrap(); // This destroys the Obnam Ok(OpMeasurements::new(self.name(), Operation::Stop)) } fn create(&mut self, create: &Create) -> Result { info!("creating {} test data files", create.files); - let tempdir = self.tempdir().path(); - debug!("creating {} files in {}", create.files, tempdir.display()); + let root = self.obnam().root(); + debug!("creating {} files in {}", create.files, root.display()); for i in 0..create.files { - let filename = tempdir.join(format!("{}", i)); + let filename = root.join(format!("{}", i)); debug!("creating {}", filename.display()); File::create(&filename).map_err(|err| SuiteError::CreateFile(filename, err))?; } @@ -157,7 +160,7 @@ impl Benchmark { fn backup(&mut self, n: usize) -> Result { info!("making backup {} in benchmark {}", n, self.name()); let mut om = OpMeasurements::new(self.name(), Operation::Backup); - let stats = filestats(self.path())?; + let stats = filestats(self.obnam().root())?; om.push(Measurement::TotalFiles(stats.count)); om.push(Measurement::TotalData(stats.size)); Ok(om) -- cgit v1.2.1