blob: 3a270663228bd433cd592619c40ed40b1b6000a6 [file] [log] [blame]
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -08001#!/usr/bin/python
2
3# Copyright 2011 Google Inc. All Rights Reserved.
4
Ahmad Sharif4467f002012-12-20 12:09:49 -08005"""The class to show the banner."""
6
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -08007import datetime
8import time
9
10
11class ExperimentStatus(object):
Ahmad Sharif4467f002012-12-20 12:09:49 -080012 """The status class."""
13
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -080014 def __init__(self, experiment):
15 self.experiment = experiment
16 self.num_total = len(self.experiment.benchmark_runs)
Ahmad Sharif4467f002012-12-20 12:09:49 -080017 self.completed = 0
18 self.new_job_start_time = time.time()
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -080019
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 Sharif4467f002012-12-20 12:09:49 -080032 """Get the elapsed_time, ETA."""
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -080033 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 Sharif4467f002012-12-20 12:09:49 -080039 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 Sharif0dcbc4b2012-02-02 16:37:18 -080049 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 Sharif4467f002012-12-20 12:09:49 -080063 """Get the status string of all the benchmark_runs."""
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -080064 status_bins = {}
65 for benchmark_run in self.experiment.benchmark_runs:
Ahmad Sharif4467f002012-12-20 12:09:49 -080066 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 Sharif0dcbc4b2012-02-02 16:37:18 -080069
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 Sharif4467f002012-12-20 12:09:49 -080083 t = time.time()
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -080084 for benchmark_run in benchmark_runs:
Ahmad Sharif4467f002012-12-20 12:09:49 -080085 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 Sharif0dcbc4b2012-02-02 16:37:18 -080088 return " %s (%s)" % (len(strings), ", ".join(strings))