# Path: pyemc/abstractions/task_api.py
# class Task(object):
#     '''Represents a task run against the server, which internally delegates its
#     work to Tasklets run in threads.
#     
#     Users need to implement a Task to do any setup/teardown of the task and
#     define the creation of Tasklets.'''
# 
#     def __init__(self, client_params):
#         self.client_params = client_params
# 
#     def create_tasklets(self):
#         raise NotImplementedError
# 
#     def pre_tasklets(self):
#         raise NotImplementedError
# 
#     def run_tasklets(self, tasklets):
#         metrics_list = []
#         threads = []
# 
#         def handle_signal(signal, frame):
#             write("Got interrupt, stopping all tasklets")
#             for tasklet in tasklets:
#                 tasklet._runnable = False
# 
#         signal.signal(signal.SIGINT, handle_signal)
# 
#         for tasklet in tasklets:
#             thread = threading.Thread(target=tasklet.launch)
#             thread.daemon = True
#             metrics_list.append(tasklet._metrics)
#             threads.append(thread)
#             thread.start()
# 
#         while threads:
#             for thread in threads:
#                 thread.join(.05)
#                 if not thread.isAlive():
#                     threads.remove(thread)
# 
#         return metrics_list
# 
#     def post_tasklets(self):
#         raise NotImplementedError
# 
#     def launch(self):
#         client = self.client_params.create_client()
#         state = TaskState()
# 
#         tasklets = self.create_tasklets(state)
#         self.pre_tasklets(client, state)
#         metrics_list = self.run_tasklets(tasklets)
#         self.post_tasklets(client, state, metrics_list)
# 
#     def write(self, msg):
#         write(msg)
# 
# Path: pyemc/abstractions/task_api.py
# class Tasklet(object):
#     '''A Tasklet represents a unit of work run in a separate thread.'''
# 
#     def __init__(self, id, client_params):
#         self.id = id
#         self.client_params = client_params
# 
#         self._metrics = TaskletMetrics()
#         self._runnable = True
# 
#     def launch(self):
#         client = self.client_params.create_client()
#         self.run(client, self._metrics)
# 
#     def run(self, client):
#         raise NotImplementedError
# 
#     def write(self, msg):
#         msg = '[thread%s] %s' % (self.id, msg)
#         write(msg)
# 
# Path: pyemc/util.py
# def generate_random_data(length_from, length_to=None):
#     length = length_from
#     if length_to is not None:
#         length = random.randint(length_from, length_to)
# 
#     return os.urandom(length)
# 
# Path: pyemc/util.py
# def generate_random_data_prng(length_from, length_to=None):
#     length = length_from
#     if length_to is not None:
#         length = random.randint(length_from, length_to)
# 
#     def get(length):
#         for _ in xrange(length):
#             yield random.getrandbits(8)
# 
#     num = bytearray(get(length))
#     return num
# 
# Path: pyemc/util.py
# def generate_random_key(length):
#     data = ''
#     while len(data) < length:
#         bytes = generate_random_data(length)
#         # filter out non printable chars
#         bytes = [b for b in bytes
#                  if 65 <= ord(b) <= 90 or 97 <= ord(b) <= 122]
#         bytes = ''.join(bytes)
#         data += bytes
#     data = data[:length]
#     return data
# 
# Path: pyemc/util.py
# def generate_random_key_uuid(length_from, length_to=None):
#     length = length_from
#     if length_to is not None:
#         length = random.randint(length_from, length_to)
# 
#     # XXX length will never be >32
#     return uuid.uuid4().hex[:length]
# 
# Path: pyemc/util.py
# def insert_number_commas(number):
#     chunks = []
# 
#     num_iterations = (len(number) / 3) + 1
#     idx = -3
# 
#     for i in range(1, num_iterations + 1):
#         chunk = number[idx:]
#         number = number[:idx]
#         chunks.append(chunk)
#         if i != num_iterations:
#             chunks.append(',')
# 
#     chunks.reverse()
#     number = ''.join(chunks)
#     if number.startswith(','):
#         number = number[1:]
# 
#     return number


