"""Benchmarks for data operations."""

import json
import random

import pytest


@pytest.fixture
def credit_records():
    """Generate synthetic credit records for benchmarking."""
    random.seed(42)
    return [
        {
            "loan_id": f"L{i:06d}",
            "loan_amount": random.uniform(1000, 500000),
            "interest_rate": random.uniform(0.02, 0.25),
            "credit_score": random.randint(300, 850),
            "dti_ratio": random.uniform(0.1, 0.6),
            "default_flag": random.random() < 0.05,
        }
        for i in range(10000)
    ]


def test_credit_data_serialization(benchmark, credit_records):
    """Benchmark credit data JSON serialization."""
    benchmark(json.dumps, credit_records)


def test_credit_data_filtering(benchmark, credit_records):
    """Benchmark filtering credit records by criteria."""

    def filter_records(records):
        return [r for r in records if r["credit_score"] >= 700 and r["dti_ratio"] < 0.4]

    benchmark(filter_records, credit_records)


def test_credit_data_aggregation(benchmark, credit_records):
    """Benchmark aggregation of credit data."""

    def aggregate(records):
        total = sum(r["loan_amount"] for r in records)
        avg_score = sum(r["credit_score"] for r in records) / len(records)
        default_rate = sum(1 for r in records if r["default_flag"]) / len(records)
        return {"total_exposure": total, "avg_score": avg_score, "default_rate": default_rate}

    benchmark(aggregate, credit_records)
