Files
cleveragents-core/benchmarks/decision_model_bench.py

193 lines
6.2 KiB
Python

"""ASV benchmarks for decision domain model operations.
Measures the performance of:
- Decision model construction (Pydantic validation)
- DecisionType enum access
- ContextSnapshot construction
- Decision serialization (model_dump / model_validate)
"""
from __future__ import annotations
import sys
from pathlib import Path
try:
from cleveragents.domain.models.core.decision import (
ArtifactRef,
ContextSnapshot,
Decision,
DecisionType,
ResourceRef,
)
except ModuleNotFoundError:
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "src"))
from cleveragents.domain.models.core.decision import (
ArtifactRef,
ContextSnapshot,
Decision,
DecisionType,
ResourceRef,
)
from ulid import ULID
_PLAN_ID = str(ULID())
_PARENT_ID = str(ULID())
_RESOURCE_ID = str(ULID())
class DecisionConstructionSuite:
"""Benchmark Decision model construction."""
def time_minimal_root_decision(self) -> None:
"""Benchmark minimal prompt_definition creation."""
Decision(
plan_id=_PLAN_ID,
sequence_number=0,
decision_type=DecisionType.PROMPT_DEFINITION,
question="What should we build?",
chosen_option="A REST API",
)
def time_child_decision_with_confidence(self) -> None:
"""Benchmark child decision with confidence score."""
Decision(
plan_id=_PLAN_ID,
parent_decision_id=_PARENT_ID,
sequence_number=1,
decision_type=DecisionType.STRATEGY_CHOICE,
question="Which framework?",
chosen_option="FastAPI",
alternatives_considered=["Flask", "Django", "Starlette"],
confidence_score=0.85,
)
def time_decision_with_snapshot(self) -> None:
"""Benchmark decision with full context snapshot."""
Decision(
plan_id=_PLAN_ID,
parent_decision_id=_PARENT_ID,
sequence_number=2,
decision_type=DecisionType.IMPLEMENTATION_CHOICE,
question="How to implement auth?",
chosen_option="JWT tokens",
context_snapshot=ContextSnapshot(
hot_context_hash="sha256:bench123",
hot_context_ref="store://snapshots/bench123",
relevant_resources=[
ResourceRef(resource_id=_RESOURCE_ID, path="src/main.py"),
ResourceRef(resource_id=str(ULID())),
],
actor_state_ref="checkpoint://actor/bench",
),
)
def time_correction_decision(self) -> None:
"""Benchmark correction decision creation."""
Decision(
plan_id=_PLAN_ID,
parent_decision_id=_PARENT_ID,
sequence_number=3,
decision_type=DecisionType.STRATEGY_CHOICE,
question="Which framework?",
chosen_option="Django instead",
is_correction=True,
corrects_decision_id=str(ULID()),
correction_reason="Changed requirements",
)
def time_decision_with_artifacts(self) -> None:
"""Benchmark decision with artifacts list."""
Decision(
plan_id=_PLAN_ID,
parent_decision_id=_PARENT_ID,
sequence_number=4,
decision_type=DecisionType.TOOL_INVOCATION,
question="Which tool?",
chosen_option="code_editor",
artifacts_produced=[
ArtifactRef(artifact_path=f"src/file_{i}.py") for i in range(10)
],
)
class DecisionSerializationSuite:
"""Benchmark Decision serialization round-trips."""
def setup(self) -> None:
self.decision = Decision(
plan_id=_PLAN_ID,
parent_decision_id=_PARENT_ID,
sequence_number=5,
decision_type=DecisionType.STRATEGY_CHOICE,
question="Architecture choice?",
chosen_option="Microservices",
alternatives_considered=["Monolith", "Serverless"],
confidence_score=0.78,
context_snapshot=ContextSnapshot(
hot_context_hash="sha256:serial",
hot_context_ref="store://serial",
relevant_resources=[
ResourceRef(resource_id=_RESOURCE_ID, path="src/app.py"),
],
actor_state_ref="checkpoint://serial",
),
artifacts_produced=[
ArtifactRef(artifact_path="src/service.py"),
],
)
self.dump = self.decision.model_dump()
def time_model_dump(self) -> None:
"""Benchmark model_dump serialization."""
self.decision.model_dump()
def time_model_dump_json(self) -> None:
"""Benchmark model_dump_json serialization."""
self.decision.model_dump_json()
def time_model_validate(self) -> None:
"""Benchmark model_validate deserialization."""
Decision.model_validate(self.dump)
def time_as_cli_dict(self) -> None:
"""Benchmark as_cli_dict rendering."""
self.decision.as_cli_dict()
class ContextSnapshotSuite:
"""Benchmark ContextSnapshot construction."""
def time_empty_snapshot(self) -> None:
"""Benchmark empty snapshot creation."""
ContextSnapshot()
def time_snapshot_with_resources(self) -> None:
"""Benchmark snapshot with multiple resource refs."""
ContextSnapshot(
hot_context_hash="sha256:bench",
hot_context_ref="store://bench",
relevant_resources=[
ResourceRef(resource_id=str(ULID()), path=f"src/f{i}.py")
for i in range(20)
],
actor_state_ref="checkpoint://bench",
)
class DecisionTypeEnumSuite:
"""Benchmark DecisionType enum operations."""
def time_enum_access(self) -> None:
"""Benchmark accessing an enum member."""
_ = DecisionType.PROMPT_DEFINITION
def time_enum_from_value(self) -> None:
"""Benchmark creating enum from string value."""
DecisionType("strategy_choice")
def time_enum_iteration(self) -> None:
"""Benchmark iterating all enum members."""
list(DecisionType)