Ahmad Sharif | 0dcbc4b | 2012-02-02 16:37:18 -0800 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | |
| 3 | # Copyright 2011 Google Inc. All Rights Reserved. |
| 4 | |
Ahmad Sharif | 4467f00 | 2012-12-20 12:09:49 -0800 | [diff] [blame] | 5 | """The class to show the banner.""" |
| 6 | |
Ahmad Sharif | 0dcbc4b | 2012-02-02 16:37:18 -0800 | [diff] [blame] | 7 | import datetime |
| 8 | import time |
| 9 | |
| 10 | |
| 11 | class ExperimentStatus(object): |
Ahmad Sharif | 4467f00 | 2012-12-20 12:09:49 -0800 | [diff] [blame] | 12 | """The status class.""" |
| 13 | |
Ahmad Sharif | 0dcbc4b | 2012-02-02 16:37:18 -0800 | [diff] [blame] | 14 | def __init__(self, experiment): |
| 15 | self.experiment = experiment |
| 16 | self.num_total = len(self.experiment.benchmark_runs) |
Ahmad Sharif | 4467f00 | 2012-12-20 12:09:49 -0800 | [diff] [blame] | 17 | self.completed = 0 |
| 18 | self.new_job_start_time = time.time() |
Ahmad Sharif | 0dcbc4b | 2012-02-02 16:37:18 -0800 | [diff] [blame] | 19 | |
| 20 | def _GetProgressBar(self, num_complete, num_total): |
| 21 | ret = "Done: %s%%" % int(100.0 * num_complete / num_total) |
| 22 | bar_length = 50 |
| 23 | done_char = ">" |
| 24 | undone_char = " " |
| 25 | num_complete_chars = bar_length * num_complete / num_total |
| 26 | num_undone_chars = bar_length - num_complete_chars |
| 27 | ret += " [%s%s]" % (num_complete_chars * done_char, num_undone_chars * |
| 28 | undone_char) |
| 29 | return ret |
| 30 | |
| 31 | def GetProgressString(self): |
Ahmad Sharif | 4467f00 | 2012-12-20 12:09:49 -0800 | [diff] [blame] | 32 | """Get the elapsed_time, ETA.""" |
Ahmad Sharif | 0dcbc4b | 2012-02-02 16:37:18 -0800 | [diff] [blame] | 33 | current_time = time.time() |
| 34 | if self.experiment.start_time: |
| 35 | elapsed_time = current_time - self.experiment.start_time |
| 36 | else: |
| 37 | elapsed_time = 0 |
| 38 | try: |
Ahmad Sharif | 4467f00 | 2012-12-20 12:09:49 -0800 | [diff] [blame] | 39 | if self.completed != self.experiment.num_complete: |
| 40 | self.completed = self.experiment.num_complete |
| 41 | self.new_job_start_time = current_time |
| 42 | time_completed_jobs = (elapsed_time - |
| 43 | (current_time - self.new_job_start_time)) |
| 44 | eta_seconds = (float(self.num_total - self.experiment.num_complete -1) * |
| 45 | time_completed_jobs / self.experiment.num_run_complete |
| 46 | + (time_completed_jobs / self.experiment.num_run_complete |
| 47 | - (current_time - self.new_job_start_time))) |
| 48 | |
Ahmad Sharif | 0dcbc4b | 2012-02-02 16:37:18 -0800 | [diff] [blame] | 49 | eta_seconds = int(eta_seconds) |
| 50 | eta = datetime.timedelta(seconds=eta_seconds) |
| 51 | except ZeroDivisionError: |
| 52 | eta = "Unknown" |
| 53 | strings = [] |
| 54 | strings.append("Current time: %s Elapsed: %s ETA: %s" % |
| 55 | (datetime.datetime.now(), |
| 56 | datetime.timedelta(seconds=int(elapsed_time)), |
| 57 | eta)) |
| 58 | strings.append(self._GetProgressBar(self.experiment.num_complete, |
| 59 | self.num_total)) |
| 60 | return "\n".join(strings) |
| 61 | |
| 62 | def GetStatusString(self): |
Ahmad Sharif | 4467f00 | 2012-12-20 12:09:49 -0800 | [diff] [blame] | 63 | """Get the status string of all the benchmark_runs.""" |
Ahmad Sharif | 0dcbc4b | 2012-02-02 16:37:18 -0800 | [diff] [blame] | 64 | status_bins = {} |
| 65 | for benchmark_run in self.experiment.benchmark_runs: |
Ahmad Sharif | 4467f00 | 2012-12-20 12:09:49 -0800 | [diff] [blame] | 66 | if benchmark_run.timeline.GetLastEvent() not in status_bins: |
| 67 | status_bins[benchmark_run.timeline.GetLastEvent()] = [] |
| 68 | status_bins[benchmark_run.timeline.GetLastEvent()].append(benchmark_run) |
Ahmad Sharif | 0dcbc4b | 2012-02-02 16:37:18 -0800 | [diff] [blame] | 69 | |
| 70 | status_strings = [] |
| 71 | for key, val in status_bins.items(): |
| 72 | status_strings.append("%s: %s" % |
| 73 | (key, self._GetNamesAndIterations(val))) |
| 74 | result = "Thread Status:\n%s" % "\n".join(status_strings) |
| 75 | |
| 76 | # Add the machine manager status. |
| 77 | result += "\n" + self.experiment.machine_manager.AsString() + "\n" |
| 78 | |
| 79 | return result |
| 80 | |
| 81 | def _GetNamesAndIterations(self, benchmark_runs): |
| 82 | strings = [] |
Ahmad Sharif | 4467f00 | 2012-12-20 12:09:49 -0800 | [diff] [blame] | 83 | t = time.time() |
Ahmad Sharif | 0dcbc4b | 2012-02-02 16:37:18 -0800 | [diff] [blame] | 84 | for benchmark_run in benchmark_runs: |
Ahmad Sharif | 4467f00 | 2012-12-20 12:09:49 -0800 | [diff] [blame] | 85 | t_last = benchmark_run.timeline.GetLastEventTime() |
| 86 | elapsed = str(datetime.timedelta(seconds=int(t-t_last))) |
| 87 | strings.append("'{0}' {1}".format(benchmark_run.name, elapsed)) |
Ahmad Sharif | 0dcbc4b | 2012-02-02 16:37:18 -0800 | [diff] [blame] | 88 | return " %s (%s)" % (len(strings), ", ".join(strings)) |