Skip to content

Instantly share code, notes, and snippets.

@jacobian
Last active December 29, 2021 11:21
Show Gist options
  • Save jacobian/5000515 to your computer and use it in GitHub Desktop.
Save jacobian/5000515 to your computer and use it in GitHub Desktop.
Benchmarking MongoDB's GridFS vs PostgreSQL's LargeObjects.
import io
import pymongo
import psycopg2
import gridfs
import timeit
# For fairness use the same chunk size - 512k.
CHUNK_SIZE = 1024 * 512
def bench_mongo(fs, size):
fp = io.BytesIO('x'*size)
fp.seek(0)
fs.put(fp, chunk_size=CHUNK_SIZE)
def bench_pg(conn, size):
fp = io.BytesIO('x'*size)
fp.seek(0)
lob = conn.lobject(mode='wb')
while 1:
chunk = fp.read(CHUNK_SIZE)
if not chunk:
break
lob.write(chunk)
conn.commit()
mongo = pymongo.MongoClient()
for name, size, reps in (('1k', 1024, 10000), ('10k', 10240, 10000), ('1M', 1024*1024, 1000), ('10M', 1024*1024*10, 100)):
fs = gridfs.GridFS(mongo.lobs_test_database)
t = timeit.timeit('bench_mongo(fs, %s)' % size, 'from __main__ import bench_mongo, fs', number=reps)
print "mongo: %3s x %5s: %0.2fs (%0.4fs/op)" % (name, reps, t, t/reps)
mongo.drop_database('lobs_test_database')
conn = psycopg2.connect(dbname='template1')
conn.autocommit = True
conn.cursor().execute('CREATE DATABASE lobs_test_database')
conn.close()
conn = psycopg2.connect(dbname='lobs_test_database')
t = timeit.timeit('bench_pg(conn, %s)' % size, 'from __main__ import bench_pg, conn', number=reps)
print "pgsql: %3s x %5s: %0.2fs (%0.4fs/op)" % (name, reps, t, t/reps)
conn.close()
conn = psycopg2.connect(dbname='template1')
conn.autocommit = True
conn.cursor().execute('DROP DATABASE lobs_test_database')
conn.close()
@jacobian
Copy link
Author

See updated version at https://gist.github.com/5005548, with better stats and results.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment