ronny-rentner / UltraDict

Sychronized, streaming Python dictionary that uses shared memory as a backend
Apache License 2.0
272 stars 25 forks source link

Memory usage analysis #14

Closed csrgxtu closed 2 years ago

csrgxtu commented 2 years ago

before image

testing... image

after image

seems ok, ultra-dict didnt eats memory after test done. -- i am afraid it allocates memory and did't release thus the server will oom finally.

if you have any thoughts to test it plz let me know, i want to use ultra-dict in our prod env but afraid something went wrong.

csrgxtu commented 2 years ago

btw it is my test script:

# -*- coding: utf-8 -*-
import time
from functools import wraps
from typing import Dict, List

import pytest
from UltraDict import UltraDict

def timing(f):
    @wraps(f)
    async def wrap(*args, **kw):
        ts = int(time.time() * 1000)
        result = await f(*args, **kw)
        te = int(time.time() * 1000)
        print(f'{f.__name__} took: {te-ts} mil-sec to load {len(result.keys())}')
        return result
    return wrap

@timing
async def load_into_default_dict(input: List[List]) -> Dict:
    """load input data into default dict

    Args:
        input (List[List]): _description_

    Returns:
        Dict: dict
    """
    default_dict = {}
    for row in input:
        default_dict.update({row[0]: row[1]})
    return default_dict

@timing
async def load_into_ultra_dict(input: List[List]) -> object:
    """load input data into ultra dict

    Args:
        input (List[List]): _description_

    Returns:
        object: UltraDict
    """
    ultra_dict = UltraDict(buffer_size=len(input))
    for row in input:
        ultra_dict[row[0]] = row[1]
    return ultra_dict

@pytest.mark.asyncio
async def test_ultra_dict() -> None:
    """test ultra-dict perm and correctness
    """
    lines = []
    with open('./domain_status.txt', 'r') as file:
        for line in file:
            parts = line.strip().split('\t')
            lines.append([parts[0], int(parts[1])])

    normal_dict = await load_into_default_dict(lines)
    udict = await load_into_ultra_dict(lines)

    # compare normal dict value with ultra-dict value for key
    for key in normal_dict:
        assert normal_dict.get(key) == udict.get(key)
ronny-rentner commented 2 years ago

One hint: In the main branch, there are a few bugs regarding release of shared memory buffers in some situations. This is fixed in the dev branch.