Skip to content

Instantly share code, notes, and snippets.

@smerritt
Created September 19, 2013 20:06
Show Gist options
  • Save smerritt/6629111 to your computer and use it in GitHub Desktop.
Save smerritt/6629111 to your computer and use it in GitHub Desktop.
bench more queue stuff, now with differences between sleeps and full queues
#!/usr/bin/env python
import benchmark
import Queue
import os
import subprocess
import tempfile
import time
import threading
class BenchQueueStuff(benchmark.Benchmark):
def setUp(self):
self.size = 10000
self.queue = Queue.Queue()
self.tempdir = tempfile.mkdtemp()
for i in xrange(self.size):
dirname = os.path.join(self.tempdir, "testdir-%d" % i)
os.mkdir(dirname)
with open(os.path.join(dirname, "somefile"), "w") as fh:
fh.write("abcdef")
# Knock all this crap out of buffer cache to simulate a loaded disk.
# (This makes a *huge* difference in the runtime.)
subprocess.check_call(['sudo', 'sync'])
subprocess.check_call(['sudo', 'sh', '-c',
'echo 3 > /proc/sys/vm/drop_caches'])
def test_big_batch(self):
for i in xrange(self.size):
self.queue.put(i)
for _ in xrange(self.size):
# should never block, but shit happens
self.queue.get(block=False)
def test_one_at_a_time(self):
for i in xrange(self.size):
self.queue.put(i)
self.queue.get()
def test_producer_consumer_thread_with_sleeps(self):
def consume():
for _ in xrange(self.size):
self.queue.get()
thr = threading.Thread(target=consume)
thr.daemon = True
thr.start()
for i in xrange(self.size):
time.sleep(0.000001)
self.queue.put(i)
thr.join()
def test_producer_consumer_thread_with_small_queue(self):
self.queue = Queue.Queue(5)
def consume():
for _ in xrange(self.size):
self.queue.get()
thr = threading.Thread(target=consume)
thr.daemon = True
thr.start()
for i in xrange(self.size):
self.queue.put(i)
thr.join()
def test_producer_consumer_with_disk_io(self):
def consume():
for _ in xrange(self.size):
n = self.queue.get()
testdir = os.path.join(self.tempdir, "testdir-%d" % n)
for fname in os.listdir(testdir):
os.unlink(os.path.join(testdir, fname))
thr = threading.Thread(target=consume)
thr.daemon = True
thr.start()
for i in xrange(self.size):
time.sleep(0.000001)
self.queue.put(i)
thr.join()
if __name__ == '__main__':
benchmark.main(each=1, numberFormat="%.4g")
Benchmark Report
================
BenchQueueStuff
---------------
name | rank | runs | mean | sd | timesBaseline
------------------------------------------|------|------|---------|----|--------------
big batch | 1 | 1 | 0.04339 | NA | 1.0
one at a time | 2 | 1 | 0.04536 | NA | 1.04521229836
producer consumer thread with small queue | 3 | 1 | 0.1663 | NA | 3.83239198277
producer consumer thread with sleeps | 4 | 1 | 1.426 | NA | 32.8556931563
producer consumer with disk io | 5 | 1 | 4.935 | NA | 113.718045361
Each of the above 5 runs were run in random, non-consecutive order by
`benchmark` v0.1.5 (http://jspi.es/benchmark) with Python 2.7.3
Linux-3.2.0-53-generic-x86_64 on 2013-09-12 08:26:26.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment