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 )
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' ) )
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 if (operations % 10000) == 0: # Read the timestamp since metrics started being tracked. # and the estimated number of samples taken.