|
| 1 | +"""Benchmarks for Expression compilation and Q operator construction.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +import pytest |
| 6 | + |
| 7 | +from mongoz.core.db.querysets.expressions import Expression, SortExpression |
| 8 | +from mongoz.core.db.querysets.operators import Q |
| 9 | +from mongoz.utils.enums import ExpressionOperator |
| 10 | + |
| 11 | + |
| 12 | +@pytest.mark.benchmark |
| 13 | +def test_bench_expression_compile_eq(): |
| 14 | + """Benchmark compiling a simple equality expression.""" |
| 15 | + expr = Expression(key="name", operator=ExpressionOperator.EQUAL, value="test") |
| 16 | + for _ in range(100): |
| 17 | + expr.compile() |
| 18 | + |
| 19 | + |
| 20 | +@pytest.mark.benchmark |
| 21 | +def test_bench_expression_compile_regex(): |
| 22 | + """Benchmark compiling a regex expression.""" |
| 23 | + expr = Expression( |
| 24 | + key="name", operator=ExpressionOperator.PATTERN, value="test.*pattern" |
| 25 | + ) |
| 26 | + for _ in range(100): |
| 27 | + expr.compile() |
| 28 | + |
| 29 | + |
| 30 | +@pytest.mark.benchmark |
| 31 | +def test_bench_expression_compile_startswith(): |
| 32 | + """Benchmark compiling a startswith expression.""" |
| 33 | + expr = Expression( |
| 34 | + key="name", operator=ExpressionOperator.STARTSWITH, value="prefix" |
| 35 | + ) |
| 36 | + for _ in range(100): |
| 37 | + expr.compile() |
| 38 | + |
| 39 | + |
| 40 | +@pytest.mark.benchmark |
| 41 | +def test_bench_expression_compile_many(): |
| 42 | + """Benchmark compiling multiple expressions at once.""" |
| 43 | + expressions = [ |
| 44 | + Expression(key="name", operator=ExpressionOperator.EQUAL, value="test"), |
| 45 | + Expression(key="age", operator=ExpressionOperator.GREATER_THAN, value=18), |
| 46 | + Expression(key="year", operator=ExpressionOperator.LESS_THAN, value=2024), |
| 47 | + Expression( |
| 48 | + key="email", operator=ExpressionOperator.PATTERN, value=".*@example.com" |
| 49 | + ), |
| 50 | + Expression( |
| 51 | + key="status", operator=ExpressionOperator.IN, value=["active", "pending"] |
| 52 | + ), |
| 53 | + ] |
| 54 | + for _ in range(100): |
| 55 | + Expression.compile_many(expressions) |
| 56 | + |
| 57 | + |
| 58 | +@pytest.mark.benchmark |
| 59 | +def test_bench_expression_unpack(): |
| 60 | + """Benchmark unpacking a dictionary into expressions.""" |
| 61 | + d = { |
| 62 | + "name": "test", |
| 63 | + "year": {"$gt": 1990, "$lt": 2024}, |
| 64 | + "status": "active", |
| 65 | + } |
| 66 | + for _ in range(100): |
| 67 | + Expression.unpack(d) |
| 68 | + |
| 69 | + |
| 70 | +@pytest.mark.benchmark |
| 71 | +def test_bench_q_operator_construction(): |
| 72 | + """Benchmark constructing Q operator expressions.""" |
| 73 | + for _ in range(100): |
| 74 | + Q.eq("name", "test") |
| 75 | + Q.neq("status", "inactive") |
| 76 | + Q.gt("age", 18) |
| 77 | + Q.lt("year", 2024) |
| 78 | + Q.gte("score", 80) |
| 79 | + Q.lte("price", 100.0) |
| 80 | + |
| 81 | + |
| 82 | +@pytest.mark.benchmark |
| 83 | +def test_bench_q_in_operator(): |
| 84 | + """Benchmark constructing Q in/not_in expressions.""" |
| 85 | + values = ["active", "pending", "approved", "rejected"] |
| 86 | + for _ in range(100): |
| 87 | + Q.in_("status", values) |
| 88 | + Q.not_in("status", values) |
| 89 | + |
| 90 | + |
| 91 | +@pytest.mark.benchmark |
| 92 | +def test_bench_q_logical_operators(): |
| 93 | + """Benchmark constructing logical Q operators (and, or, nor).""" |
| 94 | + expr1 = Q.eq("name", "test") |
| 95 | + expr2 = Q.gt("age", 18) |
| 96 | + expr3 = Q.lt("year", 2024) |
| 97 | + for _ in range(100): |
| 98 | + Q.and_(expr1, expr2, expr3) |
| 99 | + Q.or_(expr1, expr2, expr3) |
| 100 | + |
| 101 | + |
| 102 | +@pytest.mark.benchmark |
| 103 | +def test_bench_q_ordering(): |
| 104 | + """Benchmark constructing ordering expressions.""" |
| 105 | + for _ in range(100): |
| 106 | + Q.asc("name") |
| 107 | + Q.desc("created_at") |
| 108 | + Q.asc("year") |
| 109 | + Q.desc("score") |
| 110 | + |
| 111 | + |
| 112 | +@pytest.mark.benchmark |
| 113 | +def test_bench_sort_expression_compile(): |
| 114 | + """Benchmark compiling sort expressions.""" |
| 115 | + from mongoz.core.db.datastructures import Order |
| 116 | + |
| 117 | + sort_expr = SortExpression(key="name", direction=Order.ASCENDING) |
| 118 | + for _ in range(100): |
| 119 | + sort_expr.compile() |
0 commit comments