sbsv2/raptor/bin/recipestats.py
author Bob Rosenberg <bob.rosenberg@nokia.com>
Mon, 20 Sep 2010 10:55:43 +0100
changeset 658 cab9da9b71bb
parent 625 a1925fb7753a
permissions -rw-r--r--
Test and fail for badly invalid unit elements when joining
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
591
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
     1
#!/usr/bin/env python
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
     2
#
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
     3
# Copyright (c) 2007-2010 Nokia Corporation and/or its subsidiary(-ies).
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
     4
# All rights reserved.
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
     5
# This component and the accompanying materials are made available
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
     6
# under the terms of the License "Eclipse Public License v1.0"
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
     7
# which accompanies this distribution, and is available
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
     8
# at the URL "http://www.eclipse.org/legal/epl-v10.html".
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
     9
#
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
    10
# Initial Contributors:
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
    11
# Nokia Corporation - initial contribution.
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
    12
#
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
    13
# Contributors:
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
    14
#
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
    15
# Description: 
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
    16
# 
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
    17
# display summary information about recipes from raptor logs
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
    18
# e.g. total times and so on.
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
    19
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
    20
import time
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
    21
import __future__
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
    22
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
    23
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
    24
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
    25
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
    26
class RecipeStats(object):
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
    27
	def __init__(self, name, count, time):
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
    28
		self.name=name
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
    29
		self.count=count
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
    30
		self.time=time
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
    31
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
    32
	def add(self, duration):
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
    33
		self.time += duration
625
a1925fb7753a sbs version 2.15.0
Richard Taylor <richard.i.taylor@nokia.com>
parents: 591
diff changeset
    34
		self.count += 1
591
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
    35
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
    36
class BuildStats(object):
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
    37
	STAT_OK = 0
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
    38
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
    39
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
    40
	def __init__(self):
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
    41
		self.stats = {}
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
    42
		self.failcount = 0
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
    43
		self.failtime = 0.0
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
    44
		self.failtypes = {}
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
    45
		self.retryfails = 0
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
    46
		self.hosts = {}
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
    47
		
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
    48
	def add(self, starttime, duration, name, status, host, phase):
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
    49
		if status != BuildStats.STAT_OK:
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
    50
			self.failcount += 1
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
    51
			if name in self.failtypes:
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
    52
				self.failtypes[name] += 1
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
    53
			else:
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
    54
				self.failtypes[name] = 1
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
    55
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
    56
			if status == 128:
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
    57
				self.retryfails += 1
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
    58
			return
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
    59
			
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
    60
		if name in self.stats:
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
    61
			r = self.stats[name]
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
    62
			r.add(duration)
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
    63
		else:
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
    64
			self.stats[name] = RecipeStats(name,1,duration)
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
    65
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
    66
		hp=host
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
    67
		if hp in self.hosts:
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
    68
			self.hosts[hp] += 1
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
    69
		else:
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
    70
			self.hosts[hp] = 1
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
    71
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
    72
	def recipe_csv(self):
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
    73
		s = '"name", "time", "count"\n'
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
    74
		l = sorted(self.stats.values(), key= lambda r: r.time, reverse=True)
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
    75
		for r in l:
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
    76
			s += '"%s",%s,%d\n' % (r.name, str(r.time), r.count)
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
    77
		return s
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
    78
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
    79
	def hosts_csv(self):
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
    80
		s='"host","recipecount"\n'
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
    81
		hs = self.hosts
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
    82
		for h in sorted(hs.keys()):
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
    83
			s += '"%s",%d\n' % (h,hs[h])
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
    84
		return s
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
    85
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
    86
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
    87
import sys
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
    88
import re
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
    89
import os
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
    90
from optparse import OptionParser # for parsing command line parameters
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
    91
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
    92
def main():
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
    93
	recipe_re = re.compile(".*<recipe name='([^']+)'.*host='([^']+)'.*")
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
    94
	time_re = re.compile(".*<time start='([0-9]+\.[0-9]+)' *elapsed='([0-9]+\.[0-9]+)'.*")
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
    95
	status_re = re.compile(".*<status exit='(?P<exit>(ok|failed))'( *code='(?P<code>[0-9]+)')?.*")
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
    96
	phase_re = re.compile(".*<info>Making.*?([^\.]+\.[^\.]+)</info>")
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
    97
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
    98
	parser = OptionParser(prog = "recipestats",
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
    99
                                          usage = """%prog --help [-b] [-f <logfilename>]""")
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   100
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   101
	parser.add_option("-b","--buildhosts",action="store_true",dest="buildhosts_flag",
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   102
                                help="Lists which build hosts were active in each invocation of the build engine and how many recipes ran on each.", default = False)
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   103
	parser.add_option("-f","--logfile",action="store",dest="logfilename", help="Read from the file, not stdin", default = None)
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   104
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   105
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   106
	(options, stuff) = parser.parse_args(sys.argv[1:])
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   107
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   108
	if options.logfilename is None:
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   109
		f = sys.stdin
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   110
	else:
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   111
		f = open(options.logfilename,"r")
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   112
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   113
	st = BuildStats()
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   114
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   115
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   116
	alternating = 0
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   117
	start_time = 0.0
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   118
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   119
	phase=None
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   120
	for l in f:
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   121
		l2 = l.rstrip("\n\r")
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   122
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   123
		rm = recipe_re.match(l2)
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   124
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   125
		if rm is not None:
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   126
			(rname,host) = rm.groups()
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   127
			continue
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   128
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   129
		pm = phase_re.match(l2)
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   130
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   131
		if pm is not None:
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   132
			if phase is not None:
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   133
				if options.buildhosts_flag:
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   134
					print('"%s"\n' % phase)
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   135
					print(st.hosts_csv())
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   136
			st.hosts = {}	
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   137
			phase = pm.groups()[0]
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   138
			continue
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   139
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   140
		tm = time_re.match(l2)
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   141
		if tm is not None:
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   142
			try:
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   143
				s = float(tm.groups()[0])
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   144
				elapsed = float(tm.groups()[1])
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   145
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   146
				if start_time == 0.0:
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   147
					start_time = s
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   148
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   149
				s -= start_time
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   150
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   151
				continue
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   152
			except ValueError, e:
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   153
				raise Exception("Parse problem: float conversion on these groups: %s\n%s" %(str(tm.groups()), str(e)))
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   154
		else:
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   155
			if l2.find("<time") is not -1:
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   156
				raise Exception("unparsed timing status: %s\n"%l2)
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   157
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   158
		sm = status_re.match(l2)
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   159
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   160
		if sm is None:
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   161
			continue
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   162
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   163
		if sm.groupdict()['exit'] == 'ok':
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   164
			status = 0
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   165
		else:
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   166
			status = int(sm.groupdict()['code'])
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   167
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   168
		st.add(s, elapsed, rname, status, host, phase)
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   169
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   170
	if options.buildhosts_flag:
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   171
		print('"%s"\n' % phase)
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   172
		print(st.hosts_csv())
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   173
	else:
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   174
		print(st.recipe_csv())
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   175
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   176
22486c9c7b15 raptor v2.14.0
jjkang
parents:
diff changeset
   177
if __name__ == '__main__': main()