Skip to content
This repository has been archived by the owner on May 13, 2020. It is now read-only.

Commit

Permalink
ZODB performance
Browse files Browse the repository at this point in the history
  • Loading branch information
agroszer committed Jul 16, 2013
1 parent 2f4bff2 commit 3749a52
Showing 1 changed file with 116 additions and 23 deletions.
139 changes: 116 additions & 23 deletions src/mongopersist/performance.py
Expand Up @@ -14,10 +14,12 @@
"""Mongo Persistence Performance Test"""
from __future__ import absolute_import
import optparse
import os
import persistent
import pymongo
import random
import sys
import tempfile
import time
import transaction
import cPickle
Expand All @@ -26,6 +28,12 @@
from mongopersist import conflict, datamanager
from mongopersist.zope import container

import zope.container
import zope.container.btree
import ZODB
import ZODB.FileStorage


MULTIPLE_CLASSES = True


Expand Down Expand Up @@ -74,10 +82,7 @@ def printResult(self, text, t1, t2, count=None):
def getPeople(self, options):
pass

def run_basic_crud(self, options):
people = self.getPeople(options)

peopleCnt = len(people)
def slow_read(self, people, peopleCnt):
# Profile slow read
transaction.begin()
t1 = time.time()
Expand All @@ -88,6 +93,7 @@ def run_basic_crud(self, options):
transaction.commit()
self.printResult('Slow Read', t1, t2, peopleCnt)

def fast_read_values(self, people, peopleCnt):
# Profile fast read (values)
transaction.begin()
t1 = time.time()
Expand All @@ -98,6 +104,7 @@ def run_basic_crud(self, options):
transaction.commit()
self.printResult('Fast Read (values)', t1, t2, peopleCnt)

def fast_read(self, people, peopleCnt):
# Profile fast read
transaction.begin()
t1 = time.time()
Expand All @@ -108,6 +115,7 @@ def run_basic_crud(self, options):
transaction.commit()
self.printResult('Fast Read (find)', t1, t2, peopleCnt)

def object_caching(self, people, peopleCnt):
# Profile object caching
transaction.begin()
t1 = time.time()
Expand Down Expand Up @@ -142,28 +150,44 @@ def run_basic_crud(self, options):
transaction.commit()
self.printResult('Fast Read (caching x4)', t1, t2, peopleCnt*4)

def modify(self, people, peopleCnt):
# Profile modification
t1 = time.time()
def modify():
for person in list(people.values()):
person.name += 'X'
person.age += 1
transaction.commit()
modify()
#cProfile.runctx(
# 'modify()', globals(), locals())
t2 = time.time()
self.printResult('Modification', t1, t2, peopleCnt)

def delete(self, people, peopleCnt):
# Profile deletion
t1 = time.time()
for name in people.keys():
del people[name]
transaction.commit()
t2 = time.time()
self.printResult('Deletion', t1, t2, peopleCnt)

def run_basic_crud(self, options):
people = self.getPeople(options)

peopleCnt = len(people)

self.slow_read(people, peopleCnt)
self.fast_read_values(people, peopleCnt)
self.fast_read(people, peopleCnt)
self.object_caching(people, peopleCnt)

if options.modify:
# Profile modification
t1 = time.time()
def modify():
for person in list(people.find()):
person.name += 'X'
person.age += 1
transaction.commit()
modify()
#cProfile.runctx(
# 'modify()', globals(), locals())
t2 = time.time()
self.printResult('Modification', t1, t2, peopleCnt)
self.modify(people, peopleCnt)

if options.delete:
# Profile deletion
t1 = time.time()
for name in people.keys():
del people[name]
transaction.commit()
t2 = time.time()
self.printResult('Deletion', t1, t2, peopleCnt)
self.delete(people, peopleCnt)


class PerformanceMongo(PerformanceBase):
Expand Down Expand Up @@ -198,6 +222,72 @@ def getPeople(self, options):
return people


class PeopleZ(zope.container.btree.BTreeContainer):
pass

class AddressZ(persistent.Persistent):

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

class PersonZ(persistent.Persistent, zope.container.contained.Contained):

def __init__(self, name, age):
self.name = name
self.age = age
self.address = AddressZ('Boston %i' %age)

def __repr__(self):
return '<%s %s @ %i [%s]>' %(
self.__class__.__name__, self.name, self.age, self.__name__)

class Person2Z(Person):
pass


class PerformanceZODB(PerformanceBase):
personKlass = PersonZ
person2Klass = Person2Z

def getPeople(self, options):
folder = tempfile.gettempdir()
#folder = './' # my /tmp is a tmpfs
fname = os.path.join(folder, 'performance_data.fs')
if options.reload:
try:
os.remove(fname)
except:
pass
fs = ZODB.FileStorage.FileStorage(fname)
db = ZODB.DB(fs)
conn = db.open()

root = conn.root()

if options.reload:
root['people'] = people = PeopleZ()
transaction.commit()

# Profile inserts
transaction.begin()
t1 = time.time()
for idx in xrange(options.size):
klass = (self.personKlass if (MULTIPLE_CLASSES and idx % 2)
else self.person2Klass)
name = 'Mr Number %.5i' % idx
people[name] = klass(name, random.randint(0, 100))
transaction.commit()
t2 = time.time()
self.printResult('Insert', t1, t2, options.size)
else:
people = root['people']

return people

def fast_read(self, people, peopleCnt):
pass


parser = optparse.OptionParser()
parser.usage = '%prog [options]'

Expand Down Expand Up @@ -228,4 +318,7 @@ def main(args=None):
args = sys.argv[1:]
options, args = parser.parse_args(args)

print 'MONGO ---------------'
PerformanceMongo().run_basic_crud(options)
print 'ZODB ---------------'
PerformanceZODB().run_basic_crud(options)

0 comments on commit 3749a52

Please sign in to comment.