Created
May 27, 2014 13:05
-
-
Save wido/406c60951f464b355d30 to your computer and use it in GitHub Desktop.
Simple Python script to store RADOS read/write latency in Graphite
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
#!/usr/bin/env python | |
''' | |
This script writes X MegaByte random data to 10 RADOS objects and read the data again afterwards | |
It writes these statistics towards a Graphite server so you can see what the latency | |
inside your cluster is for both Read and Write | |
Author: Wido den Hollander <[email protected]> | |
''' | |
import argparse | |
import rados | |
import uuid | |
import time | |
import socket | |
def rados_bench(ceph_conf="/etc/ceph/ceph.conf", pool="data", object_size=1, num_objects=10): | |
try: | |
r = rados.Rados(conffile=ceph_conf) | |
r.conf_set("client_mount_timeout", "10"); | |
r.conf_set("rados_mon_op_timeout", "10"); | |
r.conf_set("rados_osd_op_timeout", "10"); | |
r.connect() | |
ioctx = r.open_ioctx(pool) | |
write_total_time = 0 | |
read_total_time = 0 | |
i = 0 | |
z = open('/dev/zero', 'r') | |
data = z.read(object_size * 1024 * 1024) | |
while i < num_objects: | |
obj = str(uuid.uuid4()) | |
start_write = time.clock() | |
ioctx.write_full(obj, data) | |
write_total_time += (time.clock() - start_write) * 1000 | |
start_read = time.clock() | |
ioctx.read(obj) | |
read_total_time += (time.clock() - start_read) * 1000 | |
ioctx.remove_object(obj) | |
i += 1 | |
ioctx.close() | |
return {'write_time': write_total_time / num_objects, 'read_time': read_total_time / num_objects} | |
except Exception as e: | |
raise | |
def collect_metric(host, port, name, value, timestamp): | |
sock = socket.create_connection((host, port)) | |
sock.send("%s %0.3f %d\n" % (name, value, timestamp)) | |
sock.close() | |
if __name__ == '__main__': | |
parser = argparse.ArgumentParser(description='Measure Ceph RADOS latency and write to Graphite') | |
parser.add_argument("--host", action="store", dest="graphite_host", default="localhost", help="The graphite host") | |
parser.add_argument("--port", action="store", dest="graphite_port", type=int, default=2005, help="The graphite port") | |
parser.add_argument("--prefix", action="store", dest="graphite_prefix", default="ceph", help="The prefix in graphite") | |
parser.add_argument("--size", action="store", dest="object_size", type=int, default=1, help="The object size in MegaBytes") | |
parser.add_argument("--num", action="store", dest="num_objects", type=int, default=10, help="The number of objects to write and read") | |
parser.add_argument("--pool", action="store", dest="rados_pool", default="data", help="The RADOS pool to write in") | |
parser.add_argument("--conf", action="store", dest="ceph_conf", default="/etc/ceph/ceph.conf", help="The ceph configuration file") | |
conf = parser.parse_args() | |
now = int(time.time()) | |
try: | |
stats = rados_bench(ceph_conf=conf.ceph_conf, pool=conf.rados_pool, object_size=conf.object_size, num_objects=conf.num_objects) | |
except Exception as e: | |
stats = {'write_time': 1000, 'read_time': 1000} | |
pass | |
try: | |
collect_metric(conf.graphite_host, conf.graphite_port, conf.graphite_prefix + ".rados.write_time", stats['write_time'], now) | |
collect_metric(conf.graphite_host, conf.graphite_port, conf.graphite_prefix + ".rados.read_time", stats['read_time'], now) | |
except Exception as e: | |
raise |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment