|
| 1 | +"""Performance tests comparing orjson vs standard json library. |
| 2 | +
|
| 3 | +These tests measure the performance improvements from using orjson |
| 4 | +for JSON serialization and deserialization in REST API requests/responses. |
| 5 | +""" |
| 6 | + |
| 7 | +import json |
| 8 | +import random |
| 9 | + |
| 10 | +import orjson |
| 11 | +import pytest |
| 12 | + |
| 13 | + |
| 14 | +def create_vector_payload(num_vectors: int, dimension: int) -> list[dict]: |
| 15 | + """Create a typical upsert payload with vectors.""" |
| 16 | + vectors = [] |
| 17 | + for i in range(num_vectors): |
| 18 | + vector = { |
| 19 | + "id": f"vec_{i}", |
| 20 | + "values": [random.random() for _ in range(dimension)], |
| 21 | + "metadata": { |
| 22 | + "category": f"cat_{i % 10}", |
| 23 | + "score": random.randint(0, 100), |
| 24 | + "tags": [f"tag_{j}" for j in range(3)], |
| 25 | + }, |
| 26 | + } |
| 27 | + vectors.append(vector) |
| 28 | + return vectors |
| 29 | + |
| 30 | + |
| 31 | +def create_query_response(num_matches: int, dimension: int, include_values: bool = True) -> dict: |
| 32 | + """Create a typical query response payload.""" |
| 33 | + matches = [] |
| 34 | + for i in range(num_matches): |
| 35 | + match = { |
| 36 | + "id": f"vec_{i}", |
| 37 | + "score": random.random(), |
| 38 | + "metadata": {"category": f"cat_{i % 10}", "score": random.randint(0, 100)}, |
| 39 | + } |
| 40 | + if include_values: |
| 41 | + match["values"] = [random.random() for _ in range(dimension)] |
| 42 | + matches.append(match) |
| 43 | + return {"matches": matches} |
| 44 | + |
| 45 | + |
| 46 | +class TestOrjsonSerialization: |
| 47 | + """Benchmark orjson.dumps() vs json.dumps().""" |
| 48 | + |
| 49 | + @pytest.mark.parametrize("num_vectors,dimension", [(10, 128), (100, 128), (100, 512)]) |
| 50 | + def test_json_dumps_vectors(self, benchmark, num_vectors, dimension): |
| 51 | + """Benchmark json.dumps() for vector payloads.""" |
| 52 | + payload = create_vector_payload(num_vectors, dimension) |
| 53 | + result = benchmark(json.dumps, payload) |
| 54 | + assert isinstance(result, str) |
| 55 | + assert len(result) > 0 |
| 56 | + |
| 57 | + @pytest.mark.parametrize("num_vectors,dimension", [(10, 128), (100, 128), (100, 512)]) |
| 58 | + def test_orjson_dumps_vectors(self, benchmark, num_vectors, dimension): |
| 59 | + """Benchmark orjson.dumps() for vector payloads.""" |
| 60 | + payload = create_vector_payload(num_vectors, dimension) |
| 61 | + result = benchmark(orjson.dumps, payload) |
| 62 | + assert isinstance(result, bytes) |
| 63 | + assert len(result) > 0 |
| 64 | + |
| 65 | + @pytest.mark.parametrize("num_matches,dimension", [(10, 128), (100, 128), (1000, 128)]) |
| 66 | + def test_json_dumps_query_response(self, benchmark, num_matches, dimension): |
| 67 | + """Benchmark json.dumps() for query responses.""" |
| 68 | + payload = create_query_response(num_matches, dimension) |
| 69 | + result = benchmark(json.dumps, payload) |
| 70 | + assert isinstance(result, str) |
| 71 | + assert len(result) > 0 |
| 72 | + |
| 73 | + @pytest.mark.parametrize("num_matches,dimension", [(10, 128), (100, 128), (1000, 128)]) |
| 74 | + def test_orjson_dumps_query_response(self, benchmark, num_matches, dimension): |
| 75 | + """Benchmark orjson.dumps() for query responses.""" |
| 76 | + payload = create_query_response(num_matches, dimension) |
| 77 | + result = benchmark(orjson.dumps, payload) |
| 78 | + assert isinstance(result, bytes) |
| 79 | + assert len(result) > 0 |
| 80 | + |
| 81 | + |
| 82 | +class TestOrjsonDeserialization: |
| 83 | + """Benchmark orjson.loads() vs json.loads().""" |
| 84 | + |
| 85 | + @pytest.mark.parametrize("num_vectors,dimension", [(10, 128), (100, 128), (100, 512)]) |
| 86 | + def test_json_loads_vectors(self, benchmark, num_vectors, dimension): |
| 87 | + """Benchmark json.loads() for vector payloads.""" |
| 88 | + payload = create_vector_payload(num_vectors, dimension) |
| 89 | + json_str = json.dumps(payload) |
| 90 | + result = benchmark(json.loads, json_str) |
| 91 | + assert isinstance(result, list) |
| 92 | + assert len(result) == num_vectors |
| 93 | + |
| 94 | + @pytest.mark.parametrize("num_vectors,dimension", [(10, 128), (100, 128), (100, 512)]) |
| 95 | + def test_orjson_loads_vectors(self, benchmark, num_vectors, dimension): |
| 96 | + """Benchmark orjson.loads() for vector payloads.""" |
| 97 | + payload = create_vector_payload(num_vectors, dimension) |
| 98 | + json_bytes = json.dumps(payload).encode("utf-8") |
| 99 | + result = benchmark(orjson.loads, json_bytes) |
| 100 | + assert isinstance(result, list) |
| 101 | + assert len(result) == num_vectors |
| 102 | + |
| 103 | + @pytest.mark.parametrize("num_matches,dimension", [(10, 128), (100, 128), (1000, 128)]) |
| 104 | + def test_json_loads_query_response(self, benchmark, num_matches, dimension): |
| 105 | + """Benchmark json.loads() for query responses.""" |
| 106 | + payload = create_query_response(num_matches, dimension) |
| 107 | + json_str = json.dumps(payload) |
| 108 | + result = benchmark(json.loads, json_str) |
| 109 | + assert isinstance(result, dict) |
| 110 | + assert len(result["matches"]) == num_matches |
| 111 | + |
| 112 | + @pytest.mark.parametrize("num_matches,dimension", [(10, 128), (100, 128), (1000, 128)]) |
| 113 | + def test_orjson_loads_query_response(self, benchmark, num_matches, dimension): |
| 114 | + """Benchmark orjson.loads() for query responses.""" |
| 115 | + payload = create_query_response(num_matches, dimension) |
| 116 | + json_bytes = json.dumps(payload).encode("utf-8") |
| 117 | + result = benchmark(orjson.loads, json_bytes) |
| 118 | + assert isinstance(result, dict) |
| 119 | + assert len(result["matches"]) == num_matches |
| 120 | + |
| 121 | + @pytest.mark.parametrize("num_matches,dimension", [(10, 128), (100, 128), (1000, 128)]) |
| 122 | + def test_orjson_loads_from_string(self, benchmark, num_matches, dimension): |
| 123 | + """Benchmark orjson.loads() with string input (like from decoded response).""" |
| 124 | + payload = create_query_response(num_matches, dimension) |
| 125 | + json_str = json.dumps(payload) |
| 126 | + result = benchmark(orjson.loads, json_str) |
| 127 | + assert isinstance(result, dict) |
| 128 | + assert len(result["matches"]) == num_matches |
| 129 | + |
| 130 | + |
| 131 | +class TestRoundTrip: |
| 132 | + """Benchmark complete round-trip serialization/deserialization.""" |
| 133 | + |
| 134 | + @pytest.mark.parametrize("num_vectors,dimension", [(10, 128), (100, 128)]) |
| 135 | + def test_json_round_trip(self, benchmark, num_vectors, dimension): |
| 136 | + """Benchmark json round-trip (dumps + loads).""" |
| 137 | + |
| 138 | + def round_trip(payload): |
| 139 | + json_str = json.dumps(payload) |
| 140 | + return json.loads(json_str) |
| 141 | + |
| 142 | + payload = create_vector_payload(num_vectors, dimension) |
| 143 | + result = benchmark(round_trip, payload) |
| 144 | + assert isinstance(result, list) |
| 145 | + assert len(result) == num_vectors |
| 146 | + |
| 147 | + @pytest.mark.parametrize("num_vectors,dimension", [(10, 128), (100, 128)]) |
| 148 | + def test_orjson_round_trip(self, benchmark, num_vectors, dimension): |
| 149 | + """Benchmark orjson round-trip (dumps + loads).""" |
| 150 | + |
| 151 | + def round_trip(payload): |
| 152 | + json_bytes = orjson.dumps(payload) |
| 153 | + return orjson.loads(json_bytes) |
| 154 | + |
| 155 | + payload = create_vector_payload(num_vectors, dimension) |
| 156 | + result = benchmark(round_trip, payload) |
| 157 | + assert isinstance(result, list) |
| 158 | + assert len(result) == num_vectors |
0 commit comments