from multiprocessing import Process
import random
import time

import memcache


def get_next(num):
    numbers = range(num)
    while True:
        yield random.choice(numbers)


def test(calls=1000, keys=100):
    print "Starting Test"
    mc = memcache.Client(["127.0.0.1:7000"])
    # hack to make sure we force the connection before the first call
    for server in mc.servers:
        server.connect()

    elapsed = 0
    total = 0
    numbers = get_next(keys)
    hits = 0
    miss = 0
    for _ in range(calls):
        num = numbers.next()
        key = "test:%s" % num
        start = time.time()
        result = mc.get(key)
        end = time.time()
        if not result:
            miss += 1
        else:
            hits += 1
        elapsed += end - start
        total += 1

    print "Total: %s" % total
    print "Total Elapsed: %s" % elapsed
    print "Average: %s" % (elapsed / total)
    print "Req/sec: %s" % (total / elapsed)
    print "Hits: %s" % hits
    print "Miss: %s" % miss
    print "Hit Ratio: %s" % (float(hits) / float(hits + miss))


procs = []
for _ in range(10):
    p = Process(target=test, args=(100000, 5000))
    p.start()
    procs.append(p)

for p in procs:
    p.join()

@@ -63,24 +63,26 @@
 %0Aimport 
+py
 memcache
 %0A%0A%0Adef g
@@ -73,16 +73,23 @@
 memcache
+.client
 %0A%0A%0Adef g
@@ -249,16 +249,18 @@
    mc = 
+py
 memcache
@@ -264,16 +264,23 @@
 che.
+client.
 Client(
-%5B
+(
 %22127
@@ -289,140 +289,16 @@
 .0.1
-:
+%22, 
 7000
-%22%5D)%0A    # hack to make sure we force the connection before the first call%0A    for server in mc.servers:%0A        server.connect(
+)
 )%0A%0A 
@@ -456,13 +456,9 @@
  = %22
-test:
+?
 %25s%22 

use pymemcache in benchmark, its faster