Created
December 21, 2013 00:28
-
-
Save drsnyder/8063741 to your computer and use it in GitHub Desktop.
upsert test
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 os | |
import random | |
import time | |
import psycopg2 | |
COUNTERS = 5 | |
THREADS = 50 | |
ITERATIONS = 100 | |
def increment(): | |
outf = open('synctest.out.%d' % os.getpid(), 'w') | |
conn = psycopg2.connect(database="drsnyder_byc", user="postgres", host="beast") | |
cur = conn.cursor() | |
for i in range(0,ITERATIONS): | |
time.sleep(random.random()) | |
start = time.time() | |
cur.execute("SELECT increment_view_count('galleryAlbumView', %s, 1)", [random.randint(1,COUNTERS)]) | |
conn.commit() | |
outf.write("%f\n" % (time.time() - start)) | |
conn.close() | |
outf.close() | |
def update(): | |
outf = open('synctest.update', 'w') | |
conn = psycopg2.connect(database="drsnyder_byc", user="postgres", host="beast") | |
cur = conn.cursor() | |
for i in range(0,ITERATIONS): | |
time.sleep(random.random()) | |
start = time.time() | |
cur.execute("SELECT update_gallery_album_view_counts_from_stats_views()") | |
conn.commit() | |
outf.write("%f\n" % (time.time() - start)) | |
conn.close() | |
pids = [] | |
for i in range(THREADS): | |
pid = os.fork() | |
if pid != 0: | |
print 'Process %d spawned' % pid | |
pids.append(pid) | |
else: | |
print 'Starting child %d' % os.getpid() | |
increment() | |
print 'Exiting child %d' % os.getpid() | |
os._exit(0) | |
update() | |
for pid in pids: | |
print "waiting on %d" % pid | |
os.waitpid(pid, 0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment