Use example of ZODB for storing Python objects

[ permalink ] [ download ]
# Flávio Codeço Coelho
# http://pyinsci.blogspot.com/2007/09/zodb-vs-relational-database-simple.html

import time, os, glob
import sqlite3
import ZODB
from   ZODB import FileStorage, DB
import pylab as P

def zInserts(n):
    print "Inserting %s records into ZODB"%n
    for i in xrange(n):
        dbroot[i] = {'name':'John Doe','sex':1,'age':35}
    connection.transaction_manager.commit()

def zInserts2(n):
    print "Inserting %s records into ZODB"%n
    dbroot['employees'] = [{'name':'John Doe','sex':1,'age':35} for i in xrange(n)]
    connection.transaction_manager.commit()


def testSqlite3Disk(n):
    print "Inserting %s records into SQLite(Disk) with sqlite3 module"%n
    conn = sqlite3.connect('dbsql')
    c = conn.cursor()
    # Create table
    c.execute('''create table Person(name text, sex integer, age integer)''')
    persons = [('john doe', 1, 35) for i in xrange(n)]
    c.executemany("insert into Person(name, sex, age) values (?,?,?)", persons)
    c.execute('select * from Person')
    print "Number of records selected: %s"%len(c.fetchall())
    c.execute('drop table Person')


recsize = [1000,5000,10000,50000,100000,200000,400000,600000,800000,1000000]
zperf = []
sqlperf =[]
for n in recsize:
    # remove old databases
    if os.path.exists('testdb.fs'):
        [os.remove(i) for i in glob.glob('testdb.fs*')]
    if os.path.exists('dbsql'):
        os.remove('dbsql')
    # setup ZODB storage
    dbpath = 'testdb.fs'
    storage     = FileStorage.FileStorage(dbpath)
    db          = DB(storage)
    connection  = db.open()
    dbroot      = connection.root()
    #begin tests
    t0 = time.clock()
    zInserts(n)
    t1 = time.clock()
    # closing and reopening ZODB' database to make sure
    # we are reading from file and not from some memory cache
    connection.close()
    db.close()
    storage  = FileStorage.FileStorage(dbpath)
    db  = DB(storage)
    connection  = db.open()
    dbroot = connection.root()
    t2 = time.clock()
    print "Number of records read from ZODB: %s"%len(dbroot.items())
    t3 = time.clock()
    ztime = (t1-t0)+(t3-t2)
    zperf.append(ztime)
    print 'Time for ZODB: %s seconds\n'%ztime
    t4 = time.clock()
    testSqlite3Disk(n)
    t5 = time.clock()
    stime = (t5-t4)
    sqlperf.append(stime)
    print 'Time for Sqlite3 with db on Disk: %s seconds\n'%stime
P.plot(recsize,zperf,'-v',recsize,sqlperf,'-^')
P.legend(['ZODB','SQLite3'])
P.xlabel('inserts')
P.ylabel('time(s)')
P.show()

http://pyinsci.blogspot.com/2007/09/zodb-vs-relational-database-simple.html
hits counter