"""Benchmarks for DAG operations."""

import json


def test_dag_serialization_small(benchmark, small_dag):
    """Benchmark JSON serialization of small DAG."""
    benchmark(json.dumps, small_dag)


def test_dag_serialization_medium(benchmark, medium_dag):
    """Benchmark JSON serialization of medium DAG."""
    benchmark(json.dumps, medium_dag)


def test_dag_serialization_large(benchmark, large_dag):
    """Benchmark JSON serialization of large DAG."""
    benchmark(json.dumps, large_dag)


def test_dag_deserialization(benchmark, large_dag):
    """Benchmark JSON deserialization of large DAG."""
    data = json.dumps(large_dag)
    benchmark(json.loads, data)


def test_dag_traversal_bfs(benchmark, large_dag):
    """Benchmark BFS traversal of large DAG."""

    def bfs(dag):
        adj = {}
        for n in dag["nodes"]:
            adj[n] = []
        for src, dst in dag["edges"]:
            adj[src].append(dst)
        visited = set()
        queue = [dag["nodes"][0]]
        while queue:
            node = queue.pop(0)
            if node not in visited:
                visited.add(node)
                queue.extend(adj.get(node, []))
        return visited

    benchmark(bfs, large_dag)


def test_dag_topological_sort(benchmark, large_dag):
    """Benchmark topological sort of large DAG."""

    def topo_sort(dag):
        adj = {}
        in_degree = {}
        for n in dag["nodes"]:
            adj[n] = []
            in_degree[n] = 0
        for src, dst in dag["edges"]:
            adj[src].append(dst)
            in_degree[dst] = in_degree.get(dst, 0) + 1
        queue = [n for n in dag["nodes"] if in_degree.get(n, 0) == 0]
        result = []
        while queue:
            node = queue.pop(0)
            result.append(node)
            for neighbor in adj[node]:
                in_degree[neighbor] -= 1
                if in_degree[neighbor] == 0:
                    queue.append(neighbor)
        return result

    benchmark(topo_sort, large_dag)
