pylibmc – Kestrel Queue Client – Simple Test using Memcache Protocol

Installation

pylibmc is a great Python client for memcached written in C.

sudo pip install pylibmc

Mini Benchmark


cat > kestrel-benchmark.py <<- "_EOF_"
# -*- coding: utf-8 -*-
import pylibmc
import sys, time, datetime

def main():
    # time python kestrel-benchmark.py
    if len(sys.argv) < 2:
        print("need write or read parameter")
        return
    mc = pylibmc.Client(['127.0.0.1:22133'])

    i, action = 0, sys.argv[1]
    max=1000000
    queue_name="test_queue"
    print( str(datetime.datetime.now())+" start "+action+"  ... ")
    t1 = time.time()
    if action == 'write':
        i = queue_write(mc, max, queue_name)
    else:
        i = queue_read(mc, queue_name)
    t2= time.time()
    duration = (t2-t1)*1000.0
    text = str(datetime.datetime.now())+" "+action+" "+str(i)+" entries in "+('% 4.f ms' % duration)
    print(text)

def queue_write(mc, max, queue_name):
    for i in range(1,max+1):
        mc.set(queue_name, "queue entry!"+str(i))
    return i

def queue_read(mc, queue_name):
    i, entry = 0, True
    while entry:
    	   entry = mc.get(queue_name+"/open")
    	   # do something with entry
    	   if entry:
    	       closed = mc.get(queue_name+"/close")
    	       i+=1
    return i

if __name__ == '__main__':
    main()

_EOF_

time python kestrel-benchmark.py write
#2013-04-15 14:47:16.451786 start write  ...
#2013-04-15 14:49:12.313367 write 1000000 entries in  115862 ms
#
#real	1m55.909s
#user	0m10.361s
#sys	0m25.702s

time python kestrel-benchmark.py read
#2013-04-15 14:49:37.881742 start read  ...
#2013-04-15 14:53:56.641919 read 1100000 entries in  258760 ms
#
#real	4m18.800s
#user	0m16.649s
#sys	0m59.640s

References

1. pylibmc Home:https://pypi.python.org/pypi/pylibmc
2. pylibmc Documentation: http://sendapatch.se/projects/pylibmc/
3. Kestrel Installation Guide: /371/kestrel-queue-installation-in-ubuntu