Created
June 20, 2014 10:47
-
-
Save AndreaCrotti/78e1aed6503f05ab908b to your computer and use it in GitHub Desktop.
This file contains 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 argparse | |
import multiprocessing | |
import threading | |
from pylibmc.test import make_test_client | |
import random | |
NUM_THREADS = 4 | |
NUM_PROCS = 4 | |
NUM_SETS = 1000 | |
def inner_set_values(client): | |
key, val = str(random.randrange(1, 10)), str(random.randrange(1, 10)) | |
for i in range(NUM_SETS): | |
client.set(key, val) | |
client.get(key) | |
def set_values(client): | |
threads = [] | |
for _ in range(NUM_THREADS): | |
th = threading.Thread(target=inner_set_values, args=(client, )) | |
th.daemon = False | |
th.start() | |
threads.append(th) | |
for t in threads: | |
t.join() | |
def parse_arguments(): | |
parser = argparse.ArgumentParser(description='Stress test pylibmc set') | |
parser.add_argument('-p', '--procs', default=NUM_PROCS, type=int) | |
parser.add_argument('-t', '--threads', default=NUM_THREADS, type=int) | |
parser.add_argument('-s', '--sets', default=NUM_SETS, type=int) | |
return parser.parse_args() | |
def main(): | |
ns = parse_arguments() | |
global NUM_PROCS, NUM_SETS, NUM_THREADS | |
NUM_PROCS = ns.procs | |
NUM_THREADS = ns.threads | |
NUM_SETS = ns.sets | |
client = make_test_client(binary=True) | |
procs = [multiprocessing.Process(target=set_values, args=(client, )) for _ in range(NUM_PROCS)] | |
for proc in procs: | |
proc.start() | |
for proc in procs: | |
proc.join() | |
client.disconnect_all() | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
+1. Separating pylibmc client by pid fixed the problem.