def test_get_value_with_non_existent_total(self): """`None` is returned for a non-existent total counter.""" args = (57, "h", "d/c/z", "t") key = stats.key_name(*args) kvs = self.connect() self.assertIs(None, kvs.get(key)) self.assertIs(None, stats.get_counter(*args))
def test_compute_uhs_task_pi(self): # Test that progress indicators are working properly for # `compute_uhs_task`. # Mock out the two 'heavy' functions called by this task; # we don't need to do these and we don't want to waste the cycles. cmpt_uhs = '%s.%s' % (self.UHS_CORE_MODULE, 'compute_uhs') write_uhs_data = '%s.%s' % (self.UHS_CORE_MODULE, 'write_uhs_spectrum_data') with helpers.patch(cmpt_uhs): with helpers.patch(write_uhs_data): get_counter = lambda: stats.get_counter( self.job_id, 'h', 'compute_uhs_task', 'i') # First, check that the counter for `compute_uhs_task` is # `None`: self.assertIsNone(get_counter()) realization = 0 site = Site(0.0, 0.0) # execute the task as a plain old function compute_uhs_task(self.job_id, realization, site) self.assertEqual(1, get_counter()) compute_uhs_task(self.job_id, realization, site) self.assertEqual(2, get_counter())
def test_compute_uhs_task_pi_failure_counter(self): # Same as the previous test, except that we want to make sure task # failure counters are properly incremented if a task fails. cmpt_uhs = '%s.%s' % (self.UHS_CORE_MODULE, 'compute_uhs') with helpers.patch(cmpt_uhs) as compute_mock: # We want to force a failure to occur in the task: compute_mock.side_effect = RuntimeError('Mock exception') get_counter = lambda: stats.get_counter( self.job_id, 'h', 'compute_uhs_task-failures', 'i') # The counter should start out empty: self.assertIsNone(get_counter()) # tasks_args: job_id, realization, site task_args = (self.job_id, 0, Site(0.0, 0.0)) self.assertRaises(RuntimeError, compute_uhs_task, *task_args) self.assertEqual(1, get_counter()) # Create two more failures: self.assertRaises(RuntimeError, compute_uhs_task, *task_args) self.assertRaises(RuntimeError, compute_uhs_task, *task_args) self.assertEqual(3, get_counter())
def test_get_value_with_existent_total(self): """The expected value is returned for an existent total counter.""" value = "582" args = (58, "h", "d/d/z", "t") key = stats.key_name(*args) kvs = self.connect() kvs.set(key, value) self.assertEqual(int(value), stats.get_counter(*args))
def completed_task_count(job_id): """Given the ID of a currently running calculation, query the stats counters in Redis to get the number of completed :function:`compute_uhs_task` task executions. Successful and failed executions are included in the count. :param int job_id: ID of the current job. :returns: Number of completed :function:`compute_uhs_task` task executions so far. """ success_count = stats.get_counter(job_id, 'h', 'compute_uhs_task', 'i') fail_count = stats.get_counter( job_id, 'h', 'compute_uhs_task-failures', 'i') return (success_count or 0) + (fail_count or 0)
def completed_task_count(job_id): """Given the ID of a currently running calculation, query the stats counters in Redis to get the number of completed :function:`compute_uhs_task` task executions. Successful and failed executions are included in the count. :param int job_id: ID of the current job. :returns: Number of completed :function:`compute_uhs_task` task executions so far. """ success_count = stats.get_counter(job_id, 'h', 'compute_uhs_task', 'i') fail_count = stats.get_counter(job_id, 'h', 'compute_uhs_task-failures', 'i') return (success_count or 0) + (fail_count or 0)
def test_get_value_with_debug_stats_enabled_but_no_value(self): """ `None` is returned for a debug counter if debug stats are enabled but the counter has no value. """ args = (61, "h", "d/g/z", "d") stats.delete_job_counters(args[0]) with helpers.patch("openquake.utils.stats.debug_stats_enabled") as dse: dse.return_value = True key = stats.key_name(*args) kvs = self.connect() self.assertIs(None, kvs.get(key)) self.assertIs(None, stats.get_counter(*args))
def test_get_value_with_debug_stats_enabled(self): """ The correct value is returned for a debug counter if debug stats are enabled. """ value = "603" args = (60, "h", "d/f/z", "d") with helpers.patch("openquake.utils.stats.debug_stats_enabled") as dse: dse.return_value = True key = stats.key_name(*args) kvs = self.connect() kvs.set(key, value) self.assertEqual(int(value), stats.get_counter(*args))
def test_initialize(self): # Test that `initialize` sets the task total counter with the correct # value # First, check that the total counter doesn't exist. task_total = lambda: stats.get_counter( self.job_id, 'h', 'uhs:tasks', 't') self.assertIsNone(task_total()) calc = UHSCalculator(self.job_ctxt) calc.initialize() # In this test file, there is only 1 realization and 1 site. # So, the expected total is 1. self.assertEqual(1, task_total())
def test_get_value_with_debug_stats_disabled(self): """`None` is returned for a debug counter if debug stats are off.""" args = (59, "h", "d/e/z", "d") with helpers.patch("openquake.utils.stats.debug_stats_enabled") as dse: dse.return_value = False self.assertIs(None, stats.get_counter(*args))