-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathJob-Duration-Metrics.py
104 lines (85 loc) · 3.19 KB
/
Job-Duration-Metrics.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import jenkins
import matplotlib.pyplot as plt
import matplotlib
import time
import sys, getopt
from datetime import datetime
class DurationMetrics:
username = ''
password = ''
totalBuildDuration = 0.0
numberOfBuilds = 0.0
buildDurations = []
buildTimestamps = []
server = None
def __init__(self,username,password):
self.username = username
self.password = password
def calculateAverageDuration(self):
#TODO: calculate average duration
averageDuration = (self.totalBuildDuration / self.numberOfBuilds) / 1000
return averageDuration
def plotJobDuration(self):
#TODO: plot job duration
dateTimeObjs = self.convertTimestamps()
dates = matplotlib.dates.date2num(dateTimeObjs)
plt.plot_date(dates,self.buildDurations,'-')
plt.xlabel('Time of Execution')
plt.ylabel('Build Duration (seconds)')
plt.title('Build Durations Over Time')
plt.gcf().autofmt_xdate()
plt.show()
def getJobDuration(self):
# TODO: get job duration
jenkinsJobs = self.server.get_all_jobs()
print(jenkinsJobs)
myJob = self.server.get_job_info('python-test', 0, True)
print(myJob)
myJobBuilds = myJob.get('builds')
for build in myJobBuilds:
buildNumber = build.get('number')
buildInfo = self.server.get_build_info('python-test', buildNumber)
#print(buildInfo)
buildDuration = buildInfo.get('duration')
self.buildDurations.append((buildDuration / 1000))
self.totalBuildDuration += buildDuration
self.numberOfBuilds += 1.0
buildTimestamp = buildInfo.get('timestamp')
self.buildTimestamps.append(buildTimestamp)
def connectToJenkins(self):
# TODO: connect to Jenkins server
self.server = jenkins.Jenkins('http://localhost:8080', username=self.username, password=self.password)
user = self.server.get_whoami()
version = self.server.get_version()
print('Hello %s from Jenkins %s' % (user['fullName'], version))
def convertTimestamps(self):
dates = []
for timestamp in self.buildTimestamps:
dateTimeObj = datetime.fromtimestamp((timestamp / 1000))
dates.append(dateTimeObj)
return dates
def main(argv):
username = ''
password = ''
try:
opts, args = getopt.getopt(argv, "hu:p:", ["username=", "password="])
except getopt.GetoptError:
print
'python Job-Duration-Metrics.py -u <username> -p <password>'
sys.exit(2)
for opt, arg in opts:
if opt == '-h':
print
'python Job-Duration-Metrics.py -u <username> -p <password>'
sys.exit()
elif opt in ("-u", "--username"):
username = arg
elif opt in ("-p", "--password"):
password = arg
durationMetrics = DurationMetrics(username,password)
durationMetrics.connectToJenkins()
durationMetrics.getJobDuration()
print("Build Average Duration: %.2f seconds" % durationMetrics.calculateAverageDuration())
durationMetrics.plotJobDuration()
if __name__ == "__main__":
main(sys.argv[1:])