Skip to content

Instantly share code, notes, and snippets.

@mleinart
Created August 20, 2013 16:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mleinart/6283958 to your computer and use it in GitHub Desktop.
Save mleinart/6283958 to your computer and use it in GitHub Desktop.
Script to fill a Graphite Whisper data file with random data for testing purposes
#!/usr/bin/env python
import os
import time
import whisper
import sys
from optparse import OptionParser
from random import Random
SPARSE_SPACING = 5
option_parser = OptionParser(
usage='''%prog [options] <file>
%prog writes random points to a whisper file for testing purposes''')
option_parser.add_option(
'--from', default=None, dest='_from',
type='string', help='Begin writing points at this time')
option_parser.add_option(
'--until', default=None,
type='string', help='Write points until this time')
option_parser.add_option(
'--sparse', default=False,
action='store_true', help='Write sparsely (resulting in None values)')
option_parser.add_option(
'--mean', default=100,
type='float', help='Mean of generated values (default: 100)')
option_parser.add_option(
'--deviation', default=50,
type='float', help='Standard deviation of generated values (default: 50)')
option_parser.add_option(
'--integers', default=False,
action='store_true', help='Use only integers as values')
(options, args) = option_parser.parse_args()
if len(args) < 1:
option_parser.print_usage()
sys.exit(1)
path = args[0]
if not os.path.exists(path):
sys.stderr.write("[ERROR] File '%s' does not exist!\n\n" % path)
option_parser.print_usage()
sys.exit(1)
def generate_points(from_time, until_time, step):
rand = Random()
for ts in xrange(from_time, until_time, step):
new_value = rand.normalvariate(options.mean, options.deviation)
if options.integers:
new_value = int(new_value)
if options.sparse:
new_value = rand.choice( [new_value] * (SPARSE_SPACING - 1) + [None] )
if new_value is not None:
yield (ts, new_value)
file_info = whisper.info(path)
now = int(time.time())
from_time = now - file_info['maxRetention']
until_time = now
if options._from is not None:
if options._from > from_time:
from_time = options._from
if options.until is not None:
if options.until < now:
until_time = options.until
whisper.update_many(path, generate_points(from_time, until_time, file_info['archives'][0]['secondsPerPoint']))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment