def setUp(self): conn = DjangoBrokerConnection() consumer = StatsConsumer(connection=conn) consumer.discard_all() conn.close() consumer.close() self.s = StatsCollector() self.assertEquals(self.s.total_tasks_processed, 0) self.assertEquals(self.s.total_tasks_processed_by_type, {}) self.assertEquals(self.s.total_task_time_running, 0.0) self.assertEquals(self.s.total_task_time_running_by_type, {})
class TestStatsCollector(unittest.TestCase): def setUp(self): conn = DjangoBrokerConnection() consumer = StatsConsumer(connection=conn) consumer.discard_all() conn.close() consumer.close() self.s = StatsCollector() self.assertEquals(self.s.total_tasks_processed, 0) self.assertEquals(self.s.total_tasks_processed_by_type, {}) self.assertEquals(self.s.total_task_time_running, 0.0) self.assertEquals(self.s.total_task_time_running_by_type, {}) def test_collect_report_dump(self): timer1 = TaskTimerStats() timer1.enabled = True timer1.run("foo", "bar", [], {}) timer2 = TaskTimerStats() timer2.enabled = True timer2.run("foo", "bar", [], {}) timer3 = TaskTimerStats() timer3.enabled = True timer3.run("foo", "bar", [], {}) for timer in (timer1, timer2, timer3): timer.stop() # Collect self.s.collect() self.assertEquals(self.s.total_tasks_processed, 3) # Report with OverrideStdout() as outs: stdout, stderr = outs self.s.report() self.assertTrue( "Total processing time by task type:" in stdout.getvalue()) # Dump to cache self.s.dump_to_cache()
def handle(self, *args, **options): """Handle the management command.""" stats = StatsCollector() print("* Gathering statistics...") stats.collect() stats.report()