forked from cleveragents/cleveragents-core
62 lines
2.1 KiB
Python
62 lines
2.1 KiB
Python
"""Benchmarks for complexity scan configuration parsing and validation."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import ast
|
|
import json
|
|
from pathlib import Path
|
|
|
|
NOXFILE_PATH = Path("noxfile.py")
|
|
CI_WORKFLOW_PATH = Path(".forgejo/workflows/ci.yml")
|
|
|
|
|
|
class TimeComplexityConfigParsing:
|
|
"""Measure complexity configuration parsing performance."""
|
|
|
|
def setup(self) -> None:
|
|
"""Read source files once for reuse."""
|
|
if NOXFILE_PATH.exists():
|
|
self.noxfile_text = NOXFILE_PATH.read_text()
|
|
else:
|
|
self.noxfile_text = ""
|
|
if CI_WORKFLOW_PATH.exists():
|
|
self.ci_text = CI_WORKFLOW_PATH.read_text()
|
|
else:
|
|
self.ci_text = ""
|
|
|
|
def time_parse_noxfile_ast(self) -> None:
|
|
"""Benchmark AST parsing of noxfile.py for complexity session."""
|
|
if self.noxfile_text:
|
|
ast.parse(self.noxfile_text)
|
|
|
|
def time_extract_fail_grade_constant(self) -> None:
|
|
"""Benchmark extracting COMPLEXITY_FAIL_GRADE from noxfile AST."""
|
|
if self.noxfile_text:
|
|
tree = ast.parse(self.noxfile_text)
|
|
for node in ast.walk(tree):
|
|
if isinstance(node, ast.Assign):
|
|
for target in node.targets:
|
|
if (
|
|
isinstance(target, ast.Name)
|
|
and target.id == "COMPLEXITY_FAIL_GRADE"
|
|
and isinstance(node.value, ast.Constant)
|
|
):
|
|
_ = node.value.value
|
|
|
|
def time_parse_complexity_json_report(self) -> None:
|
|
"""Benchmark parsing a complexity JSON report."""
|
|
complexity_path = Path("build/complexity.json")
|
|
if complexity_path.exists():
|
|
text = complexity_path.read_text()
|
|
data = json.loads(text)
|
|
for blocks in data.values():
|
|
for block in blocks:
|
|
_ = block.get("rank")
|
|
|
|
def time_parse_ci_workflow_yaml(self) -> None:
|
|
"""Benchmark YAML parsing of CI workflow for quality job."""
|
|
if self.ci_text:
|
|
import yaml
|
|
|
|
yaml.safe_load(self.ci_text)
|