commits.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. #!/usr/bin/env python
  2. # Copyright 2015 The Chromium Authors. All rights reserved.
  3. # Use of this source code is governed by a BSD-style license that can be
  4. # found in the LICENSE file.
  5. """Print statistics about the rate of commits to a repository."""
  6. import datetime
  7. import itertools
  8. import json
  9. import math
  10. import urllib
  11. import urllib2
  12. _BASE_URL = 'https://chromium.googlesource.com/'
  13. # Can be up to 10,000.
  14. _REVISION_COUNT = 1000
  15. _REPOSITORIES = [
  16. 'chromium/src',
  17. 'angle/angle',
  18. 'skia',
  19. 'v8/v8',
  20. ]
  21. def Pairwise(iterable):
  22. """s -> (s0,s1), (s1,s2), (s2, s3), ..."""
  23. a, b = itertools.tee(iterable)
  24. next(b, None)
  25. return itertools.izip(a, b)
  26. def Percentile(data, percentile):
  27. """Find a percentile of a list of values.
  28. Parameters:
  29. data: A sorted list of values.
  30. percentile: The percentile to look up, from 0.0 to 1.0.
  31. Returns:
  32. The percentile.
  33. Raises:
  34. ValueError: If data is empty.
  35. """
  36. if not data:
  37. raise ValueError()
  38. k = (len(data) - 1) * percentile
  39. f = math.floor(k)
  40. c = math.ceil(k)
  41. if f == c:
  42. return data[int(k)]
  43. return data[int(f)] * (c - k) + data[int(c)] * (k - f)
  44. def CommitTimes(repository, revision_count):
  45. parameters = urllib.urlencode((('n', revision_count), ('format', 'JSON')))
  46. url = '%s/%s/+log?%s' % (_BASE_URL, urllib.quote(repository), parameters)
  47. data = json.loads(''.join(urllib2.urlopen(url).read().splitlines()[1:]))
  48. commit_times = []
  49. for revision in data['log']:
  50. commit_time_string = revision['committer']['time']
  51. commit_time = datetime.datetime.strptime(
  52. commit_time_string, '%a %b %d %H:%M:%S %Y')
  53. commit_times.append(commit_time)
  54. return commit_times
  55. def main():
  56. for repository in _REPOSITORIES:
  57. commit_times = CommitTimes(repository, _REVISION_COUNT)
  58. commit_durations = []
  59. for time1, time2 in Pairwise(commit_times):
  60. commit_durations.append((time1 - time2).total_seconds())
  61. commit_durations.sort()
  62. print 'REPOSITORY:', repository
  63. print 'Start Date:', min(commit_times)
  64. print ' End Date:', max(commit_times)
  65. print ' Duration:', max(commit_times) - min(commit_times)
  66. print ' n:', len(commit_times)
  67. for p in (0.00, 0.05, 0.25, 0.50, 0.75, 0.95, 1.00):
  68. percentile = Percentile(commit_durations, p)
  69. print '%3d%% commit duration:' % (p * 100), '%6ds' % percentile
  70. mean = math.fsum(commit_durations) / len(commit_durations)
  71. print ' Min commit duration:', '%6ds' % min(commit_durations)
  72. print 'Mean commit duration:', '%6ds' % mean
  73. print ' Max commit duration:', '%6ds' % max(commit_durations)
  74. print
  75. if __name__ == '__main__':
  76. main()