Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
118 changes: 102 additions & 16 deletions apps/origin-src/benches/workspace.rs
Original file line number Diff line number Diff line change
@@ -1,26 +1,40 @@
//! Benchmarks checking a 20k-file tree out into an empty directory, durably,
//! by writing every file from the store.
//! Benchmarks creating a workspace: checking a 20k-file tree out into an
//! empty directory, durably. Files are written from the store, copied from
//! another working copy, or cloned where the file system supports it.
//!
//! Workspaces are created in the temporary directory, so set `TMPDIR` to a
//! directory on Btrfs, XFS or APFS to measure real clones. Elsewhere,
//! `cloned` falls back to copying after its first clone.

use std::io;
use std::path::Path;
use std::sync::LazyLock;
use std::thread;
use std::time::Duration;

use divan::Bencher;
use divan::counter::ItemsCount;
use origin_platform::Native;
use origin_platform::{
CloneError, CloneFile, Durability, Error, FileLock, FileSystem, FsKind, Native, Platform,
ReadAt,
};
use origin_shared::{Record, Tree, TreeId};
use origin_src::fixture::FixtureTree;
use origin_src::store::Store;
use origin_src::tree::FileChange;
use origin_src::worktree::{StatCache, Worktree};
use origin_src::worktree::{CloneSource, StatCache, Worktree};

fn main() {
divan::main();
}

const TREE: FixtureTree = FixtureTree::LARGE;

/// The main working copy: the fixture, with a snapshot taken.
/// The main working copy: the fixture, with a snapshot taken after its files
/// settled, so its stat cache covers every file.
struct Main {
dir: tempfile::TempDir,
cache: StatCache,
/// Every file of the snapshot, as file changes that a checkout applies.
changes: Vec<FileChange>,
}
Expand All @@ -32,24 +46,96 @@ static MAIN: LazyLock<Main> = LazyLock::new(|| {
store.init().unwrap();
let empty = Tree::default().encode();
store.write_all(std::slice::from_ref(&empty)).unwrap();
let (root, _) = Worktree::new(dir.path(), &store)
// The stat cache only vouches for files that changed well before the
// scan.
thread::sleep(Duration::from_millis(2_100));
let (root, cache) = Worktree::new(dir.path(), &store)
.snapshot(&StatCache::default())
.unwrap();
let changes = store.diff(TreeId::from(empty.id()), root).unwrap();
Main { dir, changes }
Main {
dir,
cache: cache.unwrap(),
changes,
}
});

impl Main {
/// Checks the tree out into new directories with `platform`, copying or
/// cloning from the main working copy if `clone_from_main`.
fn checkout<P: Platform>(&self, bencher: Bencher<'_, '_>, platform: P, clone_from_main: bool) {
let store = Store::new(self.dir.path().join(".origin"), platform);
bencher
.counter(ItemsCount::new(self.changes.len()))
.with_inputs(|| {
let sources = if clone_from_main {
vec![CloneSource::new(
self.dir.path().to_owned(),
self.cache.clone(),
)]
} else {
Vec::new()
};
(tempfile::tempdir().unwrap(), sources)
})
.bench_local_refs(|(workspace, sources)| {
Worktree::new(workspace.path(), &store)
.checkout(&self.changes, sources, None)
.unwrap();
});
}
}

/// Checks out by reading every file from the store, decompressing and
/// verifying it, and writing it.
#[divan::bench(sample_count = 10, sample_size = 1)]
fn from_store(bencher: Bencher<'_, '_>) {
let store = Store::new(MAIN.dir.path().join(".origin"), Native::default());
bencher
.counter(ItemsCount::new(MAIN.changes.len()))
.with_inputs(|| tempfile::tempdir().unwrap())
.bench_local_refs(|workspace| {
Worktree::new(workspace.path(), &store)
.checkout(&MAIN.changes, None)
.unwrap();
});
MAIN.checkout(bencher, Native::default(), false);
}

/// Checks out by copying every file from the main working copy.
#[divan::bench(sample_count = 10, sample_size = 1)]
fn copied(bencher: Bencher<'_, '_>) {
MAIN.checkout(bencher, NoClones, true);
}

/// Checks out by cloning every file from the main working copy, where the
/// file system supports it.
#[divan::bench(sample_count = 10, sample_size = 1)]
fn cloned(bencher: Bencher<'_, '_>) {
MAIN.checkout(bencher, Native::default(), true);
}

/// The native platform, pretending the file system cannot clone.
#[derive(Clone, Copy, Debug, Default)]
struct NoClones;

impl Durability for NoClones {
fn sync_batch<'f>(&self, files: impl IntoIterator<Item = &'f Path>) -> Result<(), Error> {
Native::default().sync_batch(files)
}
}

impl FileLock for NoClones {}

impl ReadAt for NoClones {}

impl CloneFile for NoClones {
fn cannot_clone(&self, _: &io::Error) -> bool {
true
}

fn clone_file(&self, from: &Path, to: &Path) -> Result<(), CloneError> {
Err(CloneError::Unsupported {
from: from.to_owned(),
to: to.to_owned(),
source: io::ErrorKind::Unsupported.into(),
})
}
}

impl FileSystem for NoClones {
fn file_system(&self, path: &Path) -> io::Result<FsKind> {
Native::default().file_system(path)
}
}
2 changes: 1 addition & 1 deletion apps/origin-src/src/workspace/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,7 @@ impl Workspace {
unchanged: Option<&StatCache>,
) -> Result<(), WorkspaceError> {
let changes = self.store.diff(from, to)?;
Ok(Worktree::new(&self.root, &self.store).checkout(&changes, unchanged)?)
Ok(Worktree::new(&self.root, &self.store).checkout(&changes, &[], unchanged)?)
}

/// `origin status`: prints the working copy's change, what it is on
Expand Down
Loading
Loading