recipestats.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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 sys
  21. import re
  22. import os
  23. from optparse import OptionParser # for parsing command line parameters
  24. class RecipeStats(object):
  25. def __init__(self, name, count, time):
  26. self.name=name
  27. self.count=count
  28. self.time=time
  29. def add(self, duration):
  30. self.time += duration
  31. self.count += 1
  32. class BuildStats(object):
  33. STAT_OK = 0
  34. def __init__(self):
  35. self.stats = {}
  36. self.failcount = 0
  37. self.failtime = 0.0
  38. self.failtypes = {}
  39. self.retryfails = 0
  40. self.hosts = {}
  41. def add(self, starttime, duration, name, status, host, phase):
  42. if status != BuildStats.STAT_OK:
  43. self.failcount += 1
  44. if name in self.failtypes:
  45. self.failtypes[name] += 1
  46. else:
  47. self.failtypes[name] = 1
  48. if status == 128:
  49. self.retryfails += 1
  50. return
  51. if name in self.stats:
  52. r = self.stats[name]
  53. r.add(duration)
  54. else:
  55. self.stats[name] = RecipeStats(name,1,duration)
  56. hp=host
  57. if hp in self.hosts:
  58. self.hosts[hp] += 1
  59. else:
  60. self.hosts[hp] = 1
  61. def recipe_csv(self):
  62. s = '"name", "time", "count"\n'
  63. l = sorted(list(self.stats.values()), key= lambda r: r.time, reverse=True)
  64. for r in l:
  65. s += '"{0}",{1},{2:d}\n'.format(r.name, str(r.time), r.count)
  66. return s
  67. def hosts_csv(self):
  68. s='"host","recipecount"\n'
  69. hs = self.hosts
  70. for h in sorted(hs.keys()):
  71. s += '"{0}",{1:d}\n'.format(h,hs[h])
  72. return s
  73. def main():
  74. recipe_re = re.compile(".*<recipe name='([^']+)'.*host='([^']+)'.*")
  75. time_re = re.compile(".*<time start='([0-9]+\.[0-9]+)' *elapsed='([0-9]+\.[0-9]+)'.*")
  76. status_re = re.compile(".*<status exit='(?P<exit>(ok|failed))'( *code='(?P<code>[0-9]+)')?.*")
  77. phase_re = re.compile(".*<info>Making.*?([^\.]+\.[^\.]+)</info>")
  78. parser = OptionParser(prog = "recipestats",
  79. usage = """%prog --help [-b] [-f <logfilename>]""")
  80. parser.add_option("-b","--buildhosts",action="store_true",dest="buildhosts_flag",
  81. help="Lists which build hosts were active in each invocation of the build engine and how many recipes ran on each.", default = False)
  82. parser.add_option("-f","--logfile",action="store",dest="logfilename", help="Read from the file, not stdin", default = None)
  83. (options, stuff) = parser.parse_args(sys.argv[1:])
  84. if options.logfilename is None:
  85. f = sys.stdin
  86. else:
  87. f = open(options.logfilename,"r")
  88. st = BuildStats()
  89. alternating = 0
  90. start_time = 0.0
  91. phase=None
  92. for l in f:
  93. l2 = l.rstrip("\n\r")
  94. rm = recipe_re.match(l2)
  95. if rm is not None:
  96. (rname,host) = rm.groups()
  97. continue
  98. pm = phase_re.match(l2)
  99. if pm is not None:
  100. if phase is not None:
  101. if options.buildhosts_flag:
  102. print('"{0}"\n'.format(phase))
  103. print(st.hosts_csv())
  104. st.hosts = {}
  105. phase = pm.groups()[0]
  106. continue
  107. tm = time_re.match(l2)
  108. if tm is not None:
  109. try:
  110. s = float(tm.groups()[0])
  111. elapsed = float(tm.groups()[1])
  112. if start_time == 0.0:
  113. start_time = s
  114. s -= start_time
  115. continue
  116. except ValueError as e:
  117. raise Exception("Parse problem: float conversion on these groups: {0}\n{1}".format(str(tm.groups()), str(e)))
  118. else:
  119. if l2.find("<time") is not -1:
  120. raise Exception("unparsed timing status: {0}\n".format(l2))
  121. sm = status_re.match(l2)
  122. if sm is None:
  123. continue
  124. if sm.groupdict()['exit'] == 'ok':
  125. status = 0
  126. else:
  127. status = int(sm.groupdict()['code'])
  128. st.add(s, elapsed, rname, status, host, phase)
  129. if options.buildhosts_flag:
  130. print('"{0}"\n'.format(phase))
  131. print(st.hosts_csv())
  132. else:
  133. print(st.recipe_csv())
  134. if __name__ == '__main__': main()