def run(self, args): params = {} if args.all: params['all'] = 1 images = self.api.get('/images/json', params=params).json() self.by_id = dict((x['Id'], x) for x in images) fields = collections.OrderedDict(( ('Name', self.name_getter), ('ID', lambda x: x['Id'].split(':', 1)[-1][:12]), ('Parent', self.parent_getter), ('Created', self.created_getter), ('Labels', self.label_getter), ('Size', self.size_getter), )) colspec = ( None, 12, None, None, None, None, ) t = shellish.Table(headers=fields.keys(), accessors=fields.values(), columns=colspec, **self.table_options(args)) t.print(images)
def run(self, args): report = requests.get(args.url + self.urn + 'report') if not report.ok: print(report.json()) return by_call = {} for stats in report.json(): for x in stats: key = (x['call']['file'], x['call']['function'], x['call']['lineno']) try: ref = by_call[key] except KeyError: by_call[key] = x['stats'] else: ref['totaltime'] += x['stats']['totaltime'] ref['inlinetime'] += x['stats']['inlinetime'] ref['callcount'] += x['stats']['callcount'] stats = sorted(by_call.items(), key=lambda x: x[1][args.sortby], reverse=True) table = shellish.Table( headers=['Function', 'Total Time', 'Inline Time', 'Calls']) if args.limit is not None: del stats[args.limit:] table.print([ '%s:%s:%s' % ('/'.join(x[0][0].rsplit('/', 2)[-2:]), x[0][1], x[0][2]), '%f' % x[1]['totaltime'], '%f' % x[1]['inlinetime'], x[1]['callcount'] ] for x in stats)
def show_mostcommon(self, report): merged = collections.defaultdict(lambda: 0) for worker in report: for otype, count in worker: merged[otype] += count stats = sorted(merged.items(), key=lambda x: x[1], reverse=True) table = shellish.Table(headers=[ 'Type', 'Count', ]) table.print(stats)
def show_growth(self, report): merged = collections.defaultdict(lambda: [0, 0]) for worker in report: for otype, count, change in worker: merged[otype][0] += count merged[otype][1] += change stats = sorted(((k, *v) for k, v in merged.items()), key=lambda x: x[2], reverse=True) table = shellish.Table(headers=[ 'Type', 'Count', 'Change', ]) table.print(stats)
def run(self, args): params = {} if args.all: params['all'] = 1 containers = self.api.get('/containers/json', params=params).json() fields = collections.OrderedDict(( ('NAME', self.name_getter), ('IMAGE', self.image_getter), ('COMMAND', lambda x: x['Command'].split()[0]), ('CREATED', self.created_getter), ('STATUS', 'Status'), ('PORTS', lambda x: ', '.join(map(self.port_render, x['Ports']))), )) t = shellish.Table(headers=fields.keys(), accessors=fields.values(), **self.table_options(args)) t.print(containers)
def _run(self, args): requests.put(args.url + self.urn + 'active', json=True) prev_totals = {} prev_ts = None while True: height = shutil.get_terminal_size()[1] report = requests.get(args.url + self.urn + 'report') ts = self.timer() if not report.ok: print(report.json()) return totals = {} for stats in report.json(): for x in stats: key = (x['call']['file'], x['call']['function'], x['call']['lineno']) try: ref = totals[key] except KeyError: totals[key] = x['stats'] else: ref['totaltime'] += x['stats']['totaltime'] ref['inlinetime'] += x['stats']['inlinetime'] ref['callcount'] += x['stats']['callcount'] if prev_ts is None: prev_ts = ts prev_totals = totals time.sleep(0.100) # quickly to get data up. continue period = ts - prev_ts for call, stats in totals.items(): if call in prev_totals: prev = prev_totals[call] sample_total = max( stats['inlinetime'] - prev['inlinetime'], 0) sample_calls = stats['callcount'] - prev['callcount'] stats['cpu_percent'] = sample_total / period stats['call_rate'] = sample_calls / period if sample_calls: stats['percall_time'] = sample_total / sample_calls else: stats['percall_time'] = 0 else: stats['cpu_percent'] = 0 stats['call_rate'] = 0 stats['percall_time'] = 0 prev_ts = ts prev_totals = totals stats = sorted(totals.items(), key=lambda x: x[1][args.sortby], reverse=True) del stats[height - 3:] table = shellish.Table(headers=[ 'Function', 'CPU %', 'Inline-Time', 'Total-Time', 'Time/call', 'Calls/s', 'Calls', ]) print('\033[H') # Move cursor home table.print([ '%s:%s:%s' % ('/'.join(x[0][0].rsplit('/', 2)[-2:]), x[0][1], x[0][2]), self.human_num((x[1]['cpu_percent'] * 100), prec=1), self.human_num(x[1]['inlinetime']), self.human_num(x[1]['totaltime']), '%.0f μs' % (x[1]['percall_time'] * 1000000), self.human_num(x[1]['call_rate']), self.human_num(x[1]['callcount']) ] for x in stats) time.sleep(args.refresh)