Skip to content

Instantly share code, notes, and snippets.

@snaury
Created October 27, 2012 20:36
Show Gist options
  • Save snaury/3966086 to your computer and use it in GitHub Desktop.
Save snaury/3966086 to your computer and use it in GitHub Desktop.
Tests MongoDB/PostgreSQL performance under different concurrency settings
For PostgreSQL you'd need to increase your kernel shared memory quotas. For Mac OS X to set it to 40MBs do:
sudo sysctl -w kern.sysv.shmmax=41943040
sudo sysctl -w kern.sysv.shmall=10240
Then in mydb/postgresql.conf change:
max_connections = 128
More info here:
http://www.postgresql.org/docs/9.2/static/kernel-resources.html
import os
import sys
import time
import random
import gevent
import gevent.monkey
gevent.monkey.patch_all()
import pymongo
import psycopg2
import gevent_psycopg2
gevent_psycopg2.monkey_patch()
class Runner(object):
def __init__(self, maxconnections=1, maxtime=10.0):
self.maxconnections = maxconnections
self.maxtime = maxtime
def run(self, kind):
counts = []
starts = []
target = getattr(self, 'worker_%s' % (kind,))
workers = [gevent.spawn(target, counts, starts, self.maxtime) for _ in xrange(self.maxconnections)]
gevent.joinall(workers)
mintime = None
maxtime = None
for t0 in starts:
if mintime is None or mintime > t0:
mintime = t0
if maxtime is None or maxtime < t0:
maxtime = t0
return sum(counts, 0.0), maxtime - mintime
def worker_mongodb(self, counts, starts, maxtime):
table = pymongo.Connection()['mydb']['table']
n = 0
t0 = time.time()
starts.append(t0)
while True:
table.insert({'t': int(time.time()), 'v': random.randint(0, 1000)}, safe=True)
n += 1
dt = time.time() - t0
if dt >= maxtime:
counts.append(float(n) / float(dt))
break
def worker_postgres(self, counts, starts, maxtime):
conn = psycopg2.connect("host=localhost dbname=mydb")
cursor = conn.cursor()
cursor.execute("commit")
n = 0
t0 = time.time()
starts.append(t0)
while True:
cursor.execute("insert into bigtable (timestamp, value) values (%s, %s)", (int(time.time()), random.randint(0, 1000)))
n += 1
dt = time.time() - t0
if dt >= maxtime:
counts.append(float(n) / float(dt))
break
for i in xrange(8):
print 2 ** i, 'connections'
for kind in ('mongodb', 'postgres'):
print '...%s:' % kind,
ops, dev = Runner(2 ** i).run(kind)
print '%.3f inserts/s, %dms startup deviation' % (ops, dev * 1000)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment