Skip to content

Commit

Permalink
index: make focus index calculate-churn a command
Browse files Browse the repository at this point in the history
Other organizations would conceivably be interested in this information, so surface it better.
  • Loading branch information
arxanas committed Sep 13, 2022
1 parent ee42353 commit 449699b
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 24 deletions.
22 changes: 22 additions & 0 deletions focus/commands/src/cli/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,7 @@ fn feature_name_for(subcommand: &Subcommand) -> String {
Subcommand::Upgrade { .. } => "upgrade".to_string(),
Subcommand::Index { subcommand } => match subcommand {
IndexSubcommand::Clear { .. } => "index-clear".to_string(),
IndexSubcommand::CalculateChurn { .. } => "index-calculate-churn".to_string(),
IndexSubcommand::Fetch { .. } => "index-fetch".to_string(),
IndexSubcommand::Get { .. } => "index-get".to_string(),
IndexSubcommand::Generate { .. } => "index-generate".to_string(),
Expand Down Expand Up @@ -494,6 +495,18 @@ enum IndexSubcommand {
sparse_repo: PathBuf,
},

/// Calculate statistics on cache invalidation for the projects in the
/// repository.
CalculateChurn {
/// Path to the sparse repository.
#[clap(long, parse(from_os_str), default_value = ".")]
sparse_repo: PathBuf,

/// The number of commits backwards to examine.
#[clap(long, default_value = "1000")]
num_commits: usize,
},

/// Fetch the pre-computed index for the repository.
Fetch {
/// Path to the sparse repository.
Expand Down Expand Up @@ -1036,6 +1049,15 @@ fn run_subcommand(app: Arc<App>, tracker: &Tracker, options: FocusOpts) -> Resul
Ok(ExitCode(0))
}

IndexSubcommand::CalculateChurn {
sparse_repo,
num_commits,
} => {
let sparse_repo_path = paths::find_repo_root_from(app.clone(), sparse_repo)?;
focus_operations::index::print_churn_stats(app, sparse_repo_path, num_commits)?;
Ok(ExitCode(0))
}

IndexSubcommand::Fetch {
sparse_repo,
force,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
// Copyright 2022 Twitter, Inc.
// SPDX-License-Identifier: Apache-2.0

//! Run with
//!
//! ```bash
//! cargo run --example calc_invalidation_rate -- ~/workspace/path/to/repo 10000
//! ```
use std::{
collections::HashMap,
ops::Deref,
Expand All @@ -17,23 +11,14 @@ use std::{
},
};

use clap::Parser;
use focus_internals::{
use crate::{
index::{content_hash, ContentHash, DependencyKey, HashContext},
model::{repo::Repo, selection::Project},
target::{Target, TargetSet},
};
use focus_util::{app::App, git_helper};
use rayon::iter::{IntoParallelRefIterator, ParallelIterator};

#[derive(Parser, Debug)]
struct Opts {
sparse_repo_path: PathBuf,

/// The number of commits backward from the `HEAD` commit to sample.
num_commits: usize,
}

struct RepoPool {
repo_path: PathBuf,
repos: Arc<Mutex<Vec<git2::Repository>>>,
Expand Down Expand Up @@ -90,13 +75,12 @@ fn average(values: impl IntoIterator<Item = f64>) -> f64 {
sum / len
}

fn main() -> anyhow::Result<()> {
let Opts {
sparse_repo_path,
num_commits,
} = Opts::parse();

let app = Arc::new(App::new(false, None, None, None)?);
/// Calculate and print statistics on target/project churn.
pub fn print_churn_stats(
app: Arc<App>,
sparse_repo_path: PathBuf,
num_commits: usize,
) -> anyhow::Result<()> {
let repo = Repo::open(&sparse_repo_path, app)?;
let selections = repo.selection_manager()?;
let all_projects: HashMap<String, Project> = selections
Expand Down
2 changes: 2 additions & 0 deletions focus/internals/src/lib/index/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@
#![warn(missing_docs)]

mod churn;
mod content_hash;
mod dependency_graph;
mod object_database;

pub use churn::print_churn_stats;
pub use content_hash::{content_hash, ContentHash, HashContext};
pub use dependency_graph::{
get_files_to_materialize, update_object_database_from_resolution, DependencyKey,
Expand Down
10 changes: 9 additions & 1 deletion focus/operations/src/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use focus_util::paths::assert_focused_repo;
use tracing::{debug, debug_span, info};

use focus_internals::index::{
content_hash, get_files_to_materialize, ContentHash, DependencyKey, HashContext,
self, content_hash, get_files_to_materialize, ContentHash, DependencyKey, HashContext,
ObjectDatabase, PathsToMaterializeResult, RocksDBCache, RocksDBMemoizationCacheExt,
FUNCTION_ID,
};
Expand Down Expand Up @@ -400,6 +400,14 @@ pub fn push(
Ok(ExitCode(0))
}

pub fn print_churn_stats(
app: Arc<App>,
sparse_repo: PathBuf,
num_commits: usize,
) -> anyhow::Result<()> {
index::print_churn_stats(app, sparse_repo, num_commits)
}

#[cfg(test)]
mod tests {
use focus_testing::ScratchGitRepo;
Expand Down

0 comments on commit 449699b

Please sign in to comment.