Skip to content

Instantly share code, notes, and snippets.

@benley
Created August 13, 2013 03:17
Show Gist options
  • Save benley/6217596 to your computer and use it in GitHub Desktop.
Save benley/6217596 to your computer and use it in GitHub Desktop.
calculate average runtimes of jenkins jobs
#!/usr/bin/python2.7
"""Average DURRRRRR - get average runtime of a jenkins job.
This is obviously just an example - a plain average of the runtimes
isn't actually all that useful. Use numpy, do something fancier.
"""
import datetime
import gflags
import json
import logging
import numpy
import urllib2
import sys
gflags.DEFINE_string('jobname', None, 'Name of jenkins job to query.')
gflags.DEFINE_string('baseurl', 'http://jenkins.cloudscaling.com',
'Base URL of jenkins server.')
FLAGS = gflags.FLAGS
def main(argv):
logging.basicConfig(level=logging.INFO)
try:
argv = FLAGS(argv)
except gflags.FlagsError as err:
logging.error('%s\n\nUsage: %s\n%s', err, sys.argv[0], FLAGS)
sys.exit(1)
if not FLAGS.jobname:
logging.error('Required flag not set: --jobname\n\nUsage: %s\n%s',
sys.argv[0], FLAGS)
sys.exit(1)
joburl = '%s/job/%s' % (FLAGS.baseurl, FLAGS.jobname)
joblist = json.load(urllib2.urlopen('%s/api/json' % joburl))
durations = []
estimated_durations = []
for builddata in joblist['builds']:
jobdata = json.load(urllib2.urlopen('%s/api/json' % builddata['url']))
durations.append(jobdata['duration'])
estimated_durations.append(jobdata['estimatedDuration'])
average_duration = datetime.timedelta(milliseconds=numpy.mean(durations))
print 'Average duration: %s' % average_duration
average_estimated_duration = datetime.timedelta(milliseconds=numpy.mean(
estimated_durations))
# Is this even remotely useful? It's how long jenkins guesses the job will
# take before it starts, I think.
print 'Average estimated duration: %s' % average_estimated_duration
if __name__ == '__main__':
main(sys.argv)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment