blob: 3e5eaa738b1e84145da939605e5287802ff9ea82 [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
11repo_url = 'https://github.com/Cyan4973/zstd.git'
12test_dir_name = 'speedTest'
13
14
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
inikepc1b154a2016-06-10 12:53:12 +0200129def send_email(branch, commit, last_commit, emails, text, results_files, logFileName, lower_limit, have_mutt, have_mail):
inikep9470b872016-06-09 12:54:06 +0200130 with open(logFileName, "w") as myfile:
131 myfile.writelines(text)
132 myfile.close()
inikepc1b154a2016-06-10 12:53:12 +0200133 if have_mutt:
134 execute("mutt -s \"[ZSTD_speedTest] Warning for branch=" + branch + " commit=" + commit + " last_commit=" + last_commit + " speed<" + str(lower_limit) + "\" " + emails + " -a " + results_files + " < " + logFileName)
135 elif have_mail:
136 execute("mail -s \"[ZSTD_speedTest] Warning for branch=" + branch + " commit=" + commit + " last_commit=" + last_commit + " speed<" + str(lower_limit) + "\" " + emails + " < " + logFileName)
137 else:
138 log("e-mail cannot be sent (mail and mutt not found)")
inikep9470b872016-06-09 12:54:06 +0200139
140
inikepc1b154a2016-06-10 12:53:12 +0200141def check_branches(args, test_path, testFilePaths, have_mutt, have_mail):
inikep9470b872016-06-09 12:54:06 +0200142 for branch, commit in fetch():
inikep9470b872016-06-09 12:54:06 +0200143 try:
144 commitFileName = test_path + "/commit_" + branch.replace("/", "_")
145 if os.path.isfile(commitFileName):
146 last_commit = file(commitFileName, 'r').read()
147 else:
148 last_commit = None
149 file(commitFileName, 'w').write(commit)
150
151 if commit == last_commit:
152 log("skipping branch %s: head %s already processed" % (branch, commit))
153 else:
154 log("build branch %s: head %s is different from prev %s" % (branch, commit, last_commit))
155 compile(branch, commit, args.dry_run)
156
157 logFileName = test_path + "/log_" + branch.replace("/", "_")
158 text_to_send = []
159 results_files = ""
160 for filePath in testFilePaths:
161 fileName = filePath.rpartition('/')[2]
162 resultsFileName = test_path + "/results_" + branch.replace("/", "_") + "_" + fileName
163 last_commit, cspeed, dspeed = get_last_commit(resultsFileName)
164
165 if not args.dry_run:
inikepc1b154a2016-06-10 12:53:12 +0200166 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 +0200167 if text:
inikepc1b154a2016-06-10 12:53:12 +0200168 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 +0200169 if text:
170 text_to_send.append(text)
171 results_files += resultsFileName + " "
172 if text_to_send:
inikepc1b154a2016-06-10 12:53:12 +0200173 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 +0200174 notify(branch, commit, last_commit)
175 except Exception as e:
176 stack = traceback.format_exc()
inikepc1b154a2016-06-10 12:53:12 +0200177 log("ERROR: build %s, error %s" % (branch, str(e)) )
inikep9470b872016-06-09 12:54:06 +0200178
179
180if __name__ == '__main__':
181 parser = argparse.ArgumentParser()
inikepc1b154a2016-06-10 12:53:12 +0200182 parser.add_argument('testFileNames', help='file names list for speed benchmark')
183 parser.add_argument('emails', help='list of e-mail addresses to send warnings')
184 parser.add_argument('--message', help='attach an additional message to e-mail')
185 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 +0200186 parser.add_argument('--maxLoadAvg', type=float, help='maximum load average to start testing', default=0.75)
187 parser.add_argument('--lastCLevel', type=int, help='last compression level for testing', default=5)
inikepc1b154a2016-06-10 12:53:12 +0200188 parser.add_argument('--sleepTime', type=int, help='frequency of repository checking in seconds', default=300)
inikep9470b872016-06-09 12:54:06 +0200189 parser.add_argument('--dry-run', dest='dry_run', action='store_true', help='not build', default=False)
190 args = parser.parse_args()
191
192 # check if test files are accessible
193 testFileNames = args.testFileNames.split()
194 testFilePaths = []
195 for fileName in testFileNames:
196 if os.path.isfile(fileName):
197 testFilePaths.append(os.path.abspath(fileName))
198 else:
199 raise RuntimeError("File not found: " + fileName)
200
201 test_path = os.getcwd() + '/' + test_dir_name # /path/to/zstd/tests/speedTest
202 clone_path = test_path + '/' + 'zstd' # /path/to/zstd/tests/speedTest/zstd
inikep9470b872016-06-09 12:54:06 +0200203
inikepc1b154a2016-06-10 12:53:12 +0200204 # check availability of e-mail senders
205 have_mutt = does_command_exist("mutt --help");
206 have_mail = does_command_exist("mail -V");
inikepf1690292016-06-10 13:59:08 +0200207 if not have_mutt and not have_mail:
inikepc1b154a2016-06-10 12:53:12 +0200208 log("WARNING: e-mail senders mail and mutt not found")
209
inikep9470b872016-06-09 12:54:06 +0200210 # clone ZSTD repo if needed
211 if not os.path.isdir(test_path):
212 os.mkdir(test_path)
213 if not os.path.isdir(clone_path):
inikep348a53a2016-06-09 13:14:21 +0200214 execute.cwd = test_path
inikep9470b872016-06-09 12:54:06 +0200215 execute('git clone ' + repo_url)
216 if not os.path.isdir(clone_path):
217 raise RuntimeError("ZSTD clone not found: " + clone_path)
inikep348a53a2016-06-09 13:14:21 +0200218 execute.cwd = clone_path
inikep9470b872016-06-09 12:54:06 +0200219
inikepc1b154a2016-06-10 12:53:12 +0200220 print "PARAMETERS:\ntest_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)
231
inikep9470b872016-06-09 12:54:06 +0200232 while True:
233 pid = str(os.getpid())
234 pidfile = "./speedTest.pid"
235 if os.path.isfile(pidfile):
inikepc1b154a2016-06-10 12:53:12 +0200236 log("%s already exists, exiting" % pidfile)
inikep9470b872016-06-09 12:54:06 +0200237 else:
238 file(pidfile, 'w').write(pid)
239 try:
240 loadavg = os.getloadavg()[0]
241 if (loadavg <= args.maxLoadAvg):
inikepc1b154a2016-06-10 12:53:12 +0200242 check_branches(args, test_path, testFilePaths, have_mutt, have_mail)
inikep9470b872016-06-09 12:54:06 +0200243 else:
inikepc1b154a2016-06-10 12:53:12 +0200244 log("WARNING: main loadavg=%.2f is higher than %s" % (loadavg, args.maxLoadAvg))
inikep9470b872016-06-09 12:54:06 +0200245 finally:
246 os.unlink(pidfile)
inikepc1b154a2016-06-10 12:53:12 +0200247 log("sleep for %s seconds" % args.sleepTime)
inikep9470b872016-06-09 12:54:06 +0200248 time.sleep(args.sleepTime)