Files
cleveragents-core/benchmarks/context_fragment_models_bench.py
freemo 10abf8985e
CI / benchmark-publish (pull_request) Has been skipped
CI / lint (pull_request) Successful in 15s
CI / quality (pull_request) Successful in 19s
CI / build (pull_request) Successful in 29s
CI / security (pull_request) Successful in 33s
CI / typecheck (pull_request) Successful in 35s
CI / unit_tests (pull_request) Successful in 2m15s
CI / docker (pull_request) Successful in 39s
CI / integration_tests (pull_request) Successful in 3m4s
CI / coverage (pull_request) Successful in 5m1s
CI / lint (push) Successful in 12s
CI / build (push) Successful in 15s
CI / quality (push) Successful in 16s
CI / security (push) Successful in 31s
CI / typecheck (push) Successful in 33s
CI / benchmark-regression (push) Has been skipped
CI / unit_tests (push) Successful in 3m20s
CI / integration_tests (push) Successful in 4m10s
CI / docker (push) Successful in 1m3s
CI / coverage (push) Successful in 4m20s
CI / benchmark-publish (push) Successful in 16m0s
CI / benchmark-regression (pull_request) Successful in 31m40s
feat(acms): add ContextFragment and ScoredFragment data models
Created ScoredFragment frozen model wrapping ContextFragment with
composite_score, score_breakdown, and rank fields. Added pipeline-
specific fragment models in domain/contexts/ with proper equality
based on uko_uri + detail_depth for deduplication support.

ISSUES CLOSED: #538
2026-03-05 14:11:23 +00:00

153 lines
4.9 KiB
Python

"""ASV benchmarks for ScoredFragment and ContextFragment pipeline models.
Measures the performance of:
- ScoredFragment creation (with and without breakdown)
- ScoredFragment factory methods
- ScoredFragment equality comparison
- ScoredFragment set-based deduplication
- ContextFragment with strategy_source
"""
from __future__ import annotations
import importlib
import sys
from pathlib import Path
_SRC = str(Path(__file__).resolve().parents[1] / "src")
if _SRC not in sys.path:
sys.path.insert(0, _SRC)
import cleveragents # noqa: E402
importlib.reload(cleveragents)
from cleveragents.domain.contexts.fragment import ScoredFragment # noqa: E402
from cleveragents.domain.models.core.context_fragment import ( # noqa: E402
ContextFragment,
FragmentProvenance,
)
_DEFAULT_PROV = FragmentProvenance(resource_uri="bench://default")
def _make_fragment(**kwargs: object) -> ContextFragment:
"""Create a ContextFragment with sensible benchmark defaults."""
defaults: dict[str, object] = {
"uko_node": "bench://file",
"content": "benchmark content",
"token_count": 10,
"provenance": _DEFAULT_PROV,
}
defaults.update(kwargs)
return ContextFragment(**defaults) # type: ignore[arg-type]
class ScoredFragmentCreationSuite:
"""Benchmark ScoredFragment creation throughput."""
def setup(self) -> None:
"""Pre-create fragment for repeated scored-fragment construction."""
self._fragment = _make_fragment()
self._fragment_with_relevance = _make_fragment(relevance_score=0.85)
self._breakdown = {
"relevance": 0.9,
"hierarchy": 0.8,
"recency": 0.95,
}
def time_scored_fragment_minimal(self) -> None:
"""Benchmark creating a ScoredFragment with minimal fields."""
ScoredFragment(
fragment=self._fragment,
composite_score=0.85,
)
def time_scored_fragment_with_breakdown(self) -> None:
"""Benchmark creating a ScoredFragment with full breakdown."""
ScoredFragment(
fragment=self._fragment,
composite_score=0.92,
score_breakdown=self._breakdown,
rank=1,
)
def time_scored_fragment_from_relevance(self) -> None:
"""Benchmark ScoredFragment.from_relevance factory."""
ScoredFragment.from_relevance(self._fragment_with_relevance)
def time_scored_fragment_from_factory(self) -> None:
"""Benchmark ScoredFragment.from_fragment factory."""
ScoredFragment.from_fragment(
self._fragment,
composite_score=0.6,
score_breakdown=self._breakdown,
rank=3,
)
class ScoredFragmentEqualitySuite:
"""Benchmark ScoredFragment equality and hashing."""
def setup(self) -> None:
"""Pre-create scored fragments for comparison benchmarks."""
frag_a = _make_fragment(
uko_node="bench://file/a",
content="A",
detail_depth=3,
)
frag_b = _make_fragment(
uko_node="bench://file/a",
content="B",
detail_depth=3,
)
self._scored_a = ScoredFragment.from_fragment(frag_a, composite_score=0.5)
self._scored_b = ScoredFragment.from_fragment(frag_b, composite_score=0.9)
# Build a list of 100 fragments (50 unique uko_node values)
self._scored_list = [
ScoredFragment.from_fragment(
_make_fragment(
uko_node=f"bench://file/{i % 50}",
content=f"content {i}",
detail_depth=3,
),
composite_score=round((i % 10) / 10, 1),
)
for i in range(100)
]
def time_equality_check(self) -> None:
"""Benchmark equality comparison."""
_ = self._scored_a == self._scored_b
def time_hash_computation(self) -> None:
"""Benchmark hash computation."""
hash(self._scored_a)
def time_set_deduplication_100(self) -> None:
"""Benchmark set-based deduplication of 100 scored fragments."""
set(self._scored_list)
class ContextFragmentStrategySuite:
"""Benchmark ContextFragment with strategy_source field."""
def time_fragment_with_strategy_source(self) -> None:
"""Benchmark creating a ContextFragment with strategy_source."""
ContextFragment(
uko_node="bench://file",
content="benchmark content",
token_count=10,
provenance=_DEFAULT_PROV,
strategy_source="keyword-search",
)
def time_fragment_default_strategy_source(self) -> None:
"""Benchmark creating a ContextFragment with default strategy_source."""
ContextFragment(
uko_node="bench://file",
content="benchmark content",
token_count=10,
provenance=_DEFAULT_PROV,
)