def testSendWhenWorkerIsActive(self, mock_send, mock_config): del mock_config # Unused. worker = mock.MagicMock() collector = client_stats.ClientStatsCollector(worker) worker.stats_collector = collector with test_lib.FakeTimeline(thread=collector) as timeline: worker.IsActive = lambda: True mock_send.reset_mock() timeline.Run(duration=rdfvalue.Duration.From(5, rdfvalue.SECONDS)) self.assertTrue(mock_send.called) mock_send.reset_mock() timeline.Run(duration=rdfvalue.Duration.From(2, rdfvalue.MINUTES)) self.assertTrue(mock_send.called) worker.IsActive = lambda: False mock_send.reset_mock() timeline.Run(duration=rdfvalue.Duration.From(30, rdfvalue.MINUTES)) self.assertFalse(mock_send.called) worker.IsActive = lambda: True mock_send.reset_mock() timeline.Run(duration=rdfvalue.Duration.From(5, rdfvalue.SECONDS)) self.assertTrue(mock_send.called)
def testSampleFiltering(self, mock_get_client_stats_auto): del mock_get_client_stats_auto # Unused. collector = client_stats.ClientStatsCollector(mock.MagicMock()) past = rdfvalue.RDFDatetime.FromHumanReadable("1980-01-01") with test_lib.FakeTimeline(thread=collector, now=past) as timeline: timeline.Run(duration=rdfvalue.Duration.From(30, rdfvalue.MINUTES)) cpu_samples = collector.CpuSamplesBetween( start_time=past + rdfvalue.Duration.From(10, rdfvalue.MINUTES) + rdfvalue.Duration.From(1, rdfvalue.SECONDS), end_time=past + rdfvalue.Duration.From(20, rdfvalue.MINUTES)) self.assertLen(cpu_samples, 60) for sample in cpu_samples: self.assertLess( past + rdfvalue.Duration.From(10, rdfvalue.MINUTES), sample.timestamp) self.assertGreaterEqual( past + rdfvalue.Duration.From(20, rdfvalue.MINUTES), sample.timestamp) io_samples = collector.IOSamplesBetween( start_time=past + rdfvalue.Duration.From(1, rdfvalue.MINUTES) + rdfvalue.Duration.From(1, rdfvalue.SECONDS), end_time=past + rdfvalue.Duration.From(2, rdfvalue.MINUTES)) self.assertLen(io_samples, 6) for sample in io_samples: self.assertLess(past + rdfvalue.Duration.From(1, rdfvalue.MINUTES), sample.timestamp) self.assertGreaterEqual( past + rdfvalue.Duration.From(2, rdfvalue.MINUTES), sample.timestamp)
def testSampleFiltering(self, STATS, GetClientStatsAuto): # pylint: disable=invalid-name del STATS # Unused. del GetClientStatsAuto # Unused. collector = client_stats.ClientStatsCollector(mock.MagicMock()) past = rdfvalue.RDFDatetime.FromHumanReadable("1980-01-01") with test_lib.FakeTimeline(thread=collector, now=past) as timeline: timeline.Run(duration=rdfvalue.Duration("30m")) cpu_samples = collector.CpuSamplesBetween( start_time=past + rdfvalue.Duration("10m") + rdfvalue.Duration("1s"), end_time=past + rdfvalue.Duration("20m")) self.assertEqual(len(cpu_samples), 60) for sample in cpu_samples: self.assertLess(past + rdfvalue.Duration("10m"), sample) self.assertGreaterEqual(past + rdfvalue.Duration("20m"), sample.timestamp) io_samples = collector.IOSamplesBetween( start_time=past + rdfvalue.Duration("1m") + rdfvalue.Duration("1s"), end_time=past + rdfvalue.Duration("2m")) self.assertEqual(len(io_samples), 6) for sample in io_samples: self.assertLess(past + rdfvalue.Duration("1m"), sample.timestamp) self.assertGreaterEqual(past + rdfvalue.Duration("2m"), sample.timestamp)
def testMinSendInterval(self, mock_send, mock_config): del mock_config # Unused. worker = mock.MagicMock() collector = client_stats.ClientStatsCollector(worker) worker.stats_collector = collector worker.IsActive = lambda: False with test_lib.FakeTimeline(thread=collector) as timeline: timeline.Run(duration=rdfvalue.Duration.From(15, rdfvalue.SECONDS)) self.assertTrue(mock_send.called) collector.RequestSend() mock_send.reset_mock() timeline.Run(duration=rdfvalue.Duration.From(10, rdfvalue.SECONDS)) self.assertFalse(mock_send.called) mock_send.reset_mock() timeline.Run(duration=rdfvalue.Duration.From(20, rdfvalue.SECONDS)) self.assertFalse(mock_send.called) mock_send.reset_mock() timeline.Run(duration=rdfvalue.Duration.From(40, rdfvalue.SECONDS)) self.assertTrue(mock_send.called) mock_send.reset_mock() timeline.Run(duration=rdfvalue.Duration.From(30, rdfvalue.SECONDS)) self.assertFalse(mock_send.called)
def testMinSendInterval(self, Send, CONFIG, STATS): # pylint: disable=invalid-name del CONFIG # Unused. del STATS # Unused. worker = mock.MagicMock() collector = client_stats.ClientStatsCollector(worker) worker.stats_collector = collector worker.IsActive = lambda: False with test_lib.FakeTimeline(thread=collector) as timeline: timeline.Run(duration=rdfvalue.Duration("15s")) self.assertTrue(Send.called) collector.RequestSend() Send.reset_mock() timeline.Run(duration=rdfvalue.Duration("10s")) self.assertFalse(Send.called) Send.reset_mock() timeline.Run(duration=rdfvalue.Duration("20s")) self.assertFalse(Send.called) Send.reset_mock() timeline.Run(duration=rdfvalue.Duration("40s")) self.assertTrue(Send.called) Send.reset_mock() timeline.Run(duration=rdfvalue.Duration("30s")) self.assertFalse(Send.called)
def testSendWhenWorkerIsActive(self, Send, CONFIG, STATS): # pylint: disable=invalid-name del CONFIG # Unused. del STATS # Unused. worker = mock.MagicMock() collector = client_stats.ClientStatsCollector(worker) worker.stats_collector = collector with test_lib.FakeTimeline(thread=collector) as timeline: worker.IsActive = lambda: True Send.reset_mock() timeline.Run(duration=rdfvalue.Duration("5s")) self.assertTrue(Send.called) Send.reset_mock() timeline.Run(duration=rdfvalue.Duration("2m")) self.assertTrue(Send.called) worker.IsActive = lambda: False Send.reset_mock() timeline.Run(duration=rdfvalue.Duration("30m")) self.assertFalse(Send.called) worker.IsActive = lambda: True Send.reset_mock() timeline.Run(duration=rdfvalue.Duration("5s")) self.assertTrue(Send.called)
def testSampleSending(self, mock_send, mock_config): del mock_config # Unused. with MockCpuTimes(), MockIoCounters(), MockCpuPercent(): worker = mock.MagicMock() collector = client_stats.ClientStatsCollector(worker) worker.stats_collector = collector worker.IsActive = lambda: False today = rdfvalue.RDFDatetime.FromHumanReadable("2018-03-14") with test_lib.FakeTimeline(thread=collector, now=today) as timeline: timeline.Run(duration=rdfvalue.Duration.From(10, rdfvalue.SECONDS)) self.assertTrue(mock_send.called) response = mock_send.call_args[0][0] self.assertTrue(response.HasField("cpu_samples")) self.assertTrue(response.HasField("io_samples")) self.assertLen(response.cpu_samples, 1) self.assertLen(response.io_samples, 1) self.assertEqual(response.cpu_samples[0].timestamp, today) self.assertEqual(response.cpu_samples[0].user_cpu_time, FAKE_CPU_TIMES[0].user) self.assertEqual(response.cpu_samples[0].system_cpu_time, FAKE_CPU_TIMES[0].system) self.assertEqual(response.cpu_samples[0].cpu_percent, FAKE_CPU_PERCENT[0]) self.assertEqual(response.cpu_samples[0].timestamp, today) self.assertEqual(response.io_samples[0].read_bytes, FAKE_IO_COUNTERS[0].read_bytes) self.assertEqual(response.io_samples[0].write_bytes, FAKE_IO_COUNTERS[0].write_bytes)
def testFakeSamples(self, mock_get_client_stats_auto): del mock_get_client_stats_auto # Unused. with MockCpuTimes(), MockIoCounters(), MockCpuPercent(): collector = client_stats.ClientStatsCollector(mock.MagicMock()) millennium = rdfvalue.RDFDatetime.FromHumanReadable("2000-01-01") with test_lib.FakeTimeline(thread=collector, now=millennium) as timeline: timeline.Run(duration=rdfvalue.Duration.From(25, rdfvalue.SECONDS)) cpu_samples = collector.CpuSamplesBetween( start_time=millennium, end_time=millennium + rdfvalue.Duration.From(25, rdfvalue.SECONDS)) io_samples = collector.IOSamplesBetween( start_time=millennium, end_time=millennium + rdfvalue.Duration.From(25, rdfvalue.SECONDS)) self.assertLen(cpu_samples, 3) self.assertLen(io_samples, 3) for i in range(3): expected_timestamp = millennium + rdfvalue.Duration.From( 10, rdfvalue.SECONDS) * i cpu_sample = cpu_samples[i] self.assertEqual(cpu_sample.timestamp, expected_timestamp) self.assertEqual(cpu_sample.user_cpu_time, FAKE_CPU_TIMES[i].user) self.assertEqual(cpu_sample.system_cpu_time, FAKE_CPU_TIMES[i].system) self.assertEqual(cpu_sample.cpu_percent, FAKE_CPU_PERCENT[i]) io_sample = io_samples[i] self.assertEqual(io_sample.timestamp, expected_timestamp) self.assertEqual(io_sample.read_bytes, FAKE_IO_COUNTERS[i].read_bytes) self.assertEqual(io_sample.write_bytes, FAKE_IO_COUNTERS[i].write_bytes)
def testRealSamples(self, mock_get_client_stats_auto): del mock_get_client_stats_auto # Unused. worker = mock.MagicMock() collector = client_stats.ClientStatsCollector(worker) future = rdfvalue.RDFDatetime.FromHumanReadable("2033-09-01") with test_lib.FakeTimeline(thread=collector, now=future) as timeline: timeline.Run(duration=rdfvalue.Duration("25s")) cpu_samples = collector.CpuSamplesBetween( start_time=future, end_time=future + rdfvalue.Duration("25s")) io_samples = collector.IOSamplesBetween( start_time=future, end_time=future + rdfvalue.Duration("25s")) self.assertLen(cpu_samples, 3) self.assertLen(io_samples, 3)
def testOldSampleCleanup(self, mock_get_client_stats_auto): del mock_get_client_stats_auto # Unused. collector = client_stats.ClientStatsCollector(mock.MagicMock()) epoch = rdfvalue.RDFDatetime.FromSecondsSinceEpoch(0) with test_lib.FakeTimeline(thread=collector, now=epoch) as timeline: timeline.Run(duration=rdfvalue.Duration("3h")) cpu_samples = collector.CpuSamplesBetween( start_time=epoch, end_time=epoch + rdfvalue.Duration("1h")) self.assertEmpty(cpu_samples) io_samples = collector.IOSamplesBetween( start_time=epoch + rdfvalue.Duration("30m"), end_time=epoch + rdfvalue.Duration("1h") + rdfvalue.Duration("50m")) self.assertEmpty(io_samples)
def testRealSamples(self, STATS, GetClientStatsAuto): # pylint: disable=invalid-name del STATS # Unused. del GetClientStatsAuto # Unused. worker = mock.MagicMock() collector = client_stats.ClientStatsCollector(worker) future = rdfvalue.RDFDatetime.FromHumanReadable("2033-09-01") with test_lib.FakeTimeline(thread=collector, now=future) as timeline: timeline.Run(duration=rdfvalue.Duration("25s")) cpu_samples = collector.CpuSamplesBetween( start_time=future, end_time=future + rdfvalue.Duration("25s")) io_samples = collector.IOSamplesBetween( start_time=future, end_time=future + rdfvalue.Duration("25s")) self.assertEqual(len(cpu_samples), 3) self.assertEqual(len(io_samples), 3)
def testOldSampleCleanup(self, STATS, GetClientStatsAuto): # pylint: disable=invalid-name del STATS # Unused. del GetClientStatsAuto # Unused. collector = client_stats.ClientStatsCollector(mock.MagicMock()) epoch = rdfvalue.RDFDatetime.FromSecondsSinceEpoch(0) with test_lib.FakeTimeline(thread=collector, now=epoch) as timeline: timeline.Run(duration=rdfvalue.Duration("3h")) cpu_samples = collector.CpuSamplesBetween( start_time=epoch, end_time=epoch + rdfvalue.Duration("1h")) self.assertEqual(len(cpu_samples), 0) io_samples = collector.IOSamplesBetween( start_time=epoch + rdfvalue.Duration("30m"), end_time=epoch + rdfvalue.Duration("1h") + rdfvalue.Duration("50m")) self.assertEqual(len(io_samples), 0)
def __init__(self, *args, **kwargs): super(DisabledNannyClientWorker, self).__init__(*args, **kwargs) self.stats_collector = client_stats.ClientStatsCollector(self)
def __init__(self, *args, **kw): super(FakeMixin, self).__init__(*args, **kw) self.responses = [] self.sent_bytes_per_flow = {} self.lock = threading.RLock() self.stats_collector = client_stats.ClientStatsCollector(self)
def StartStatsCollector(self): if not GRRClientWorker.stats_collector: GRRClientWorker.stats_collector = client_stats.ClientStatsCollector(self) GRRClientWorker.stats_collector.start()
def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.stats_collector = client_stats.ClientStatsCollector(self)