recipestats.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. #!/usr/bin/env python
  2. #
  3. # Copyright (c) 2007-2010 Nokia Corporation and/or its subsidiary(-ies).
  4. # All rights reserved.
  5. # This component and the accompanying materials are made available
  6. # under the terms of the License "Eclipse Public License v1.0"
  7. # which accompanies this distribution, and is available
  8. # at the URL "http://www.eclipse.org/legal/epl-v10.html".
  9. #
  10. # Initial Contributors:
  11. # Nokia Corporation - initial contribution.
  12. #
  13. # Contributors:
  14. #
  15. # Description:
  16. #
  17. # display summary information about recipes from raptor logs
  18. # e.g. total times and so on.
  19. import time
  20. import __future__
  21. class RecipeStats(object):
  22. def __init__(self, name, count, time):
  23. self.name=name
  24. self.count=count
  25. self.time=time
  26. def add(self, duration):
  27. self.time += duration
  28. self.count += 1
  29. class BuildStats(object):
  30. STAT_OK = 0
  31. def __init__(self):
  32. self.stats = {}
  33. self.failcount = 0
  34. self.failtime = 0.0
  35. self.failtypes = {}
  36. self.retryfails = 0
  37. self.hosts = {}
  38. def add(self, starttime, duration, name, status, host, phase):
  39. if status != BuildStats.STAT_OK:
  40. self.failcount += 1
  41. if name in self.failtypes:
  42. self.failtypes[name] += 1
  43. else:
  44. self.failtypes[name] = 1
  45. if status == 128:
  46. self.retryfails += 1
  47. return
  48. if name in self.stats:
  49. r = self.stats[name]
  50. r.add(duration)
  51. else:
  52. self.stats[name] = RecipeStats(name,1,duration)
  53. hp=host
  54. if hp in self.hosts:
  55. self.hosts[hp] += 1
  56. else:
  57. self.hosts[hp] = 1
  58. def recipe_csv(self):
  59. s = '"name", "time", "count"\n'
  60. l = sorted(self.stats.values(), key= lambda r: r.time, reverse=True)
  61. for r in l:
  62. s += '"%s",%s,%d\n' % (r.name, str(r.time), r.count)
  63. return s
  64. def hosts_csv(self):
  65. s='"host","recipecount"\n'
  66. hs = self.hosts
  67. for h in sorted(hs.keys()):
  68. s += '"%s",%d\n' % (h,hs[h])
  69. return s
  70. import sys
  71. import re
  72. import os
  73. from optparse import OptionParser # for parsing command line parameters
  74. def main():
  75. recipe_re = re.compile(".*<recipe name='([^']+)'.*host='([^']+)'.*")
  76. time_re = re.compile(".*<time start='([0-9]+\.[0-9]+)' *elapsed='([0-9]+\.[0-9]+)'.*")
  77. status_re = re.compile(".*<status exit='(?P<exit>(ok|failed))'( *code='(?P<code>[0-9]+)')?.*")
  78. phase_re = re.compile(".*<info>Making.*?([^\.]+\.[^\.]+)</info>")
  79. parser = OptionParser(prog = "recipestats",
  80. usage = """%prog --help [-b] [-f <logfilename>]""")
  81. parser.add_option("-b","--buildhosts",action="store_true",dest="buildhosts_flag",
  82. help="Lists which build hosts were active in each invocation of the build engine and how many recipes ran on each.", default = False)
  83. parser.add_option("-f","--logfile",action="store",dest="logfilename", help="Read from the file, not stdin", default = None)
  84. (options, stuff) = parser.parse_args(sys.argv[1:])
  85. if options.logfilename is None:
  86. f = sys.stdin
  87. else:
  88. f = open(options.logfilename,"r")
  89. st = BuildStats()
  90. alternating = 0
  91. start_time = 0.0
  92. phase=None
  93. for l in f:
  94. l2 = l.rstrip("\n\r")
  95. rm = recipe_re.match(l2)
  96. if rm is not None:
  97. (rname,host) = rm.groups()
  98. continue
  99. pm = phase_re.match(l2)
  100. if pm is not None:
  101. if phase is not None:
  102. if options.buildhosts_flag:
  103. print('"%s"\n' % phase)
  104. print(st.hosts_csv())
  105. st.hosts = {}
  106. phase = pm.groups()[0]
  107. continue
  108. tm = time_re.match(l2)
  109. if tm is not None:
  110. try:
  111. s = float(tm.groups()[0])
  112. elapsed = float(tm.groups()[1])
  113. if start_time == 0.0:
  114. start_time = s
  115. s -= start_time
  116. continue
  117. except ValueError, e:
  118. raise Exception("Parse problem: float conversion on these groups: %s\n%s" %(str(tm.groups()), str(e)))
  119. else:
  120. if l2.find("<time") is not -1:
  121. raise Exception("unparsed timing status: %s\n"%l2)
  122. sm = status_re.match(l2)
  123. if sm is None:
  124. continue
  125. if sm.groupdict()['exit'] == 'ok':
  126. status = 0
  127. else:
  128. status = int(sm.groupdict()['code'])
  129. st.add(s, elapsed, rname, status, host, phase)
  130. if options.buildhosts_flag:
  131. print('"%s"\n' % phase)
  132. print(st.hosts_csv())
  133. else:
  134. print(st.recipe_csv())
  135. if __name__ == '__main__': main()