Esempio n. 1
0
 def test_metrics_is_ok_when_cpu_freq_returns_none(self):
     # Some psutil functions, such as cpu_freq(), can return None depending on
     # the environment; make sure we don't crash when that occurs.
     with mock.patch.object(resources.psutil, 'cpu_freq',
                            return_value=None):
         with resources.ResourceMonitor() as monitor:
             self.assertEqual(monitor.metrics().cpu_frequency_mhz, 0)
Esempio n. 2
0
 def test_metrics_is_ok_when_cpu_freq_throws(self):
   # psutil.cpu_freq() can throw NotImplementedError in certain environments;
   # make sure we don't crash when that occurs.
   with mock.patch.object(
       resources.psutil, 'cpu_freq', side_effect=NotImplementedError):
     with resources.ResourceMonitor() as monitor:
       self.assertEqual(monitor.metrics().cpu_frequency_mhz, 0)
Esempio n. 3
0
 def test_metrics_is_ok_if_rusage_fails(self):
   # Some psutil functions, such as cpu_freq(), can return None depending on
   # the environment; make sure we don't crash when that occurs.
   with mock.patch.object(
       resources.resource, 'getrusage', side_effect=resource.error):
     with resources.ResourceMonitor() as monitor:
       self.assertEqual(monitor.metrics().cpu_user_time_seconds, 0)
       self.assertEqual(monitor.metrics().cpu_system_time_seconds, 0)
       self.assertEqual(monitor.metrics().memory_peak_rss_mb, 0)
Esempio n. 4
0
def make_examples_runner(options):
    """Runs examples creation stage of deepvariant."""
    resource_monitor = resources.ResourceMonitor().start()
    logging.info('Preparing inputs')
    regions = processing_regions_from_options(options)

    # Create a processor to create candidates and examples for each region.
    region_processor = RegionProcessor(options)

    logging.info('Writing examples to %s', options.examples_filename)
    if options.candidates_filename:
        logging.info('Writing candidates to %s', options.candidates_filename)
    if options.gvcf_filename:
        logging.info('Writing gvcf records to %s', options.gvcf_filename)

    n_regions, n_candidates, n_examples = 0, 0, 0
    with OutputsWriter(options) as writer:
        for region in regions:
            candidates, examples, gvcfs = region_processor.process(region)
            n_candidates += len(candidates)
            n_examples += len(examples)
            n_regions += 1

            writer.write_candidates(*candidates)

            # If we have any gvcf records, write them out. This if also serves to
            # protect us from trying to write to the gvcfs output of writer when gvcf
            # generation is turned off. In that case, gvcfs will always be empty and
            # we'll never execute the write.
            if gvcfs:
                writer.write_gvcfs(*gvcfs)
            writer.write_examples(*examples)

    # Construct and then write out our MakeExamplesRunInfo proto.
    if options.run_info_filename:
        run_info = deepvariant_pb2.MakeExamplesRunInfo(
            options=options, resource_metrics=resource_monitor.metrics())
        if in_training_mode(options):
            if region_processor.labeler.metrics is not None:
                run_info.labeling_metrics.CopyFrom(
                    region_processor.labeler.metrics)
            else:
                logging.warning(
                    'Labeling metrics requested but the selected labeling '
                    'algorithm %s does not collect metrics; skipping.',
                    options.labeler_algorithm)
        logging.info('Writing MakeExamplesRunInfo to %s',
                     options.run_info_filename)
        write_make_examples_run_info(run_info, path=options.run_info_filename)

    logging.info('Found %s candidate variants', n_candidates)
    logging.info('Created %s examples', n_examples)
Esempio n. 5
0
    def test_monitor_gets_expected_metric_values(self):
        patchers = [
            mock.patch.object(resources,
                              '_get_host_name',
                              return_value='hostname'),
            mock.patch.object(resources, '_get_cpu_count', return_value=3),
            mock.patch.object(resources,
                              '_get_cpu_frequency',
                              return_value=2312.4),
            mock.patch.object(resources,
                              '_get_total_memory',
                              return_value=1234),
            mock.patch.object(
                resources.resource,
                'getrusage',
                return_value=mock.Mock(
                    ru_utime=1.0,
                    ru_stime=2.0,
                    ru_maxrss=3 * 1024  # 3 Megabytes
                )),
        ]

        process_mock = mock.Mock()
        process_mock.io_counters.return_value = mock.Mock(read_bytes=12,
                                                          write_bytes=34)
        patchers.append(
            mock.patch.object(resources.psutil,
                              'Process',
                              return_value=process_mock))

        with contextlib.nested(*patchers):
            with resources.ResourceMonitor() as monitor:
                metrics = monitor.metrics()

                # Environment metrics; all mocked out.
                self.assertEqual(metrics.host_name, 'hostname')
                self.assertEqual(metrics.physical_core_count, 3)
                self.assertEqual(metrics.cpu_frequency_mhz, 2312.4)
                self.assertEqual(metrics.total_memory_mb, 1234)

                # Runtime metrics; they are all mocked out.
                self.assertEqual(metrics.cpu_user_time_seconds, 1.0)
                self.assertEqual(metrics.cpu_system_time_seconds, 2.0)
                self.assertEqual(metrics.memory_peak_rss_mb, 3)
                self.assertEqual(metrics.read_bytes, 12)
                self.assertEqual(metrics.write_bytes, 34)
Esempio n. 6
0
    def test_monitor_real_function_calls(self):
        # We want to actually make all of the real function calls under test, but
        # we of course don't know their values and can only do sanity checks.
        with resources.ResourceMonitor() as monitor:
            metrics = monitor.metrics()
            self.assertGreater(len(metrics.host_name), 0)
            self.assertGreater(metrics.physical_core_count, 0)
            self.assertGreater(metrics.total_memory_mb, 0)
            self.assertGreater(metrics.cpu_user_time_seconds, 0)
            self.assertGreater(metrics.cpu_system_time_seconds, 0)
            self.assertGreater(metrics.memory_peak_rss_mb, 0)

            # We unfortunately cannot make sure that read_bytes and write_bytes is
            # greater than zero, so these tests are commented out.
            # self.assertGreater(metrics.read_bytes, 0)
            # self.assertGreater(metrics.write_bytes, 0)

            # CPU frequency may not be available on all systems, so the value is
            # either a real frequency (> 0) or the magic value of 0.0 indicating that
            # the value could not be determined.
            self.assertGreaterEqual(metrics.cpu_frequency_mhz, 0.0)
Esempio n. 7
0
 def test_metrics_without_start_raises_exception(self):
     monitor = resources.ResourceMonitor()
     with self.assertRaises(RuntimeError):
         monitor.metrics()
Esempio n. 8
0
 def test_start_returns_self(self):
     monitor = resources.ResourceMonitor()
     self.assertIs(monitor.start(), monitor)