test_memory.py

Overview

`test_memory.py` is a test suite focused on verifying that the `orjson` library’s JSON serialization and deserialization functions (`loads` and `dumps`) do not cause memory leaks under various scenarios. The tests utilize the `pytest` framework and rely on the `psutil` library to monitor the process’s resident memory size (RSS) before and after repeated operations. By running intensive loops of JSON encoding/decoding and checking memory usage, these tests ensure `orjson` maintains efficient memory management and garbage collection.

The file also includes tests for serialization of complex data types, such as Python dataclasses, numpy arrays, pandas Series, and pytz timezone objects, covering a wide range of typical usage patterns. It uses a constant memory increase threshold to assert no significant leak occurs during orjson operations.


Classes and Functions

Constants and Fixtures


Function: default(obj) -> str


Dataclasses

Member

Object


Class: Unsupported


Class: TestMemory

This class contains multiple pytest test methods to check memory behavior of `orjson` under various conditions. All tests that require `psutil` are skipped if the library is unavailable.

Methods:


test_memory_loads(self)


test_memory_loads_memoryview(self)


test_memory_dumps(self)


test_memory_loads_exc(self)


test_memory_dumps_exc(self)


test_memory_dumps_default(self)


test_memory_dumps_dataclass(self)


test_memory_dumps_pytz_tzinfo(self)


test_memory_loads_keys(self)


test_memory_dumps_numpy(self)


test_memory_dumps_pandas(self)


test_memory_dumps_fragment(self)


Important Implementation Details


Interaction with Other Parts of the System


Usage Example

Run all tests with pytest, ensuring `psutil` and optionally other dependencies are installed:

pip install pytest psutil orjson numpy pandas pytz
pytest test_memory.py

The test results will confirm that `orjson` does not leak memory under heavy serialization/deserialization workloads.


Mermaid Class Diagram

classDiagram
    class Member {
        +int id
        +bool active
    }
    class Object {
        +int id
        +datetime.datetime updated_at
        +str name
        +list~Member~ members
    }
    class Unsupported

    class TestMemory {
        +test_memory_loads()
        +test_memory_loads_memoryview()
        +test_memory_dumps()
        +test_memory_loads_exc()
        +test_memory_dumps_exc()
        +test_memory_dumps_default()
        +test_memory_dumps_dataclass()
        +test_memory_dumps_pytz_tzinfo()
        +test_memory_loads_keys()
        +test_memory_dumps_numpy()
        +test_memory_dumps_pandas()
        +test_memory_dumps_fragment()
    }

    Object o-- Member : contains

Summary

`test_memory.py` is a comprehensive test suite that systematically exercises `orjson`’s serialization and deserialization functions under different data scenarios and monitors memory usage to detect leaks. It leverages system monitoring tools and Python’s garbage collection controls to create rigorous memory tests, ensuring robustness of `orjson` for high-performance JSON processing in Python applications.