blob: 562d0790cb479106e3c76cd15cb0ceaa52ea9278 [file] [log] [blame]
inikep9470b872016-06-09 12:54:06 +02001#! /usr/bin/env python
inikep9470b872016-06-09 12:54:06 +02002
3import argparse
4import os
5import string
6import time
7import traceback
inikep95da7432016-06-22 12:12:35 +02008import subprocess
inikepc364ee72016-06-22 14:01:53 +02009import signal
Yann Colletb7522982016-07-22 05:02:27 +020010
inikep9470b872016-06-09 12:54:06 +020011
inikepd731de82016-06-21 11:26:17 +020012default_repo_url = 'https://github.com/Cyan4973/zstd.git'
inikep95da7432016-06-22 12:12:35 +020013working_dir_name = 'speedTest'
Yann Colletb7522982016-07-22 05:02:27 +020014working_path = os.getcwd() + '/' + working_dir_name # /path/to/zstd/tests/speedTest
15clone_path = working_path + '/' + 'zstd' # /path/to/zstd/tests/speedTest/zstd
inikepd731de82016-06-21 11:26:17 +020016email_header = '[ZSTD_speedTest]'
inikep95da7432016-06-22 12:12:35 +020017pid = str(os.getpid())
inikep8c53ad52016-07-19 15:49:14 +020018verbose = False
inikep95da7432016-06-22 12:12:35 +020019
inikep9470b872016-06-09 12:54:06 +020020
21def log(text):
inikep2d9272f2016-06-21 19:28:51 +020022 print(time.strftime("%Y/%m/%d %H:%M:%S") + ' - ' + text)
inikep9470b872016-06-09 12:54:06 +020023
inikep2d9272f2016-06-21 19:28:51 +020024
inikep8c53ad52016-07-19 15:49:14 +020025def execute(command, print_command=True, print_output=False, print_error=True, param_shell=True):
26 if print_command:
27 log("> " + command)
inikep95da7432016-06-22 12:12:35 +020028 popen = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=param_shell, cwd=execute.cwd)
29 stdout = popen.communicate()[0]
30 stdout_lines = stdout.splitlines()
inikep2d9272f2016-06-21 19:28:51 +020031 if print_output:
inikep95da7432016-06-22 12:12:35 +020032 print('\n'.join(stdout_lines))
inikep9470b872016-06-09 12:54:06 +020033 if popen.returncode is not None and popen.returncode != 0:
inikepc1b154a2016-06-10 12:53:12 +020034 if not print_output and print_error:
inikep95da7432016-06-22 12:12:35 +020035 print('\n'.join(stdout_lines))
36 raise RuntimeError('\n'.join(stdout_lines))
37 return stdout_lines
inikep9470b872016-06-09 12:54:06 +020038execute.cwd = None
39
40
inikepc1b154a2016-06-10 12:53:12 +020041def does_command_exist(command):
inikep95da7432016-06-22 12:12:35 +020042 try:
inikep8c53ad52016-07-19 15:49:14 +020043 execute(command, verbose, False, False);
inikep95da7432016-06-22 12:12:35 +020044 except Exception as e:
45 return False
46 return True
inikepc1b154a2016-06-10 12:53:12 +020047
48
inikep95da7432016-06-22 12:12:35 +020049def send_email(emails, topic, text, have_mutt, have_mail):
50 logFileName = working_path + '/' + 'tmpEmailContent'
inikep9470b872016-06-09 12:54:06 +020051 with open(logFileName, "w") as myfile:
52 myfile.writelines(text)
53 myfile.close()
inikepc1b154a2016-06-10 12:53:12 +020054 if have_mutt:
inikep8c53ad52016-07-19 15:49:14 +020055 execute('mutt -s "' + topic + '" ' + emails + ' < ' + logFileName, verbose)
inikepc1b154a2016-06-10 12:53:12 +020056 elif have_mail:
inikep8c53ad52016-07-19 15:49:14 +020057 execute('mail -s "' + topic + '" ' + emails + ' < ' + logFileName, verbose)
inikepc1b154a2016-06-10 12:53:12 +020058 else:
inikep95da7432016-06-22 12:12:35 +020059 log("e-mail cannot be sent (mail or mutt not found)")
inikep9470b872016-06-09 12:54:06 +020060
61
inikepa4847eb2016-07-19 17:59:53 +020062def send_email_with_attachments(branch, commit, last_commit, args, text, results_files, logFileName, have_mutt, have_mail):
inikep95da7432016-06-22 12:12:35 +020063 with open(logFileName, "w") as myfile:
64 myfile.writelines(text)
65 myfile.close()
inikepa4847eb2016-07-19 17:59:53 +020066 email_topic = '%s:%s Warning for %s:%s last_commit=%s speed<%s ratio<%s' % (email_header, pid, branch, commit, last_commit, args.lowerLimit, args.ratioLimit)
inikep95da7432016-06-22 12:12:35 +020067 if have_mutt:
inikepa4847eb2016-07-19 17:59:53 +020068 execute('mutt -s "' + email_topic + '" ' + args.emails + ' -a ' + results_files + ' < ' + logFileName)
inikep95da7432016-06-22 12:12:35 +020069 elif have_mail:
inikepa4847eb2016-07-19 17:59:53 +020070 execute('mail -s "' + email_topic + '" ' + args.emails + ' < ' + logFileName)
inikep95da7432016-06-22 12:12:35 +020071 else:
72 log("e-mail cannot be sent (mail or mutt not found)")
73
74
inikepbcb9aad2016-06-22 13:07:58 +020075def git_get_branches():
inikep8c53ad52016-07-19 15:49:14 +020076 execute('git fetch -p', verbose)
77 branches = execute('git branch -rl', verbose)
inikep6e5beea2016-07-19 13:09:00 +020078 output = []
79 for line in branches:
80 if ("HEAD" not in line) and ("coverity_scan" not in line) and ("gh-pages" not in line):
81 output.append(line.strip())
82 return output
inikepbcb9aad2016-06-22 13:07:58 +020083
84
inikepf2f59d72016-06-22 15:42:26 +020085def git_get_changes(branch, commit, last_commit):
inikepbcb9aad2016-06-22 13:07:58 +020086 fmt = '--format="%h: (%an) %s, %ar"'
87 if last_commit is None:
88 commits = execute('git log -n 10 %s %s' % (fmt, commit))
89 else:
90 commits = execute('git --no-pager log %s %s..%s' % (fmt, last_commit, commit))
inikepf2f59d72016-06-22 15:42:26 +020091 return str('Changes in %s since %s:\n' % (branch, last_commit)) + '\n'.join(commits)
inikepbcb9aad2016-06-22 13:07:58 +020092
93
inikepc364ee72016-06-22 14:01:53 +020094def get_last_results(resultsFileName):
inikepbcb9aad2016-06-22 13:07:58 +020095 if not os.path.isfile(resultsFileName):
inikepa4847eb2016-07-19 17:59:53 +020096 return None, None, None, None
inikepbcb9aad2016-06-22 13:07:58 +020097 commit = None
inikepa4847eb2016-07-19 17:59:53 +020098 csize = []
inikepbcb9aad2016-06-22 13:07:58 +020099 cspeed = []
100 dspeed = []
101 with open(resultsFileName,'r') as f:
102 for line in f:
103 words = line.split()
104 if len(words) == 2: # branch + commit
105 commit = words[1];
inikepa4847eb2016-07-19 17:59:53 +0200106 csize = []
inikepbcb9aad2016-06-22 13:07:58 +0200107 cspeed = []
108 dspeed = []
109 if (len(words) == 8): # results
inikepa4847eb2016-07-19 17:59:53 +0200110 csize.append(int(words[1]))
inikepbcb9aad2016-06-22 13:07:58 +0200111 cspeed.append(float(words[3]))
112 dspeed.append(float(words[5]))
inikepa4847eb2016-07-19 17:59:53 +0200113 return commit, csize, cspeed, dspeed
inikepbcb9aad2016-06-22 13:07:58 +0200114
115
inikep2214e462016-07-26 13:05:01 +0200116def benchmark_and_compare(branch, commit, last_commit, args, executableName, resultsFileName, testFilePath, fileName, last_csize, last_cspeed, last_dspeed):
inikepbcb9aad2016-06-22 13:07:58 +0200117 sleepTime = 30
inikepa4847eb2016-07-19 17:59:53 +0200118 while os.getloadavg()[0] > args.maxLoadAvg:
119 log("WARNING: bench loadavg=%.2f is higher than %s, sleeping for %s seconds" % (os.getloadavg()[0], args.maxLoadAvg, sleepTime))
inikepbcb9aad2016-06-22 13:07:58 +0200120 time.sleep(sleepTime)
121 start_load = str(os.getloadavg())
inikep2214e462016-07-26 13:05:01 +0200122 result = execute('programs/%s -qi5b1e%s %s' % (executableName, args.lastCLevel, testFilePath), print_output=True)
inikepbcb9aad2016-06-22 13:07:58 +0200123 end_load = str(os.getloadavg())
inikepa4847eb2016-07-19 17:59:53 +0200124 linesExpected = args.lastCLevel + 2;
inikepbcb9aad2016-06-22 13:07:58 +0200125 if len(result) != linesExpected:
126 raise RuntimeError("ERROR: number of result lines=%d is different that expected %d\n%s" % (len(result), linesExpected, '\n'.join(result)))
127 with open(resultsFileName, "a") as myfile:
128 myfile.write(branch + " " + commit + "\n")
129 myfile.write('\n'.join(result) + '\n')
130 myfile.close()
131 if (last_cspeed == None):
132 log("WARNING: No data for comparison for branch=%s file=%s " % (branch, fileName))
133 return ""
inikepa4847eb2016-07-19 17:59:53 +0200134 commit, csize, cspeed, dspeed = get_last_results(resultsFileName)
inikepbcb9aad2016-06-22 13:07:58 +0200135 text = ""
136 for i in range(0, min(len(cspeed), len(last_cspeed))):
inikep164ce992016-07-25 10:35:53 +0200137 print("%s:%s -%d cSpeed=%6.2f cLast=%6.2f cDiff=%1.4f dSpeed=%6.2f dLast=%6.2f dDiff=%1.4f ratioDiff=%1.4f %s" % (branch, commit, i+1, cspeed[i], last_cspeed[i], cspeed[i]/last_cspeed[i], dspeed[i], last_dspeed[i], dspeed[i]/last_dspeed[i], float(last_csize[i])/csize[i], fileName))
inikepa4847eb2016-07-19 17:59:53 +0200138 if (cspeed[i]/last_cspeed[i] < args.lowerLimit):
inikep2214e462016-07-26 13:05:01 +0200139 text += "WARNING: %s -%d cSpeed=%.2f cLast=%.2f cDiff=%.4f %s\n" % (executableName, i+1, cspeed[i], last_cspeed[i], cspeed[i]/last_cspeed[i], fileName)
inikepa4847eb2016-07-19 17:59:53 +0200140 if (dspeed[i]/last_dspeed[i] < args.lowerLimit):
inikep2214e462016-07-26 13:05:01 +0200141 text += "WARNING: %s -%d dSpeed=%.2f dLast=%.2f dDiff=%.4f %s\n" % (executableName, i+1, dspeed[i], last_dspeed[i], dspeed[i]/last_dspeed[i], fileName)
inikep164ce992016-07-25 10:35:53 +0200142 if (float(last_csize[i])/csize[i] < args.ratioLimit):
inikep2214e462016-07-26 13:05:01 +0200143 text += "WARNING: %s -%d cSize=%d last_cSize=%d diff=%.4f %s\n" % (executableName, i+1, csize[i], last_csize[i], float(last_csize[i])/csize[i], fileName)
inikepbcb9aad2016-06-22 13:07:58 +0200144 if text:
inikep2214e462016-07-26 13:05:01 +0200145 text = args.message + ("\nmaxLoadAvg=%s load average at start=%s end=%s last_commit=%s\n" % (args.maxLoadAvg, start_load, end_load, last_commit)) + text
inikepbcb9aad2016-06-22 13:07:58 +0200146 return text
147
148
inikep116128c2016-06-22 18:12:57 +0200149def update_config_file(branch, commit):
150 last_commit = None
151 commitFileName = working_path + "/commit_" + branch.replace("/", "_") + ".txt"
152 if os.path.isfile(commitFileName):
153 last_commit = file(commitFileName, 'r').read()
154 file(commitFileName, 'w').write(commit)
155 return last_commit
inikep9470b872016-06-09 12:54:06 +0200156
inikep9470b872016-06-09 12:54:06 +0200157
inikep2214e462016-07-26 13:05:01 +0200158def double_check(branch, commit, args, executableName, resultsFileName, filePath, fileName):
159 last_commit, csize, cspeed, dspeed = get_last_results(resultsFileName)
160 if not args.dry_run:
161 text = benchmark_and_compare(branch, commit, last_commit, args, executableName, resultsFileName, filePath, fileName, csize, cspeed, dspeed)
162 if text:
163 log("WARNING: redoing tests for branch %s: commit %s" % (branch, commit))
164 text = benchmark_and_compare(branch, commit, last_commit, args, executableName, resultsFileName, filePath, fileName, csize, cspeed, dspeed)
165 return text
166
167
inikep116128c2016-06-22 18:12:57 +0200168def test_commit(branch, commit, last_commit, args, testFilePaths, have_mutt, have_mail):
inikep82babfc2016-06-22 20:06:42 +0200169 local_branch = string.split(branch, '/')[1]
170 version = local_branch.rpartition('-')[2] + '_' + commit
171 if not args.dry_run:
inikepc4b51062016-07-29 16:11:37 +0200172 execute('make -C programs clean zstd MOREFLAGS="-DZSTD_GIT_COMMIT=%s" && make -B -C programs zstd32 MOREFLAGS="-DZSTD_GIT_COMMIT=%s"' % (version, version))
inikep116128c2016-06-22 18:12:57 +0200173 logFileName = working_path + "/log_" + branch.replace("/", "_") + ".txt"
174 text_to_send = []
175 results_files = ""
176 for filePath in testFilePaths:
177 fileName = filePath.rpartition('/')[2]
178 resultsFileName = working_path + "/results_" + branch.replace("/", "_") + "_" + fileName.replace(".", "_") + ".txt"
inikep2214e462016-07-26 13:05:01 +0200179 text = double_check(branch, commit, args, 'zstd', resultsFileName, filePath, fileName)
180 if text:
181 text_to_send.append(text)
182 results_files += resultsFileName + " "
183 resultsFileName = working_path + "/results32_" + branch.replace("/", "_") + "_" + fileName.replace(".", "_") + ".txt"
184 text = double_check(branch, commit, args, 'zstd32', resultsFileName, filePath, fileName)
185 if text:
186 text_to_send.append(text)
187 results_files += resultsFileName + " "
inikep116128c2016-06-22 18:12:57 +0200188 if text_to_send:
inikepa4847eb2016-07-19 17:59:53 +0200189 send_email_with_attachments(branch, commit, last_commit, args, text_to_send, results_files, logFileName, have_mutt, have_mail)
inikep9470b872016-06-09 12:54:06 +0200190
191
192if __name__ == '__main__':
193 parser = argparse.ArgumentParser()
inikepc1b154a2016-06-10 12:53:12 +0200194 parser.add_argument('testFileNames', help='file names list for speed benchmark')
195 parser.add_argument('emails', help='list of e-mail addresses to send warnings')
inikep1e375f12016-06-13 10:50:09 +0200196 parser.add_argument('--message', help='attach an additional message to e-mail', default="")
inikepd731de82016-06-21 11:26:17 +0200197 parser.add_argument('--repoURL', help='changes default repository URL', default=default_repo_url)
inikepc1b154a2016-06-10 12:53:12 +0200198 parser.add_argument('--lowerLimit', type=float, help='send email if speed is lower than given limit', default=0.98)
inikepa4847eb2016-07-19 17:59:53 +0200199 parser.add_argument('--ratioLimit', type=float, help='send email if ratio is lower than given limit', default=0.999)
inikep9470b872016-06-09 12:54:06 +0200200 parser.add_argument('--maxLoadAvg', type=float, help='maximum load average to start testing', default=0.75)
201 parser.add_argument('--lastCLevel', type=int, help='last compression level for testing', default=5)
inikepc1b154a2016-06-10 12:53:12 +0200202 parser.add_argument('--sleepTime', type=int, help='frequency of repository checking in seconds', default=300)
inikep9470b872016-06-09 12:54:06 +0200203 parser.add_argument('--dry-run', dest='dry_run', action='store_true', help='not build', default=False)
inikep8c53ad52016-07-19 15:49:14 +0200204 parser.add_argument('--verbose', action='store_true', help='more verbose logs', default=False)
inikep9470b872016-06-09 12:54:06 +0200205 args = parser.parse_args()
inikep8c53ad52016-07-19 15:49:14 +0200206 verbose = args.verbose
inikep9470b872016-06-09 12:54:06 +0200207
208 # check if test files are accessible
209 testFileNames = args.testFileNames.split()
210 testFilePaths = []
211 for fileName in testFileNames:
inikep47020672016-06-22 17:11:01 +0200212 fileName = os.path.expanduser(fileName)
inikep9470b872016-06-09 12:54:06 +0200213 if os.path.isfile(fileName):
214 testFilePaths.append(os.path.abspath(fileName))
215 else:
inikepd731de82016-06-21 11:26:17 +0200216 log("ERROR: File not found: " + fileName)
217 exit(1)
inikep9470b872016-06-09 12:54:06 +0200218
inikepc1b154a2016-06-10 12:53:12 +0200219 # check availability of e-mail senders
inikep2d9272f2016-06-21 19:28:51 +0200220 have_mutt = does_command_exist("mutt -h");
inikepc1b154a2016-06-10 12:53:12 +0200221 have_mail = does_command_exist("mail -V");
inikepf1690292016-06-10 13:59:08 +0200222 if not have_mutt and not have_mail:
inikepd731de82016-06-21 11:26:17 +0200223 log("ERROR: e-mail senders 'mail' or 'mutt' not found")
224 exit(1)
inikepc1b154a2016-06-10 12:53:12 +0200225
inikep8c53ad52016-07-19 15:49:14 +0200226 if verbose:
227 print("PARAMETERS:\nrepoURL=%s" % args.repoURL)
228 print("working_path=%s" % working_path)
229 print("clone_path=%s" % clone_path)
230 print("testFilePath(%s)=%s" % (len(testFilePaths), testFilePaths))
231 print("message=%s" % args.message)
232 print("emails=%s" % args.emails)
233 print("maxLoadAvg=%s" % args.maxLoadAvg)
234 print("lowerLimit=%s" % args.lowerLimit)
inikepa4847eb2016-07-19 17:59:53 +0200235 print("ratioLimit=%s" % args.ratioLimit)
inikep8c53ad52016-07-19 15:49:14 +0200236 print("lastCLevel=%s" % args.lastCLevel)
237 print("sleepTime=%s" % args.sleepTime)
238 print("dry_run=%s" % args.dry_run)
239 print("verbose=%s" % args.verbose)
240 print("have_mutt=%s have_mail=%s" % (have_mutt, have_mail))
inikepc1b154a2016-06-10 12:53:12 +0200241
inikepd731de82016-06-21 11:26:17 +0200242 # clone ZSTD repo if needed
inikep95da7432016-06-22 12:12:35 +0200243 if not os.path.isdir(working_path):
244 os.mkdir(working_path)
inikepd731de82016-06-21 11:26:17 +0200245 if not os.path.isdir(clone_path):
inikep95da7432016-06-22 12:12:35 +0200246 execute.cwd = working_path
inikepd731de82016-06-21 11:26:17 +0200247 execute('git clone ' + args.repoURL)
248 if not os.path.isdir(clone_path):
249 log("ERROR: ZSTD clone not found: " + clone_path)
250 exit(1)
251 execute.cwd = clone_path
252
253 # check if speedTest.pid already exists
inikepd731de82016-06-21 11:26:17 +0200254 pidfile = "./speedTest.pid"
255 if os.path.isfile(pidfile):
256 log("ERROR: %s already exists, exiting" % pidfile)
257 exit(1)
258
inikep47020672016-06-22 17:11:01 +0200259 send_email(args.emails, email_header + ':%s test-zstd-speed.py has been started' % pid, args.message, have_mutt, have_mail)
inikep95da7432016-06-22 12:12:35 +0200260 file(pidfile, 'w').write(pid)
inikepc364ee72016-06-22 14:01:53 +0200261
inikep9470b872016-06-09 12:54:06 +0200262 while True:
inikepd731de82016-06-21 11:26:17 +0200263 try:
264 loadavg = os.getloadavg()[0]
265 if (loadavg <= args.maxLoadAvg):
inikepbcb9aad2016-06-22 13:07:58 +0200266 branches = git_get_branches()
inikep95da7432016-06-22 12:12:35 +0200267 for branch in branches:
inikep8c53ad52016-07-19 15:49:14 +0200268 commit = execute('git show -s --format=%h ' + branch, verbose)[0]
inikep116128c2016-06-22 18:12:57 +0200269 last_commit = update_config_file(branch, commit)
270 if commit == last_commit:
271 log("skipping branch %s: head %s already processed" % (branch, commit))
272 else:
273 log("build branch %s: head %s is different from prev %s" % (branch, commit, last_commit))
inikep82babfc2016-06-22 20:06:42 +0200274 execute('git checkout -- . && git checkout ' + branch)
275 print(git_get_changes(branch, commit, last_commit))
inikep116128c2016-06-22 18:12:57 +0200276 test_commit(branch, commit, last_commit, args, testFilePaths, have_mutt, have_mail)
inikepd731de82016-06-21 11:26:17 +0200277 else:
278 log("WARNING: main loadavg=%.2f is higher than %s" % (loadavg, args.maxLoadAvg))
inikep8c53ad52016-07-19 15:49:14 +0200279 if verbose:
280 log("sleep for %s seconds" % args.sleepTime)
inikep95da7432016-06-22 12:12:35 +0200281 time.sleep(args.sleepTime)
inikep116128c2016-06-22 18:12:57 +0200282 except Exception as e:
283 stack = traceback.format_exc()
284 email_topic = '%s:%s ERROR in %s:%s' % (email_header, pid, branch, commit)
285 send_email(args.emails, email_topic, stack, have_mutt, have_mail)
286 print(stack)
inikepc4b51062016-07-29 16:11:37 +0200287 time.sleep(args.sleepTime)
inikepc364ee72016-06-22 14:01:53 +0200288 except KeyboardInterrupt:
inikepd731de82016-06-21 11:26:17 +0200289 os.unlink(pidfile)
inikep47020672016-06-22 17:11:01 +0200290 send_email(args.emails, email_header + ':%s test-zstd-speed.py has been stopped' % pid, args.message, have_mutt, have_mail)
inikepc364ee72016-06-22 14:01:53 +0200291 exit(0)