BSDDB copyright and licensing restrictions while in use via Python
Warren Postma
embed at geocities.com
Tue Feb 15 10:21:01 EST 2000
Wowee. I ran my little "hammer on the database" benchmark (which took over 5
hours to complete using the bsddb included in Python) and so far it's
running approximately 3 times faster using your bsddb.
Good work Robin!
Warren
-- test script --
# DBTEST2.PY
from bsddb2 import db
filename = "c:/temp/TestBsdDb.db"
#import bsddb # built in bsddb
#filename = "c:/temp/TestBsdDb.db"
import random
import time
import string
# full test range
start,end = 4,17
# quick test
#start,end = 4,8
print "BSD Database Test in Python (Version 2)"
print
descriptions = [ "Add Rows", "Read Keys", "Shuffle", "Read Rows", "Delete
75%", "Close" ]
List1 = [ "Abc", "Def", "Ghi", "Jkl", "Mno", "Pqr", "Stu", "Vwx", "Yz" ]
List2 = [ "X123", "Y456", "Z789", "Y012", "Z345", "X678", "Y901", "Z234",
"X567" ]
# Shuffle an array like you might shuffle cards
# Note: This is intended to be Good Enough, Not Perfect.
# We limit shuffle operations to 100 per data set!
def Shuffle(ar):
sz = len(ar)
th = sz/2
lp = sz/4
if (lp > 100):
lp = 100
# now move a bunch of cards up or down:
x1 = random.randrange(0,sz/2)
x2 = random.randrange(0,sz/2)+(sz/2)
c = random.randrange(0,sz/4)
ar[x1:x1+c], ar[x2:x2-c] = ar[x2:x2-c], ar[x1:x1+c]
for k in range(0,lp):
# do a little random substitution to kick things off
for i in range(0,lp):
x = random.randrange(0,th)
y = random.randrange(th,sz)
ar[x],ar[y] = ar[y], ar[x]
# rough scramble of sections:
def testset(testindex,RowCount,db):
times=[]
starttime = time.clock()
bytesread=0
print "---- Storing "+`RowCount`+" rows in the database
"+`testindex`+" ) ----"
for n in range(0,RowCount):
r = random.randrange(0,8)
V = List1[r]*200
K = List2[r]+"-"+`n`+`testindex` # avoid inorder insertion of keys
db[K] = K+':'+V # DB Btree-lookup Key 'K' has value 'V'
times.append(time.clock()-starttime)
# Get Keys
#print "Read Keys"
Keys = db.keys()
N = len(Keys)
times.append(time.clock()-starttime)
print "Shuffling Key array... (slow!)"
# Scramble Keys But Good...
Shuffle(Keys)
# print Keys[0:10] # taste and see
times.append(time.clock()-starttime)
print "After inserting ",RowCount," rows the Key Count is now ",
len(Keys)
bytesread = 0
#print "Reading Rows, in Random Order"
for r in Keys:
x = db[r]
bytesread = bytesread + len(x)
print "Bytes read = ", `bytesread`
times.append(time.clock()-starttime)
# Delete 75% of the data in the database:
delcount = len(Keys) - ( len(Keys)/4 )
for k in Keys[0:delcount]:
del db[k]
db.sync()
Keys = db.keys();
print "After deleting, the key count is ", len(Keys)
times.append(time.clock()-starttime)
#print "Closing"
#print "Done"
#times.append(time.clock()-starttime)
print "Elapsed Times:"
for i in range(0,5):
print string.ljust(descriptions[i],20), ": ", times[i]
print "-----------------"
print
def testloop():
#db1 = bsddb.btopen( filename, "n" )
db1 = db.DB()
db1.open(filename, db.DB_BTREE, db.DB_CREATE)
for i in range(start,end):
testset(i,long(20**(i/4.0)),db1)
db1.close()
# Quick Shuffle test:
#for i in range(0,10):
# X = List1[:]
# Shuffle(X)
# print X
testloop()
More information about the Python-list
mailing list