Created
December 29, 2011 22:03
-
-
Save adamgreig/1536411 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import couchdbkit | |
import datetime | |
import hashlib | |
import base64 | |
import time | |
s = couchdbkit.Server("http://localhost:5984") | |
NUMDOCS = 1000000 | |
def setup_db(): | |
if "docid" in s: | |
s.delete_db("docid") | |
return s.create_db("docid") | |
def longid(): | |
return hashlib.sha256(str(datetime.datetime.now())).hexdigest() | |
def shortid(): | |
return base64.b64encode(hashlib.md5(str(datetime.datetime.now())).digest()) | |
seqid_count = 0 | |
def seqid(): | |
global seqid_count | |
seqid_count += 1 | |
return str(seqid_count) | |
def makedoc(docid): | |
return {"_id": docid(), "content": "uninteresting at best"} | |
def makedocs(idf, n=NUMDOCS): | |
md = lambda i: makedoc(idf) | |
return map(md, range(n)) | |
def designdoc(): | |
return { | |
"_id": "_design/docid", | |
"views": { | |
"test": { | |
"map": "function(doc) { emit(doc._id, null); }" | |
} | |
} | |
} | |
print "Using {0} documents.".format(NUMDOCS) | |
for idtype in (longid, shortid, seqid): | |
print "Using {0} for doc IDs...".format(idtype) | |
db = setup_db() | |
print "Making docs...", | |
t = time.time() | |
docs = makedocs(idtype) | |
print "done, took {0:.2} seconds.".format(time.time() - t) | |
print "Inserting docs...", | |
t = time.time() | |
for somedocs in zip(*[iter(docs)]*1000): | |
db.bulk_save(somedocs) | |
db.ensure_full_commit() | |
print "done, took {0:.2} seconds.".format(time.time() - t) | |
print "Database size: {0}MB".format(db.info()['disk_size']/(1024*1024)) | |
print "Compacting database...", | |
t = time.time() | |
db.compact() | |
print "done, took {0:.2} seconds.".format(time.time() - t) | |
print "Database size: {0}MB".format(db.info()['disk_size']/(1024*1024)) | |
print "Inserting design document...", | |
db.save_doc(designdoc()) | |
print "done." | |
print "Running view...", | |
t = time.time() | |
list(db.view("docid/test")) | |
print "done, took {0:.2} seconds.".format(time.time() - t) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment