from multiprocessing import Process
import random
import time

import pymemcache.client


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


def test(calls=1000, keys=100):
    print "Starting Test"
    mc = pymemcache.client.Client(("127.0.0.1", 7000))

    elapsed = 0
    total = 0
    numbers = get_next(keys)
    hits = 0
    miss = 0
    for _ in range(calls):
        num = numbers.next()
        key = "?%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()

@@ -28,16 +28,23 @@
  Process
+, Queue
 %0Aimport 
@@ -198,16 +198,25 @@
 ef test(
+results, 
 calls=10
@@ -234,34 +234,8 @@
 0):%0A
-    print %22Starting Test%22%0A
     
@@ -446,9 +446,8 @@
  = %22
-?
 %25s%22 
@@ -675,166 +675,897 @@
     
-print %22Total: %25s%22 %25 total%0A    print %22Total Elapsed: %25s%22 %25 elapsed%0A    print %22Average: %25s%22 %25 (elapsed / total)%0A    print %22Req/sec: %25s%22 %25 (total / elapsed)%0A    
+results.put((total, hits, miss))%0A%0A%0Arequests_per_process = 100000%0Apossible_urls = 5000%0Aworkers = 10%0Aprocs = %5B%5D%0Aresults = Queue()%0Astart = time.time()%0A%0Aprint %22Starting %25s Workers%22 %25 workers%0Afor _ in range(workers):%0A    p = Process(target=test, args=(results, requests_per_process, possible_urls))%0A    p.start()%0A    procs.append(p)%0A%0Afor p in procs:%0A    p.join()%0Aend = time.time()%0A%0Aelapsed = end - start%0Atotal = 0%0Ahits = 0%0Amiss = 0%0Awhile not results.empty():%0A    one_total, one_hits, one_miss = results.get()%0A    total += one_total%0A    hits += one_hits%0A    miss += one_miss%0A%0Aprint %22=%22 * 30%0Aprint %22Num Workers:%5Ct%5Ct%25s%22 %25 workers%0Aprint %22Expected Requests:%5Ct%25s%22 %25 (requests_per_process * workers)%0Aprint %22Actual Request:%5Ct%5Ct%25s%22 %25 total%0Aprint %22Elapsed Time (sec):%5Ct%252.4f%22 %25 elapsed%0Aprint %22Total Reqs/Sec:%5Ct%5Ct%25.4f%22 %25 (total / elapsed)%0Aprint %22Req/Sec Per Worker:%5Ct%25.4f%22 %25 ((total / elapsed) / workers)%0A
 prin
@@ -1572,17 +1572,22 @@
 t %22Hits:
- 
+%5Ct%5Ct%5Ct
 %25s%22 %25 hi
@@ -1589,20 +1589,16 @@
  %25 hits%0A
-    
 print %22M
@@ -1600,18 +1600,25 @@
 nt %22Miss
-: 
+es:%5Ct%5Ct%5Ct
 %25s%22 %25 mi
@@ -1620,20 +1620,16 @@
  %25 miss%0A
-    
 print %22H
@@ -1637,19 +1637,25 @@
 t Ratio:
- %25s
+%5Ct%5Ct%252.4f
 %22 %25 (flo
@@ -1675,165 +1675,27 @@
 oat(
-hits + miss))%0A%0A%0Aprocs = %5B%5D%0Afor _ in range(10):%0A    p = Process(target=test, args=(100000, 5000))%0A    p.start()%0A    procs.append(p)%0A%0Afor p in procs:%0A    p.join()
+total))%0Aprint %22=%22 * 30
 %0A

update benchmark script, better stats output