Skip to content

Commit

Permalink
Merge pull request #68 from alfred82santa/feature/performance-tests
Browse files Browse the repository at this point in the history
Added performace test, BlobField and slots for models
  • Loading branch information
alfred82santa committed Sep 15, 2014
2 parents 1da49bd + 3213eef commit d9a37fe
Show file tree
Hide file tree
Showing 7 changed files with 117 additions and 0 deletions.
1 change: 1 addition & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[email protected]
3 changes: 3 additions & 0 deletions dirty_models/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ class BaseData():
"""
Base class for data inside dirty model
"""

__slots__ = []

_locked = None
_read_only = None
_parent = None
Expand Down
4 changes: 4 additions & 0 deletions dirty_models/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -452,3 +452,7 @@ def __init__(self, model_class=None, **kwargs):

def convert_value(self, value):
return self._model_class(data=value, field_type=self.field_type)


class BlobField(BaseField):
pass
32 changes: 32 additions & 0 deletions performance/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
from datetime import datetime, timedelta
from sys import setrecursionlimit
from functools import reduce


class Runner:

def __init__(self, config):
self.config = config

def run(self):
setrecursionlimit(9999999)

result = {}
for label, data in self.config.items():
test = data['test_class'](**data['params'])
test.prepare()
result_test = []
print ('{0} start'.format(label))
for i in range(data.get('repeats', 1)):
print ('{0}: iteration no. {1} start'.format(label, i))
time_start = datetime.now()
test.run()
time_stop = datetime.now()
elapsed = time_stop - time_start
print ('{0}: iteration no. {1} => {2}'.format(label, i, str(elapsed)))
result_test.append(elapsed)
total = reduce(lambda acc, x: acc + x, result_test, timedelta())
print ('{0} => {1}'.format(label, str(total)))
result[label] = {'results': result_test, 'total': total}

return result
25 changes: 25 additions & 0 deletions performance/blobfield.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
'''
Created on 15/09/2014
:author: alfred
'''
from dirty_models.models import BaseModel
from dirty_models.fields import BlobField
from performance.dynamicmodel import create_dict


class FakeDynModel(BaseModel):
fake_data = BlobField()


class BlobFieldPerformance:

def __init__(self, depth=5, children_count=5):
self.depth = depth
self.children_count = children_count

def prepare(self):
self.data = create_dict(self.depth, self.children_count)

def run(self):
return FakeDynModel(data={'fake_data': self.data})
31 changes: 31 additions & 0 deletions performance/dynamicmodel.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
'''
Created on 15/09/2014
:author: alfred
'''
from dirty_models.models import DynamicModel, BaseModel
from dirty_models.fields import ModelField


def create_dict(depth=5, children_count=5):
if depth > 0:
return {'test_{0}'.format(i): create_dict(depth - 1, children_count) for i in range(children_count)}
else:
return 'top'


class FakeDynModel(BaseModel):
fake_data = ModelField(model_class=DynamicModel)


class DynamicModelPerformance:

def __init__(self, depth=5, children_count=5):
self.depth = depth
self.children_count = children_count

def prepare(self):
self.data = create_dict(self.depth, self.children_count)

def run(self):
return FakeDynModel(data={'fake_data': self.data})
21 changes: 21 additions & 0 deletions performancerunner.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
'''
Created on 15/09/2014
:author: alfred
'''
from performance import Runner
from performance.dynamicmodel import DynamicModelPerformance
from performance.blobfield import BlobFieldPerformance

config = {'DynamicModel': {'test_class': DynamicModelPerformance,
'repeats': 5,
'params': {'depth': 6, 'children_count': 6}},
'BlobField': {'test_class': BlobFieldPerformance,
'repeats': 5,
'params': {'depth': 6, 'children_count': 6}}}

if __name__ == '__main__':

runner = Runner(config)

print(runner.run())

0 comments on commit d9a37fe

Please sign in to comment.