Skip to content

Commit

Permalink
Replace json to orjson for performance.
Browse files Browse the repository at this point in the history
  • Loading branch information
ralgond committed Dec 7, 2024
1 parent 05471d3 commit b426e82
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 13 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ jobs:

- name: Install dependencies
run: |
pip install pytest pytest-cov mysql-connector-python Pympler
pip install pytest pytest-cov mysql-connector-python Pympler orjson
- name: Set up MySQL client
run: sudo apt-get install mysql-client
Expand Down
3 changes: 1 addition & 2 deletions mybatis/cache.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import json
import pickle
import orjson as json
import time
from typing import Dict, Any, Optional

Expand Down
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
packages=find_packages(),
install_requires=[
'mysql-connector-python>=9.0.0',
'Pympler>=1.1'
'Pympler>=1.1',
'orjson>=3.10.12'
],
classifiers=[
'Programming Language :: Python :: 3',
Expand Down
20 changes: 11 additions & 9 deletions test/test_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@
from mybatis import Cache, CacheKey

def test_basic():
cache = Cache(memory_limit=555, max_live_ms=10*1000) # 50MB, 10sec
cache = Cache(memory_limit=400, max_live_ms=10*1000) # 50MB, 10sec
cache.put(CacheKey("a", [1, 'a', None]), [{"a1": 1}, {"a2": 2}])
cache.put(CacheKey("b", [2, 'b', None]), "2")
cache.put(CacheKey("c", [3, 'c', None]), "3")
cache.put(CacheKey("d", [4, 'd', None]), None)

print ("============> memory_used:", cache.memory_used)

assert cache.get(CacheKey('a', [1, 'a', None])) == None
assert cache.get(CacheKey('b', [2, 'b', None])) == '2'

Expand All @@ -18,13 +20,13 @@ def test_basic():
l.append((key, value, memory_usage, type(value)))

assert len(l) == 3
assert l[0][0] == '{"sql": "b", "param_list": [2, "b", null]}'
assert l[0][0] == b'{"sql":"b","param_list":[2,"b",null]}'
assert l[0][1] == '2'

assert l[1][0] == '{"sql": "d", "param_list": [4, "d", null]}'
assert l[1][0] == b'{"sql":"d","param_list":[4,"d",null]}'
assert l[1][1] == None

assert l[2][0] == '{"sql": "c", "param_list": [3, "c", null]}'
assert l[2][0] == b'{"sql":"c","param_list":[3,"c",null]}'
assert l[2][1] == '3'

cache.put(CacheKey("e", [5, 'e', None]), "5")
Expand All @@ -36,17 +38,17 @@ def test_basic():

assert len(l) == 3

assert l[0][0] == '{"sql": "e", "param_list": [5, "e", null]}'
assert l[0][0] == b'{"sql":"e","param_list":[5,"e",null]}'
assert l[0][1] == '5'

assert l[1][0] == '{"sql": "b", "param_list": [2, "b", null]}'
assert l[1][0] == b'{"sql":"b","param_list":[2,"b",null]}'
assert l[1][1] == '2'

assert l[2][0] == '{"sql": "d", "param_list": [4, "d", null]}'
assert l[2][0] == b'{"sql":"d","param_list":[4,"d",null]}'
assert l[2][1] == None

def test_timeout():
cache = Cache(memory_limit=555, max_live_ms=1 * 1000) # 50MB, 10sec
cache = Cache(memory_limit=400, max_live_ms=1 * 1000) # 50MB, 10sec
cache.put(CacheKey("a", [1, 'a', None]), [{"a1": 1}, {"a2": 2}])
cache.put(CacheKey("b", [2, 'b', None]), "2")
cache.put(CacheKey("c", [3, 'c', None]), "3")
Expand All @@ -61,7 +63,7 @@ def test_timeout():
assert cache.memory_used == 0

def test_overwrite():
cache = Cache(memory_limit=180, max_live_ms=10 * 1000) # 50MB, 10sec
cache = Cache(memory_limit=120, max_live_ms=10 * 1000) # 50MB, 10sec
cache.put(CacheKey("a", [1, 'a', None]), [{"a1": 1}, {"a2": 2}])
#print ("++++>cache.memory_used:", cache.memory_used)
cache.put(CacheKey("a", [1, 'a', None]), [{"a1": 1}, {"a2": 2}, {"a3":3}])
Expand Down

0 comments on commit b426e82

Please sign in to comment.