def test_create_sample_set_populates_hash_with_parameters_provided(self): mock_time = MockTime() guesstimator = Guesstimator(redis_host='localhost', time=mock_time, environment='test') guesstimator.create_sample_set( name='foobar-sample', recording_frequency=0.05 ) self.assertEqual( '0.05', self.redis.hget( guesstimator._get_hash_key('foobar-sample'), 'recording_frequency' ) ) self.assertEqual( '0', self.redis.hget( guesstimator._get_hash_key('foobar-sample'), 'samples_taken' ) ) self.assertEqual( '%s' % mock_time.time, self.redis.hget( guesstimator._get_hash_key('foobar-sample'), 'sample_start_time' ) )
""" This example demonstrates what a reader that consumes guesstimator's data might look like. in the wild this might be a Ganglia or Munin plugin. """ import time from guesstimator import Guesstimator from redis import Redis guesstimator = Guesstimator(environment='example') sample_set_name = 'count-operations' redis = Redis('localhost') # Initialize a sample set, this should only be done once. if not sample_set_name in guesstimator.list_sample_sets(): guesstimator.create_sample_set( name=sample_set_name ) while True: time.sleep(10) operations = float( redis.get('guesstimator_example_actual_operation_count') ) timestamp, estimated_operations = guesstimator.read(sample_set_name) print "Metrics:\n\toperation count: \t\033[32m%s\033[m\n\testimated count:\t\033[31m%s\033[m\n\n\tOperations per-second:\t%s\n---------" % ( operations, estimated_operations, ( operations / (time.time() - timestamp) ) ) guesstimator.reset(sample_set_name)
def test_read_returns_a_timestamp_and_an_estimation_of_the_number_of_samples_taken(self): mock_time = MockTime() guesstimator = Guesstimator(redis_host='localhost', random=MockRandom(), time=mock_time, environment='test') guesstimator.create_sample_set( name='50-percent-sample', recording_frequency=0.5 ) guesstimator.record('50-percent-sample') guesstimator.record('50-percent-sample') guesstimator.record('50-percent-sample') guesstimator.record('50-percent-sample') timestamp, samples_taken = guesstimator.read('50-percent-sample') self.assertEqual(4, samples_taken) self.assertEqual(mock_time.time, timestamp) guesstimator.create_sample_set( name='25-percent-sample', recording_frequency=0.25 ) guesstimator.record('25-percent-sample') guesstimator.record('25-percent-sample') guesstimator.record('25-percent-sample') guesstimator.record('25-percent-sample') guesstimator.record('25-percent-sample') guesstimator.record('25-percent-sample') guesstimator.record('25-percent-sample') timestamp, samples_taken = guesstimator.read('25-percent-sample') self.assertEqual(8, samples_taken)
def test_record_increments_the_number_of_samples_taken_with_the_appropriate_frequency(self): guesstimator = Guesstimator(redis_host='localhost', random=MockRandom(), environment='test') guesstimator.create_sample_set( name='100-percent-sample', recording_frequency=1.0 ) guesstimator.record('100-percent-sample') guesstimator.record('100-percent-sample') self.assertEqual( '2', self.redis.hget( guesstimator._get_hash_key('100-percent-sample'), 'samples_taken' ) ) guesstimator.create_sample_set( name='50-percent-sample', recording_frequency=0.5 ) guesstimator.record('50-percent-sample') guesstimator.record('50-percent-sample') guesstimator.record('50-percent-sample') guesstimator.record('50-percent-sample') self.assertEqual( '3', self.redis.hget( guesstimator._get_hash_key('50-percent-sample'), 'samples_taken' ) ) guesstimator.create_sample_set( name='25-percent-sample', recording_frequency=0.25 ) guesstimator.record('25-percent-sample') guesstimator.record('25-percent-sample') guesstimator.record('25-percent-sample') guesstimator.record('25-percent-sample') guesstimator.record('25-percent-sample') guesstimator.record('25-percent-sample') guesstimator.record('25-percent-sample') self.assertEqual( '2', self.redis.hget( guesstimator._get_hash_key('25-percent-sample'), 'samples_taken' ) )
def test_create_sample_set_creates_populates_sample_set_list(self): guesstimator = Guesstimator(redis_host='localhost', environment='test') self.assertEqual( self.redis.type( guesstimator._sample_set_list_key ), 'none') guesstimator.create_sample_set('foobar-sample') self.assertTrue( 'foobar-sample' in guesstimator.list_sample_sets() )
def test_list_sample_set_lists_all_sample_sets_currently_being_written_to(self): guesstimator = Guesstimator(redis_host='localhost', environment='test') self.redis.lpush( guesstimator._sample_set_list_key, 'user-message-queue' ) self.redis.lpush( guesstimator._sample_set_list_key, 'user-background-jobs' ) self.assertTrue( 'user-background-jobs' in guesstimator.list_sample_sets() ) self.assertTrue( 'user-message-queue' in guesstimator.list_sample_sets() )
def _delete_guesstimator_data(self): guesstimator = Guesstimator(redis_host='localhost', environment='test') self.redis.delete( guesstimator._sample_set_list_key ) for sample_set_name in guesstimator.list_sample_sets(): self.redis.delete(guesstimator._get_hash_key(sample_set_name))
def test_reset_resets_timestamp_and_samples_taken(self): mock_time = MockTime() new_time = 1345576088.71 guesstimator = Guesstimator(redis_host='localhost', random=MockRandom(), time=mock_time, environment='test') guesstimator.create_sample_set( name='100-percent-sample', recording_frequency=1.0 ) guesstimator.record('100-percent-sample') guesstimator.record('100-percent-sample') timestamp, samples_taken = guesstimator.read('100-percent-sample') self.assertEqual( '2', self.redis.hget( guesstimator._get_hash_key('100-percent-sample'), 'samples_taken' ) ) self.assertFalse( timestamp == new_time ) mock_time.time = new_time guesstimator.reset('100-percent-sample') timestamp, samples_taken = guesstimator.read('100-percent-sample') self.assertEqual( '0', self.redis.hget( guesstimator._get_hash_key('100-percent-sample'), 'samples_taken' ) ) self.assertEqual( new_time, timestamp )
""" This exmaple demonstrates how guesstimator might be used by a worker in a distributed system. guesstimator.record() is called 20 times more frequently than a write to Redis actually occurs (this is the default configuration). """ from guesstimator import Guesstimator from redis import Redis guesstimator = Guesstimator(environment="example") sample_set_name = "count-operations" redis = Redis("localhost") last_operations_count = 0 # Initialize a sample set, this should only be done once. if not sample_set_name in guesstimator.list_sample_sets(): guesstimator.create_sample_set(name=sample_set_name) while True: # Record that an operation has occurred. guesstimator.record(sample_set_name) # our operation happens to be an increment in Redis, it could be anything. # this is a convenient operation because it lets us keep track of the # true number of total operations across multiple workers. operations = redis.incr("guesstimator_example_actual_operation_count") # reader.py must have reset the operation count. if last_operations_count > operations: guesstimator.writes_performed = 0 last_operations_count = operations