Skip to content

Commit

Permalink
Include scope kind in scope_count metric, add bar graph
Browse files Browse the repository at this point in the history
  • Loading branch information
dcreager committed Feb 7, 2025
1 parent 8e880c8 commit 70a5373
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 7 deletions.
13 changes: 7 additions & 6 deletions crates/red_knot_python_semantic/src/semantic_index/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,12 +180,6 @@ impl<'db> SemanticIndexBuilder<'db> {
}

fn push_scope_with_parent(&mut self, node: NodeWithScopeRef, parent: Option<FileScopeId>) {
metrics::counter!(
"semantic_index.scope_count",
"file" => self.file_path.clone(),
)
.increment(1);

let children_start = self.scopes.next_index() + 1;

#[allow(unsafe_code)]
Expand All @@ -197,6 +191,13 @@ impl<'db> SemanticIndexBuilder<'db> {
};
self.try_node_context_stack_manager.enter_nested_scope();

metrics::counter!(
"semantic_index.scope_count",
"file" => self.file_path.clone(),
"kind" => scope.kind().as_str(),
)
.increment(1);

let file_scope_id = self.scopes.push(scope);
self.symbol_tables.push(SymbolTableBuilder::default());
self.use_def_maps.push(UseDefMapBuilder::default());
Expand Down
12 changes: 12 additions & 0 deletions crates/red_knot_python_semantic/src/semantic_index/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,18 @@ impl ScopeKind {
pub const fn is_comprehension(self) -> bool {
matches!(self, ScopeKind::Comprehension)
}

pub const fn as_str(self) -> &'static str {
match self {
Self::Module => "Module",
Self::Annotation => "Annotation",
Self::Class => "Class",
Self::Function => "Function",
Self::Lambda => "Lambda",
Self::Comprehension => "Comprehension",
Self::TypeAlias => "TypeAlias",
}
}
}

/// Symbol table for a specific [`Scope`].
Expand Down
17 changes: 16 additions & 1 deletion crates/ruff_metrics/plot_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@
parser.add_argument("-o", "--output", help="save image to the given filename")
subparsers = parser.add_subparsers(dest="command")

bar_parser = subparsers.add_parser("bar")
bar_parser.add_argument("key", help="the metric to render")
bar_parser.add_argument("--group-by")

counter_parser = subparsers.add_parser("counter")
counter_parser.add_argument("key", help="the counter metric to render")
counter_parser.add_argument("--group-by", required=False)
Expand Down Expand Up @@ -59,6 +63,15 @@ def show_plot():
plt.show()


def cmd_bar() -> None:
data = get_metric(all_data, args.key)
groups = data.groupby(args.group_by, as_index=False)
plt.xlabel(args.group_by)
plt.ylabel("Count")
plt.bar(args.group_by, "delta", data=groups["delta"].sum())
show_plot()


def cmd_counter() -> None:
data = get_metric(all_data, args.key)
plt.ylabel(args.key)
Expand All @@ -83,7 +96,9 @@ def cmd_histogram() -> None:
show_plot()


if args.command == "counter":
if args.command == "bar":
cmd_bar()
elif args.command == "counter":
cmd_counter()
elif args.command == "histogram":
cmd_histogram()
Expand Down

0 comments on commit 70a5373

Please sign in to comment.