from itertools import izip
from pyemc.abstractions.task_api import Task
from pyemc.abstractions.task_api import Tasklet
from pyemc.util import generate_random_data
from pyemc.util import generate_random_data_prng
from pyemc.util import generate_random_key
from pyemc.util import generate_random_key_uuid
from pyemc.util import insert_number_commas
import time
        self.percentage = percentage

    def get_pct_full(self, client):
        stats = client.get_stats()
        capacity = stats['limit_maxbytes']
        bytes = stats['bytes']
        pct_full = 100 * float(bytes) / float(capacity)
        return pct_full

    def run(self, client, metrics):
        # use client.set + client.flush_pipeline pattern
        client.pipeline_mode = True

        stats = client.get_stats()
        capacity = stats['limit_maxbytes']
        capacity_fmt = insert_number_commas(capacity)

        metrics.pct_full = self.get_pct_full(client)
        metrics.batch_size = 100
        metrics.bytes_cum = 0
        metrics.time_cum = 0
        metrics.time_total_cum = 0
        metrics.items_cum = 0
        rate = -1

        time_total_st = time.time()

        while metrics.pct_full < self.percentage:
            self.write("Cache is %.2f%% full of %s, inserting %s items (rate: %s items/s)" %
                       (metrics.pct_full, capacity_fmt, metrics.batch_size,

# Path: pyemc/abstractions/task_api.py
# class Task(object):
#     '''Represents a task run against the server, which internally delegates its
#     work to Tasklets run in threads.
#     
#     Users need to implement a Task to do any setup/teardown of the task and
#     define the creation of Tasklets.'''
# 
#     def __init__(self, client_params):
#         self.client_params = client_params
# 
#     def create_tasklets(self):
#         raise NotImplementedError
# 
#     def pre_tasklets(self):
#         raise NotImplementedError
# 
#     def run_tasklets(self, tasklets):
#         metrics_list = []
#         threads = []
# 
#         def handle_signal(signal, frame):
#             write("Got interrupt, stopping all tasklets")
#             for tasklet in tasklets:
#                 tasklet._runnable = False
# 
#         signal.signal(signal.SIGINT, handle_signal)
# 
#         for tasklet in tasklets:
#             thread = threading.Thread(target=tasklet.launch)
#             thread.daemon = True
#             metrics_list.append(tasklet._metrics)
#             threads.append(thread)
#             thread.start()
# 
#         while threads:
#             for thread in threads:
#                 thread.join(.05)
#                 if not thread.isAlive():
#                     threads.remove(thread)
# 
#         return metrics_list
# 
#     def post_tasklets(self):
#         raise NotImplementedError
# 
#     def launch(self):
#         client = self.client_params.create_client()
#         state = TaskState()
# 
#         tasklets = self.create_tasklets(state)
#         self.pre_tasklets(client, state)
#         metrics_list = self.run_tasklets(tasklets)
#         self.post_tasklets(client, state, metrics_list)
# 
#     def write(self, msg):
#         write(msg)
# 
# Path: pyemc/abstractions/task_api.py
# class Tasklet(object):
#     '''A Tasklet represents a unit of work run in a separate thread.'''
# 
#     def __init__(self, id, client_params):
#         self.id = id
#         self.client_params = client_params
# 
#         self._metrics = TaskletMetrics()
#         self._runnable = True
# 
#     def launch(self):
#         client = self.client_params.create_client()
#         self.run(client, self._metrics)
# 
#     def run(self, client):
#         raise NotImplementedError
# 
#     def write(self, msg):
#         msg = '[thread%s] %s' % (self.id, msg)
#         write(msg)
# 
# Path: pyemc/util.py
# def generate_random_data(length_from, length_to=None):
#     length = length_from
#     if length_to is not None:
#         length = random.randint(length_from, length_to)
# 
#     return os.urandom(length)
# 
# Path: pyemc/util.py
# def generate_random_data_prng(length_from, length_to=None):
#     length = length_from
#     if length_to is not None:
#         length = random.randint(length_from, length_to)
# 
#     def get(length):
#         for _ in xrange(length):
#             yield random.getrandbits(8)
# 
#     num = bytearray(get(length))
#     return num
# 
# Path: pyemc/util.py
# def generate_random_key(length):
#     data = ''
#     while len(data) < length:
#         bytes = generate_random_data(length)
#         # filter out non printable chars
#         bytes = [b for b in bytes
#                  if 65 <= ord(b) <= 90 or 97 <= ord(b) <= 122]
#         bytes = ''.join(bytes)
#         data += bytes
#     data = data[:length]
#     return data
# 
# Path: pyemc/util.py
# def generate_random_key_uuid(length_from, length_to=None):
#     length = length_from
#     if length_to is not None:
#         length = random.randint(length_from, length_to)
# 
#     # XXX length will never be >32
#     return uuid.uuid4().hex[:length]
# 
# Path: pyemc/util.py
# def insert_number_commas(number):
#     chunks = []
# 
#     num_iterations = (len(number) / 3) + 1
#     idx = -3
# 
#     for i in range(1, num_iterations + 1):
#         chunk = number[idx:]
#         number = number[:idx]
#         chunks.append(chunk)
#         if i != num_iterations:
#             chunks.append(',')
# 
#     chunks.reverse()
#     number = ''.join(chunks)
#     if number.startswith(','):
#         number = number[1:]
# 
#     return number


# Path: pyemc/task_filler.py
from itertools import izip
from pyemc.abstractions.task_api import Task
from pyemc.abstractions.task_api import Tasklet
from pyemc.util import generate_random_data
from pyemc.util import generate_random_data_prng
from pyemc.util import generate_random_key
from pyemc.util import generate_random_key_uuid
from pyemc.util import insert_number_commas
import time

        self.percentage = percentage

    def get_pct_full(self, client):
        stats = client.get_stats()
        capacity = stats['limit_maxbytes']
        bytes = stats['bytes']
        pct_full = 100 * float(bytes) / float(capacity)
        return pct_full

    def run(self, client, metrics):
        # use client.set + client.flush_pipeline pattern
        client.pipeline_mode = True

        stats = client.get_stats()
        capacity = stats['limit_maxbytes']
        capacity_fmt = insert_number_commas(capacity)

        metrics.pct_full = self.get_pct_full(client)
        metrics.batch_size = 100
        metrics.bytes_cum = 0
        metrics.time_cum = 0
        metrics.time_total_cum = 0
        metrics.items_cum = 0
        rate = -1

        time_total_st = time.time()

        while metrics.pct_full < self.percentage:
            self.write("Cache is %.2f%% full of %s, inserting %s items (rate: %s items/s)" %
                       (metrics.pct_full, capacity_fmt, metrics.batch_size,

                        insert_number_commas(str(int(rate)))))