blob: 2c13fd386b663d7ea37db5b9cd2a861ba828445c [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):
inikep2d9272f2016-06-21 19:28:51 +020016 print(time.strftime("%Y/%m/%d %H:%M:%S") + ' - ' + text)
inikep9470b872016-06-09 12:54:06 +020017
inikep2d9272f2016-06-21 19:28:51 +020018
19def execute(command, print_output=False, print_error=True, param_shell=True):
inikep9470b872016-06-09 12:54:06 +020020 log("> " + command)
inikep2d9272f2016-06-21 19:28:51 +020021 popen = Popen(command, stdout=PIPE, stderr=PIPE, shell=param_shell, cwd=execute.cwd)
inikep9470b872016-06-09 12:54:06 +020022 itout = iter(popen.stdout.readline, b"")
23 iterr = iter(popen.stderr.readline, b"")
24 stdout_lines = list(itout)
inikep9470b872016-06-09 12:54:06 +020025 stderr_lines = list(iterr)
inikep9470b872016-06-09 12:54:06 +020026 popen.communicate()
inikep2d9272f2016-06-21 19:28:51 +020027 if print_output:
28 print(''.join(stdout_lines))
29 print(''.join(stderr_lines))
inikep9470b872016-06-09 12:54:06 +020030 if popen.returncode is not None and popen.returncode != 0:
inikepc1b154a2016-06-10 12:53:12 +020031 if not print_output and print_error:
inikep2d9272f2016-06-21 19:28:51 +020032 print(''.join(stderr_lines))
33 return popen.returncode, stdout_lines, stderr_lines
inikep9470b872016-06-09 12:54:06 +020034execute.cwd = None
35
36
inikepc1b154a2016-06-10 12:53:12 +020037def does_command_exist(command):
inikep2d9272f2016-06-21 19:28:51 +020038 result, stdoutdata, stderrdata = execute(command, False, False);
39 return result == 0
inikepc1b154a2016-06-10 12:53:12 +020040
41
inikep9470b872016-06-09 12:54:06 +020042def fetch():
43 execute('git fetch -p')
inikep2d9272f2016-06-21 19:28:51 +020044 returncode, output, stderrdata = execute('git branch -rl')
inikep9470b872016-06-09 12:54:06 +020045 for line in output:
46 if "HEAD" in line:
inikep2d9272f2016-06-21 19:28:51 +020047 output.remove(line) # remove "origin/HEAD -> origin/dev"
inikep9470b872016-06-09 12:54:06 +020048 branches = map(lambda l: l.strip(), output)
inikep2d9272f2016-06-21 19:28:51 +020049 print("branches=%s" % branches)
50 return map(lambda b: (b, execute('git show -s --format=%h ' + b)[1][0].strip()), branches)
inikep9470b872016-06-09 12:54:06 +020051
52
53def notify(branch, commit, last_commit):
54 text_tmpl = string.Template('Changes since $last_commit:\r\n$commits')
55 branch = branch.split('/')[1]
56 fmt = '--format="%h: (%an) %s, %ar"'
57 if last_commit is None:
inikep2d9272f2016-06-21 19:28:51 +020058 returncode, commits, stderrdata = execute('git log -n 10 %s %s' % (fmt, commit))
inikep9470b872016-06-09 12:54:06 +020059 else:
inikep2d9272f2016-06-21 19:28:51 +020060 returncode, commits, stderrdata = execute('git --no-pager log %s %s..%s' % (fmt, last_commit, commit))
inikep9470b872016-06-09 12:54:06 +020061 text = text_tmpl.substitute({'last_commit': last_commit, 'commits': ''.join(commits)})
inikep2d9272f2016-06-21 19:28:51 +020062 print(str("commits for %s: %s" % (commit, text)))
inikep9470b872016-06-09 12:54:06 +020063
inikep2d9272f2016-06-21 19:28:51 +020064
inikep9470b872016-06-09 12:54:06 +020065def compile(branch, commit, dry_run):
66 local_branch = string.split(branch, '/')[1]
67 version = local_branch.rpartition('-')[2]
68 version = version + '_' + commit
69 execute('git checkout -- . && git checkout ' + branch)
70 if not dry_run:
71 execute('VERSION=' + version + '; make clean zstdprogram')
72
73
74def get_last_commit(resultsFileName):
75 if not os.path.isfile(resultsFileName):
76 return None, None, None
77 commit = None
78 cspeed = []
79 dspeed = []
80 with open(resultsFileName,'r') as f:
81 for line in f:
82 words = line.split()
83 if len(words) == 2: # branch + commit
84 commit = words[1];
85 cspeed = []
86 dspeed = []
87 if (len(words) == 8):
88 cspeed.append(float(words[3]))
89 dspeed.append(float(words[5]))
90 #if commit != None:
91 # print "commit=%s cspeed=%s dspeed=%s" % (commit, cspeed, dspeed)
92 return commit, cspeed, dspeed
93
94
inikepc1b154a2016-06-10 12:53:12 +020095def benchmark_and_compare(branch, commit, resultsFileName, lastCLevel, testFilePath, fileName, last_cspeed, last_dspeed, lower_limit, maxLoadAvg, message):
96 sleepTime = 30
inikep9470b872016-06-09 12:54:06 +020097 while os.getloadavg()[0] > maxLoadAvg:
inikepc1b154a2016-06-10 12:53:12 +020098 log("WARNING: bench loadavg=%.2f is higher than %s, sleeping for %s seconds" % (os.getloadavg()[0], maxLoadAvg, sleepTime))
99 time.sleep(sleepTime)
inikep9470b872016-06-09 12:54:06 +0200100 start_load = str(os.getloadavg())
inikep2d9272f2016-06-21 19:28:51 +0200101 returncode, result, stderrdata = execute('programs/zstd -qi5b1e' + str(lastCLevel) + ' ' + testFilePath)
inikep9470b872016-06-09 12:54:06 +0200102 end_load = str(os.getloadavg())
103 linesExpected = lastCLevel + 2;
104 if len(result) != linesExpected:
inikepc1b154a2016-06-10 12:53:12 +0200105 log("ERROR: number of result lines=%d is different that expected %d" % (len(result), linesExpected))
inikep9470b872016-06-09 12:54:06 +0200106 return ""
107 with open(resultsFileName, "a") as myfile:
108 myfile.write(branch + " " + commit + "\n")
109 myfile.writelines(result)
110 myfile.close()
111 if (last_cspeed == None):
112 return ""
113 commit, cspeed, dspeed = get_last_commit(resultsFileName)
114 text = ""
115 for i in range(0, min(len(cspeed), len(last_cspeed))):
116 if (cspeed[i]/last_cspeed[i] < lower_limit):
117 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])
118 if (dspeed[i]/last_dspeed[i] < lower_limit):
119 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])
120 if text:
inikepc1b154a2016-06-10 12:53:12 +0200121 text = message + ("\nmaxLoadAvg=%s load average at start=%s end=%s\n" % (maxLoadAvg, start_load, end_load)) + text
inikep9470b872016-06-09 12:54:06 +0200122 return text
123
124
inikepd731de82016-06-21 11:26:17 +0200125def send_simple_email(emails, email_topic, have_mutt, have_mail):
126 if have_mutt:
127 execute('mutt -s "' + email_header + ' ' + email_topic + '" ' + emails + ' </dev/null')
128 elif have_mail:
129 execute('mail -s "' + email_header + ' ' + email_topic + '" ' + emails + ' </dev/null')
130 else:
131 log("e-mail cannot be sent (mail and mutt not found)")
132
133
inikepc1b154a2016-06-10 12:53:12 +0200134def send_email(branch, commit, last_commit, emails, text, results_files, logFileName, lower_limit, have_mutt, have_mail):
inikep9470b872016-06-09 12:54:06 +0200135 with open(logFileName, "w") as myfile:
136 myfile.writelines(text)
137 myfile.close()
inikepc1b154a2016-06-10 12:53:12 +0200138 if have_mutt:
inikepd731de82016-06-21 11:26:17 +0200139 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 +0200140 elif have_mail:
inikepd731de82016-06-21 11:26:17 +0200141 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 +0200142 else:
143 log("e-mail cannot be sent (mail and mutt not found)")
inikep9470b872016-06-09 12:54:06 +0200144
145
inikepc1b154a2016-06-10 12:53:12 +0200146def check_branches(args, test_path, testFilePaths, have_mutt, have_mail):
inikep9470b872016-06-09 12:54:06 +0200147 for branch, commit in fetch():
inikep9470b872016-06-09 12:54:06 +0200148 try:
149 commitFileName = test_path + "/commit_" + branch.replace("/", "_")
150 if os.path.isfile(commitFileName):
151 last_commit = file(commitFileName, 'r').read()
152 else:
153 last_commit = None
154 file(commitFileName, 'w').write(commit)
155
156 if commit == last_commit:
157 log("skipping branch %s: head %s already processed" % (branch, commit))
158 else:
159 log("build branch %s: head %s is different from prev %s" % (branch, commit, last_commit))
160 compile(branch, commit, args.dry_run)
161
162 logFileName = test_path + "/log_" + branch.replace("/", "_")
163 text_to_send = []
164 results_files = ""
165 for filePath in testFilePaths:
166 fileName = filePath.rpartition('/')[2]
167 resultsFileName = test_path + "/results_" + branch.replace("/", "_") + "_" + fileName
168 last_commit, cspeed, dspeed = get_last_commit(resultsFileName)
169
170 if not args.dry_run:
inikepc1b154a2016-06-10 12:53:12 +0200171 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 +0200172 if text:
inikepc1b154a2016-06-10 12:53:12 +0200173 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 +0200174 if text:
175 text_to_send.append(text)
176 results_files += resultsFileName + " "
177 if text_to_send:
inikepc1b154a2016-06-10 12:53:12 +0200178 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 +0200179 notify(branch, commit, last_commit)
180 except Exception as e:
181 stack = traceback.format_exc()
inikepc1b154a2016-06-10 12:53:12 +0200182 log("ERROR: build %s, error %s" % (branch, str(e)) )
inikep2d9272f2016-06-21 19:28:51 +0200183 print(stack)
inikep9470b872016-06-09 12:54:06 +0200184
185
186if __name__ == '__main__':
187 parser = argparse.ArgumentParser()
inikepc1b154a2016-06-10 12:53:12 +0200188 parser.add_argument('testFileNames', help='file names list for speed benchmark')
189 parser.add_argument('emails', help='list of e-mail addresses to send warnings')
inikep1e375f12016-06-13 10:50:09 +0200190 parser.add_argument('--message', help='attach an additional message to e-mail', default="")
inikepd731de82016-06-21 11:26:17 +0200191 parser.add_argument('--repoURL', help='changes default repository URL', default=default_repo_url)
inikepc1b154a2016-06-10 12:53:12 +0200192 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 +0200193 parser.add_argument('--maxLoadAvg', type=float, help='maximum load average to start testing', default=0.75)
194 parser.add_argument('--lastCLevel', type=int, help='last compression level for testing', default=5)
inikepc1b154a2016-06-10 12:53:12 +0200195 parser.add_argument('--sleepTime', type=int, help='frequency of repository checking in seconds', default=300)
inikep9470b872016-06-09 12:54:06 +0200196 parser.add_argument('--dry-run', dest='dry_run', action='store_true', help='not build', default=False)
197 args = parser.parse_args()
198
199 # check if test files are accessible
200 testFileNames = args.testFileNames.split()
201 testFilePaths = []
202 for fileName in testFileNames:
203 if os.path.isfile(fileName):
204 testFilePaths.append(os.path.abspath(fileName))
205 else:
inikepd731de82016-06-21 11:26:17 +0200206 log("ERROR: File not found: " + fileName)
207 exit(1)
inikep9470b872016-06-09 12:54:06 +0200208
209 test_path = os.getcwd() + '/' + test_dir_name # /path/to/zstd/tests/speedTest
210 clone_path = test_path + '/' + 'zstd' # /path/to/zstd/tests/speedTest/zstd
inikep9470b872016-06-09 12:54:06 +0200211
inikepc1b154a2016-06-10 12:53:12 +0200212 # check availability of e-mail senders
inikep2d9272f2016-06-21 19:28:51 +0200213 have_mutt = does_command_exist("mutt -h");
inikepc1b154a2016-06-10 12:53:12 +0200214 have_mail = does_command_exist("mail -V");
inikepf1690292016-06-10 13:59:08 +0200215 if not have_mutt and not have_mail:
inikepd731de82016-06-21 11:26:17 +0200216 log("ERROR: e-mail senders 'mail' or 'mutt' not found")
217 exit(1)
inikepc1b154a2016-06-10 12:53:12 +0200218
inikep2d9272f2016-06-21 19:28:51 +0200219 print("PARAMETERS:\nrepoURL=%s" % args.repoURL)
220 print("test_path=%s" % test_path)
221 print("clone_path=%s" % clone_path)
222 print("testFilePath(%s)=%s" % (len(testFilePaths), testFilePaths))
223 print("message=%s" % args.message)
224 print("emails=%s" % args.emails)
225 print("maxLoadAvg=%s" % args.maxLoadAvg)
226 print("lowerLimit=%s" % args.lowerLimit)
227 print("lastCLevel=%s" % args.lastCLevel)
228 print("sleepTime=%s" % args.sleepTime)
229 print("dry_run=%s" % args.dry_run)
230 print("have_mutt=%s have_mail=%s" % (have_mutt, have_mail))
inikepc1b154a2016-06-10 12:53:12 +0200231
inikepd731de82016-06-21 11:26:17 +0200232 # clone ZSTD repo if needed
233 if not os.path.isdir(test_path):
234 os.mkdir(test_path)
235 if not os.path.isdir(clone_path):
236 execute.cwd = test_path
237 execute('git clone ' + args.repoURL)
238 if not os.path.isdir(clone_path):
239 log("ERROR: ZSTD clone not found: " + clone_path)
240 exit(1)
241 execute.cwd = clone_path
242
243 # check if speedTest.pid already exists
244 pid = str(os.getpid())
245 pidfile = "./speedTest.pid"
246 if os.path.isfile(pidfile):
247 log("ERROR: %s already exists, exiting" % pidfile)
248 exit(1)
249
250 send_simple_email(args.emails, "test-zstd-speed.py(%s) has been started" % pid, have_mutt, have_mail)
inikep9470b872016-06-09 12:54:06 +0200251 while True:
inikepd731de82016-06-21 11:26:17 +0200252 file(pidfile, 'w').write(pid)
253 try:
254 loadavg = os.getloadavg()[0]
255 if (loadavg <= args.maxLoadAvg):
256 check_branches(args, test_path, testFilePaths, have_mutt, have_mail)
257 else:
258 log("WARNING: main loadavg=%.2f is higher than %s" % (loadavg, args.maxLoadAvg))
259 finally:
260 os.unlink(pidfile)
inikepc1b154a2016-06-10 12:53:12 +0200261 log("sleep for %s seconds" % args.sleepTime)
inikep9470b872016-06-09 12:54:06 +0200262 time.sleep(args.sleepTime)