blob: 5cc07fb379fa7c99e25c217ab4851862f5b14a94 [file] [log] [blame]
inikep9470b872016-06-09 12:54:06 +02001#! /usr/bin/env python
2# execute(), fetch(), notify() are based on https://github.com/getlantern/build-automation/blob/master/build.py
3
4import argparse
5import os
6import string
7import time
8import traceback
9from subprocess import Popen, PIPE
10
inikepd731de82016-06-21 11:26:17 +020011default_repo_url = 'https://github.com/Cyan4973/zstd.git'
inikep9470b872016-06-09 12:54:06 +020012test_dir_name = 'speedTest'
inikepd731de82016-06-21 11:26:17 +020013email_header = '[ZSTD_speedTest]'
inikep9470b872016-06-09 12:54:06 +020014
15def log(text):
16 print time.strftime("%Y/%m/%d %H:%M:%S") + ' - ' + text
17
inikepc1b154a2016-06-10 12:53:12 +020018def execute(command, print_output=False, print_error=True):
inikep9470b872016-06-09 12:54:06 +020019 log("> " + command)
20 popen = Popen(command, stdout=PIPE, stderr=PIPE, shell=True, cwd=execute.cwd)
21 itout = iter(popen.stdout.readline, b"")
22 iterr = iter(popen.stderr.readline, b"")
23 stdout_lines = list(itout)
24 if print_output:
25 print ''.join(stdout_lines)
26 stderr_lines = list(iterr)
inikepc1b154a2016-06-10 12:53:12 +020027 if print_output:
inikep9470b872016-06-09 12:54:06 +020028 print ''.join(stderr_lines)
29 popen.communicate()
30 if popen.returncode is not None and popen.returncode != 0:
inikepc1b154a2016-06-10 12:53:12 +020031 if not print_output and print_error:
32 print ''.join(stderr_lines)
inikep9470b872016-06-09 12:54:06 +020033 raise RuntimeError(''.join(stderr_lines))
34 return stdout_lines + stderr_lines
35execute.cwd = None
36
37
inikepc1b154a2016-06-10 12:53:12 +020038def does_command_exist(command):
39 try:
40 execute(command, False, False);
41 except Exception as e:
42 return False
43 return True
44
45
inikep9470b872016-06-09 12:54:06 +020046def fetch():
47 execute('git fetch -p')
48 output = execute('git branch -rl')
49 for line in output:
50 if "HEAD" in line:
51 output.remove(line) # remove "origin/HEAD -> origin/dev"
52 branches = map(lambda l: l.strip(), output)
53 return map(lambda b: (b, execute('git show -s --format=%h ' + b)[0].strip()), branches)
54
55
56def notify(branch, commit, last_commit):
57 text_tmpl = string.Template('Changes since $last_commit:\r\n$commits')
58 branch = branch.split('/')[1]
59 fmt = '--format="%h: (%an) %s, %ar"'
60 if last_commit is None:
61 commits = execute('git log -n 10 %s %s' % (fmt, commit))
62 else:
63 commits = execute('git --no-pager log %s %s..%s' % (fmt, last_commit, commit))
64
65 text = text_tmpl.substitute({'last_commit': last_commit, 'commits': ''.join(commits)})
66 print str("commits for %s: %s" % (commit, text))
67
68
69def compile(branch, commit, dry_run):
70 local_branch = string.split(branch, '/')[1]
71 version = local_branch.rpartition('-')[2]
72 version = version + '_' + commit
73 execute('git checkout -- . && git checkout ' + branch)
74 if not dry_run:
75 execute('VERSION=' + version + '; make clean zstdprogram')
76
77
78def get_last_commit(resultsFileName):
79 if not os.path.isfile(resultsFileName):
80 return None, None, None
81 commit = None
82 cspeed = []
83 dspeed = []
84 with open(resultsFileName,'r') as f:
85 for line in f:
86 words = line.split()
87 if len(words) == 2: # branch + commit
88 commit = words[1];
89 cspeed = []
90 dspeed = []
91 if (len(words) == 8):
92 cspeed.append(float(words[3]))
93 dspeed.append(float(words[5]))
94 #if commit != None:
95 # print "commit=%s cspeed=%s dspeed=%s" % (commit, cspeed, dspeed)
96 return commit, cspeed, dspeed
97
98
inikepc1b154a2016-06-10 12:53:12 +020099def benchmark_and_compare(branch, commit, resultsFileName, lastCLevel, testFilePath, fileName, last_cspeed, last_dspeed, lower_limit, maxLoadAvg, message):
100 sleepTime = 30
inikep9470b872016-06-09 12:54:06 +0200101 while os.getloadavg()[0] > maxLoadAvg:
inikepc1b154a2016-06-10 12:53:12 +0200102 log("WARNING: bench loadavg=%.2f is higher than %s, sleeping for %s seconds" % (os.getloadavg()[0], maxLoadAvg, sleepTime))
103 time.sleep(sleepTime)
inikep9470b872016-06-09 12:54:06 +0200104 start_load = str(os.getloadavg())
inikepc1b154a2016-06-10 12:53:12 +0200105 result = execute('programs/zstd -qi5b1e' + str(lastCLevel) + ' ' + testFilePath)
inikep9470b872016-06-09 12:54:06 +0200106 end_load = str(os.getloadavg())
107 linesExpected = lastCLevel + 2;
108 if len(result) != linesExpected:
inikepc1b154a2016-06-10 12:53:12 +0200109 log("ERROR: number of result lines=%d is different that expected %d" % (len(result), linesExpected))
inikep9470b872016-06-09 12:54:06 +0200110 return ""
111 with open(resultsFileName, "a") as myfile:
112 myfile.write(branch + " " + commit + "\n")
113 myfile.writelines(result)
114 myfile.close()
115 if (last_cspeed == None):
116 return ""
117 commit, cspeed, dspeed = get_last_commit(resultsFileName)
118 text = ""
119 for i in range(0, min(len(cspeed), len(last_cspeed))):
120 if (cspeed[i]/last_cspeed[i] < lower_limit):
121 text += "WARNING: File=%s level=%d cspeed=%s last=%s diff=%s\n" % (fileName, i+1, cspeed[i], last_cspeed[i], cspeed[i]/last_cspeed[i])
122 if (dspeed[i]/last_dspeed[i] < lower_limit):
123 text += "WARNING: File=%s level=%d dspeed=%s last=%s diff=%s\n" % (fileName, i+1, dspeed[i], last_dspeed[i], dspeed[i]/last_dspeed[i])
124 if text:
inikepc1b154a2016-06-10 12:53:12 +0200125 text = message + ("\nmaxLoadAvg=%s load average at start=%s end=%s\n" % (maxLoadAvg, start_load, end_load)) + text
inikep9470b872016-06-09 12:54:06 +0200126 return text
127
128
inikepd731de82016-06-21 11:26:17 +0200129def send_simple_email(emails, email_topic, have_mutt, have_mail):
130 if have_mutt:
131 execute('mutt -s "' + email_header + ' ' + email_topic + '" ' + emails + ' </dev/null')
132 elif have_mail:
133 execute('mail -s "' + email_header + ' ' + email_topic + '" ' + emails + ' </dev/null')
134 else:
135 log("e-mail cannot be sent (mail and mutt not found)")
136
137
inikepc1b154a2016-06-10 12:53:12 +0200138def send_email(branch, commit, last_commit, emails, text, results_files, logFileName, lower_limit, have_mutt, have_mail):
inikep9470b872016-06-09 12:54:06 +0200139 with open(logFileName, "w") as myfile:
140 myfile.writelines(text)
141 myfile.close()
inikepc1b154a2016-06-10 12:53:12 +0200142 if have_mutt:
inikepd731de82016-06-21 11:26:17 +0200143 execute('mutt -s "' + email_header + ' Warning for branch=' + branch + ' commit=' + commit + ' last_commit=' + last_commit + ' speed<' + str(lower_limit) + '" ' + emails + ' -a ' + results_files + ' < ' + logFileName)
inikepc1b154a2016-06-10 12:53:12 +0200144 elif have_mail:
inikepd731de82016-06-21 11:26:17 +0200145 execute('mail -s "' + email_header + ' Warning for branch=' + branch + ' commit=' + commit + ' last_commit=' + last_commit + ' speed<' + str(lower_limit) + '" ' + emails + ' < ' + logFileName)
inikepc1b154a2016-06-10 12:53:12 +0200146 else:
147 log("e-mail cannot be sent (mail and mutt not found)")
inikep9470b872016-06-09 12:54:06 +0200148
149
inikepc1b154a2016-06-10 12:53:12 +0200150def check_branches(args, test_path, testFilePaths, have_mutt, have_mail):
inikep9470b872016-06-09 12:54:06 +0200151 for branch, commit in fetch():
inikep9470b872016-06-09 12:54:06 +0200152 try:
153 commitFileName = test_path + "/commit_" + branch.replace("/", "_")
154 if os.path.isfile(commitFileName):
155 last_commit = file(commitFileName, 'r').read()
156 else:
157 last_commit = None
158 file(commitFileName, 'w').write(commit)
159
160 if commit == last_commit:
161 log("skipping branch %s: head %s already processed" % (branch, commit))
162 else:
163 log("build branch %s: head %s is different from prev %s" % (branch, commit, last_commit))
164 compile(branch, commit, args.dry_run)
165
166 logFileName = test_path + "/log_" + branch.replace("/", "_")
167 text_to_send = []
168 results_files = ""
169 for filePath in testFilePaths:
170 fileName = filePath.rpartition('/')[2]
171 resultsFileName = test_path + "/results_" + branch.replace("/", "_") + "_" + fileName
172 last_commit, cspeed, dspeed = get_last_commit(resultsFileName)
173
174 if not args.dry_run:
inikepc1b154a2016-06-10 12:53:12 +0200175 text = benchmark_and_compare(branch, commit, resultsFileName, args.lastCLevel, filePath, fileName, cspeed, dspeed, args.lowerLimit, args.maxLoadAvg, args.message)
inikep9470b872016-06-09 12:54:06 +0200176 if text:
inikepc1b154a2016-06-10 12:53:12 +0200177 text = benchmark_and_compare(branch, commit, resultsFileName, args.lastCLevel, filePath, fileName, cspeed, dspeed, args.lowerLimit, args.maxLoadAvg, args.message)
inikep9470b872016-06-09 12:54:06 +0200178 if text:
179 text_to_send.append(text)
180 results_files += resultsFileName + " "
181 if text_to_send:
inikepc1b154a2016-06-10 12:53:12 +0200182 send_email(branch, commit, last_commit, args.emails, text_to_send, results_files, logFileName, args.lowerLimit, have_mutt, have_mail)
inikep9470b872016-06-09 12:54:06 +0200183 notify(branch, commit, last_commit)
184 except Exception as e:
185 stack = traceback.format_exc()
inikepc1b154a2016-06-10 12:53:12 +0200186 log("ERROR: build %s, error %s" % (branch, str(e)) )
inikep1e375f12016-06-13 10:50:09 +0200187 print stack
inikep9470b872016-06-09 12:54:06 +0200188
189
190if __name__ == '__main__':
191 parser = argparse.ArgumentParser()
inikepc1b154a2016-06-10 12:53:12 +0200192 parser.add_argument('testFileNames', help='file names list for speed benchmark')
193 parser.add_argument('emails', help='list of e-mail addresses to send warnings')
inikep1e375f12016-06-13 10:50:09 +0200194 parser.add_argument('--message', help='attach an additional message to e-mail', default="")
inikepd731de82016-06-21 11:26:17 +0200195 parser.add_argument('--repoURL', help='changes default repository URL', default=default_repo_url)
inikepc1b154a2016-06-10 12:53:12 +0200196 parser.add_argument('--lowerLimit', type=float, help='send email if speed is lower than given limit', default=0.98)
inikep9470b872016-06-09 12:54:06 +0200197 parser.add_argument('--maxLoadAvg', type=float, help='maximum load average to start testing', default=0.75)
198 parser.add_argument('--lastCLevel', type=int, help='last compression level for testing', default=5)
inikepc1b154a2016-06-10 12:53:12 +0200199 parser.add_argument('--sleepTime', type=int, help='frequency of repository checking in seconds', default=300)
inikep9470b872016-06-09 12:54:06 +0200200 parser.add_argument('--dry-run', dest='dry_run', action='store_true', help='not build', default=False)
201 args = parser.parse_args()
202
203 # check if test files are accessible
204 testFileNames = args.testFileNames.split()
205 testFilePaths = []
206 for fileName in testFileNames:
207 if os.path.isfile(fileName):
208 testFilePaths.append(os.path.abspath(fileName))
209 else:
inikepd731de82016-06-21 11:26:17 +0200210 log("ERROR: File not found: " + fileName)
211 exit(1)
inikep9470b872016-06-09 12:54:06 +0200212
213 test_path = os.getcwd() + '/' + test_dir_name # /path/to/zstd/tests/speedTest
214 clone_path = test_path + '/' + 'zstd' # /path/to/zstd/tests/speedTest/zstd
inikep9470b872016-06-09 12:54:06 +0200215
inikepc1b154a2016-06-10 12:53:12 +0200216 # check availability of e-mail senders
217 have_mutt = does_command_exist("mutt --help");
218 have_mail = does_command_exist("mail -V");
inikepf1690292016-06-10 13:59:08 +0200219 if not have_mutt and not have_mail:
inikepd731de82016-06-21 11:26:17 +0200220 log("ERROR: e-mail senders 'mail' or 'mutt' not found")
221 exit(1)
inikepc1b154a2016-06-10 12:53:12 +0200222
inikepd731de82016-06-21 11:26:17 +0200223 print "PARAMETERS:\nrepoURL=%s" % args.repoURL
224 print "test_path=%s" % test_path
inikepc1b154a2016-06-10 12:53:12 +0200225 print "clone_path=%s" % clone_path
226 print "testFilePath(%s)=%s" % (len(testFilePaths), testFilePaths)
227 print "message=%s" % args.message
228 print "emails=%s" % args.emails
229 print "maxLoadAvg=%s" % args.maxLoadAvg
230 print "lowerLimit=%s" % args.lowerLimit
231 print "lastCLevel=%s" % args.lastCLevel
232 print "sleepTime=%s" % args.sleepTime
233 print "dry_run=%s" % args.dry_run
234 print "have_mutt=%s have_mail=%s" % (have_mutt, have_mail)
235
inikepd731de82016-06-21 11:26:17 +0200236 # clone ZSTD repo if needed
237 if not os.path.isdir(test_path):
238 os.mkdir(test_path)
239 if not os.path.isdir(clone_path):
240 execute.cwd = test_path
241 execute('git clone ' + args.repoURL)
242 if not os.path.isdir(clone_path):
243 log("ERROR: ZSTD clone not found: " + clone_path)
244 exit(1)
245 execute.cwd = clone_path
246
247 # check if speedTest.pid already exists
248 pid = str(os.getpid())
249 pidfile = "./speedTest.pid"
250 if os.path.isfile(pidfile):
251 log("ERROR: %s already exists, exiting" % pidfile)
252 exit(1)
253
254 send_simple_email(args.emails, "test-zstd-speed.py(%s) has been started" % pid, have_mutt, have_mail)
inikep9470b872016-06-09 12:54:06 +0200255 while True:
inikepd731de82016-06-21 11:26:17 +0200256 file(pidfile, 'w').write(pid)
257 try:
258 loadavg = os.getloadavg()[0]
259 if (loadavg <= args.maxLoadAvg):
260 check_branches(args, test_path, testFilePaths, have_mutt, have_mail)
261 else:
262 log("WARNING: main loadavg=%.2f is higher than %s" % (loadavg, args.maxLoadAvg))
263 finally:
264 os.unlink(pidfile)
inikepc1b154a2016-06-10 12:53:12 +0200265 log("sleep for %s seconds" % args.sleepTime)
inikep9470b872016-06-09 12:54:06 +0200266 time.sleep(args.sleepTime)