Exemplo n.º 1
0
 def testUIDecimalShort(self):
     """Tests DecimalShort for UI."""
     self.assertEqual('12.3b', DecimalShort(12345678910))
     self.assertEqual('123.5m', DecimalShort(123456789))
     self.assertEqual('1.2k', DecimalShort(1234))
     self.assertEqual('1.0k', DecimalShort(1000))
     self.assertEqual('432', DecimalShort(432))
     self.assertEqual('43.2t', DecimalShort(43.25 * 10**12))
     self.assertEqual('43.2q', DecimalShort(43.25 * 10**15))
     self.assertEqual('43250.0q', DecimalShort(43.25 * 10**18))
Exemplo n.º 2
0
  def PrintFinalSummaryMessage(self, stream=sys.stderr):
    """Prints a final message to indicate operation succeeded.

    Args:
      stream: Stream to print messages. Usually sys.stderr, but customizable
              for testing.
    """
    string_to_print = ('Operation completed over %s objects' %
                       DecimalShort(self.num_objects))
    if self.total_size:
      string_to_print += ('/%s' %
                          HumanReadableWithDecimalPlaces(self.total_size))
    remaining_width = self.console_width - len(string_to_print)
    if not self.quiet_mode:
      stream.write(('\n' + string_to_print + '.' +
                    (max(remaining_width, 0) * ' ') + '\n'))
Exemplo n.º 3
0
  def PrintProgress(self, stream=sys.stderr):
    """Prints progress and throughput/time estimation.

    If a ProducerThreadMessage or SeekAheadMessage has been provided,
    it outputs the number of files completed, number of total files,
    the current progress, the total size, and the percentage it
    represents.
    If none of those have been provided, it only includes the number of files
    completed, the current progress and total size (which might be updated),
    with no percentage as we do not know if more files are coming.
    It may also include time estimation (available only given
    ProducerThreadMessage or SeekAheadMessage provided) and throughput. For that
    to happen, there is an extra condition of at least first_throughput_latency
    seconds having been passed since the UIController started, and that
    either the ProducerThread or the SeekAheadThread have estimated total
    number of files and total size.

    Args:
      stream: Stream to print messages. Usually sys.stderr, but customizable
              for testing.
    """
    # Time to update all information.
    total_remaining = self.total_size - self.total_progress

    if self.throughput:
      time_remaining = total_remaining / self.throughput
    else:
      time_remaining = None

    char_to_print = self.GetSpinner()

    if self.num_objects_source <= EstimationSource.SEEK_AHEAD_THREAD:
      # An example of objects_completed here would be ' [2/3 files]'.
      objects_completed = ('[' + DecimalShort(self.objects_finished) + '/' +
                           DecimalShort(self.num_objects) + ' files]')
    else:
      # An example of objects_completed here would be ' [2 files]'.
      objects_completed = '[' + DecimalShort(self.objects_finished) + ' files]'

    # An example of bytes_progress would be '[101.0 MiB/1.0 GiB]'.
    bytes_progress = ('[%s/%s]' % (BytesToFixedWidthString(
        self.total_progress), BytesToFixedWidthString(self.total_size)))

    if self.total_size_source <= EstimationSource.SEEK_AHEAD_THREAD:
      if self.num_objects == self.objects_finished:
        percentage = '100'
      else:
        percentage = (
            '%3d' %
            min(99, int(100 * float(self.total_progress) / self.total_size)))
      percentage_completed = percentage + '% Done'
    else:
      percentage_completed = ''

    if (self.refresh_message_time - self.start_time >
        self.first_throughput_latency):
      # Should also include throughput.
      # An example of throughput here would be ' 82.3 MiB/s'
      throughput = BytesToFixedWidthString(self.throughput) + '/s'

      if (self.total_size_source <= EstimationSource.PRODUCER_THREAD_ESTIMATE
          and self.throughput):
        # Should also include time remaining.
        # An example of time remaining would be ' ETA 00:00:11'.
        time_remaining_str = 'ETA ' + PrettyTime(time_remaining)
      else:
        time_remaining_str = ''
    else:
      throughput = ''
      time_remaining_str = ''

    format_str = ('{char_to_print} {objects_completed}{bytes_progress}'
                  ' {percentage_completed} {throughput} {time_remaining_str}')
    string_to_print = format_str.format(
        char_to_print=char_to_print,
        objects_completed=objects_completed,
        bytes_progress=bytes_progress,
        percentage_completed=percentage_completed,
        throughput=throughput,
        time_remaining_str=time_remaining_str)
    remaining_width = self.console_width - len(string_to_print)
    if not self.quiet_mode:
      stream.write(string_to_print + (max(remaining_width, 0) * ' ') + '\r')
Exemplo n.º 4
0
  def PrintProgress(self, stream=sys.stderr):
    """Prints progress and throughput/time estimation.

    Prints total number of objects and number of finished objects with the
    percentage of work done, potentially including the throughput
    (in objects/second) and estimated time remaining.

    Args:
      stream: Stream to print messages. Usually sys.stderr, but customizable
              for testing.
    """
    # Time to update all information
    total_remaining = self.num_objects - self.objects_finished
    if self.throughput:
      time_remaining = total_remaining / self.throughput
    else:
      time_remaining = None

    char_to_print = self.GetSpinner()
    if self.num_objects_source <= EstimationSource.SEEK_AHEAD_THREAD:
      # An example of objects_completed here would be ' [2/3 objects]'.
      objects_completed = ('[' + DecimalShort(self.objects_finished) + '/' +
                           DecimalShort(self.num_objects) + ' objects]')
      if self.num_objects == self.objects_finished:
        percentage = '100'
      else:
        percentage = (
            '%3d' %
            min(99, int(100 * float(self.objects_finished) / self.num_objects)))
      percentage_completed = percentage + '% Done'
    else:
      # An example of objects_completed here would be ' [2 objects]'.
      objects_completed = ('[' + DecimalShort(self.objects_finished) +
                           ' objects]')
      percentage_completed = ''

    if (self.refresh_message_time - self.start_time >
        self.first_throughput_latency):
      # Should also include throughput.
      # An example of throughput here would be '2 objects/s'
      throughput = '%.2f objects/s' % self.throughput
      if (self.num_objects_source <= EstimationSource.PRODUCER_THREAD_ESTIMATE
          and self.throughput):
        # Should also include time remaining.
        # An example of time remaining would be ' ETA 00:00:11'.
        time_remaining_str = 'ETA ' + PrettyTime(time_remaining)
      else:
        time_remaining_str = ''
    else:
      throughput = ''
      time_remaining_str = ''

    format_str = ('{char_to_print} {objects_completed} {percentage_completed}'
                  ' {throughput} {time_remaining_str}')
    string_to_print = format_str.format(
        char_to_print=char_to_print,
        objects_completed=objects_completed,
        percentage_completed=percentage_completed,
        throughput=throughput,
        time_remaining_str=time_remaining_str)
    remaining_width = self.console_width - len(string_to_print)
    if not self.quiet_mode:
      stream.write(string_to_print + (max(remaining_width, 0) * ' ') + '\r')