Skip to content

Instantly share code, notes, and snippets.

@fluential
Last active August 29, 2015 14:06
Show Gist options
  • Save fluential/d777f8c8c996c8e748a7 to your computer and use it in GitHub Desktop.
Save fluential/d777f8c8c996c8e748a7 to your computer and use it in GitHub Desktop.
Threaded carbon load tester
#!/usr/bin/env python
# The idea is to generate carbon load from multiple threads / connections to test load balanging feature of graphite / carbon cluster
import sys, os, time, thread, datetime
from socket import socket
from random import random, choice
try:
host = sys.argv[1]
port = int(sys.argv[2])
mpm = int(sys.argv[3])
threads = int(sys.argv[4])
tframe = int(sys.argv[5])
except:
print 'Usage: %s <host> <port> <metrics-per-timeframe> <threads> <time_frame>' % os.path.basename(sys.argv[0])
sys.exit(1)
def send_metrics(tname, tnum):
time.sleep(random() * 5 * tnum)
now = int( time.time() )
#now -= now % tframe
while True:
start = time.time()
s = socket()
s.connect( (host,port) )
count = 0
for i in xrange(0, mpm):
r = 42 + tnum
metric = 'TEST%d.%d' % (r,i)
value = random()
s.sendall('%s %s %s\n' % (metric, value, now))
count += 1
print '%s [%s]: sent %d metrics: <TEST%d> in %.5f seconds' % (datetime.datetime.now(), tname, count, r, time.time() - start)
s.close()
now += tframe
diff = now - time.time()
if diff > 0:
print "%s [%s]: sleeping for %.5f seconds" % (datetime.datetime.now(), tname, diff)
time.sleep(diff)
def print_time( threadName, delay):
count = 0
while count < 5:
time.sleep(delay)
count += 1
print "%s: %s" % ( threadName, time.ctime(time.time()) )
def main(args):
print 'Main here'
tcount = 0
while tcount <= threads:
thread.start_new_thread( send_metrics, ("Thread-%d" % tcount, tcount) )
tcount += 1
while True:
time.sleep(10)
pass
return 0
if __name__=='__main__':
sys.exit(main(sys.argv))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment