blob: fa9ce0da90e1b4a44fdfcd541f95a6c0ecacfe06 [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
10
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'
14working_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
inikep95da7432016-06-22 12:12:35 +020062def send_email_with_attachments(branch, commit, last_commit, emails, text, results_files, logFileName, lower_limit, have_mutt, have_mail):
63 with open(logFileName, "w") as myfile:
64 myfile.writelines(text)
65 myfile.close()
inikepbcb9aad2016-06-22 13:07:58 +020066 email_topic = '%s:%s Warning for %s:%s last_commit=%s speed<%s' % (email_header, pid, branch, commit, last_commit, lower_limit)
inikep95da7432016-06-22 12:12:35 +020067 if have_mutt:
68 execute('mutt -s "' + email_topic + '" ' + emails + ' -a ' + results_files + ' < ' + logFileName)
69 elif have_mail:
70 execute('mail -s "' + email_topic + '" ' + emails + ' < ' + logFileName)
71 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):
96 return None, None, None
97 commit = None
98 cspeed = []
99 dspeed = []
100 with open(resultsFileName,'r') as f:
101 for line in f:
102 words = line.split()
103 if len(words) == 2: # branch + commit
104 commit = words[1];
105 cspeed = []
106 dspeed = []
107 if (len(words) == 8): # results
108 cspeed.append(float(words[3]))
109 dspeed.append(float(words[5]))
110 return commit, cspeed, dspeed
111
112
113def benchmark_and_compare(branch, commit, resultsFileName, lastCLevel, testFilePath, fileName, last_cspeed, last_dspeed, lower_limit, maxLoadAvg, message):
114 sleepTime = 30
115 while os.getloadavg()[0] > maxLoadAvg:
116 log("WARNING: bench loadavg=%.2f is higher than %s, sleeping for %s seconds" % (os.getloadavg()[0], maxLoadAvg, sleepTime))
117 time.sleep(sleepTime)
118 start_load = str(os.getloadavg())
inikep116128c2016-06-22 18:12:57 +0200119 result = execute('programs/zstd -qi5b1e%s %s' % (lastCLevel, testFilePath), print_output=True)
inikepbcb9aad2016-06-22 13:07:58 +0200120 end_load = str(os.getloadavg())
121 linesExpected = lastCLevel + 2;
122 if len(result) != linesExpected:
123 raise RuntimeError("ERROR: number of result lines=%d is different that expected %d\n%s" % (len(result), linesExpected, '\n'.join(result)))
124 with open(resultsFileName, "a") as myfile:
125 myfile.write(branch + " " + commit + "\n")
126 myfile.write('\n'.join(result) + '\n')
127 myfile.close()
128 if (last_cspeed == None):
129 log("WARNING: No data for comparison for branch=%s file=%s " % (branch, fileName))
130 return ""
inikepc364ee72016-06-22 14:01:53 +0200131 commit, cspeed, dspeed = get_last_results(resultsFileName)
inikepbcb9aad2016-06-22 13:07:58 +0200132 text = ""
133 for i in range(0, min(len(cspeed), len(last_cspeed))):
134 print("%s:%s -%d cspeed=%6.2f clast=%6.2f cdiff=%1.4f dspeed=%6.2f dlast=%6.2f ddiff=%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], fileName))
135 if (cspeed[i]/last_cspeed[i] < lower_limit):
136 text += "WARNING: -%d cspeed=%.2f clast=%.2f cdiff=%.4f %s\n" % (i+1, cspeed[i], last_cspeed[i], cspeed[i]/last_cspeed[i], fileName)
137 if (dspeed[i]/last_dspeed[i] < lower_limit):
138 text += "WARNING: -%d dspeed=%.2f dlast=%.2f ddiff=%.4f %s\n" % (i+1, dspeed[i], last_dspeed[i], dspeed[i]/last_dspeed[i], fileName)
139 if text:
140 text = message + ("\nmaxLoadAvg=%s load average at start=%s end=%s\n" % (maxLoadAvg, start_load, end_load)) + text
141 return text
142
143
inikep116128c2016-06-22 18:12:57 +0200144def update_config_file(branch, commit):
145 last_commit = None
146 commitFileName = working_path + "/commit_" + branch.replace("/", "_") + ".txt"
147 if os.path.isfile(commitFileName):
148 last_commit = file(commitFileName, 'r').read()
149 file(commitFileName, 'w').write(commit)
150 return last_commit
inikep9470b872016-06-09 12:54:06 +0200151
inikep9470b872016-06-09 12:54:06 +0200152
inikep116128c2016-06-22 18:12:57 +0200153def test_commit(branch, commit, last_commit, args, testFilePaths, have_mutt, have_mail):
inikep82babfc2016-06-22 20:06:42 +0200154 local_branch = string.split(branch, '/')[1]
155 version = local_branch.rpartition('-')[2] + '_' + commit
156 if not args.dry_run:
157 execute('make clean zstdprogram MOREFLAGS="-DZSTD_GIT_COMMIT=%s"' % version)
inikep116128c2016-06-22 18:12:57 +0200158 logFileName = working_path + "/log_" + branch.replace("/", "_") + ".txt"
159 text_to_send = []
160 results_files = ""
161 for filePath in testFilePaths:
162 fileName = filePath.rpartition('/')[2]
163 resultsFileName = working_path + "/results_" + branch.replace("/", "_") + "_" + fileName.replace(".", "_") + ".txt"
164 last_commit, cspeed, dspeed = get_last_results(resultsFileName)
inikep116128c2016-06-22 18:12:57 +0200165 if not args.dry_run:
166 text = benchmark_and_compare(branch, commit, resultsFileName, args.lastCLevel, filePath, fileName, cspeed, dspeed, args.lowerLimit, args.maxLoadAvg, args.message)
167 if text:
168 log("WARNING: redoing tests for branch %s: commit %s" % (branch, commit))
inikepd7d251c2016-06-22 16:13:25 +0200169 text = benchmark_and_compare(branch, commit, resultsFileName, args.lastCLevel, filePath, fileName, cspeed, dspeed, args.lowerLimit, args.maxLoadAvg, args.message)
170 if text:
inikep116128c2016-06-22 18:12:57 +0200171 text_to_send.append(text)
172 results_files += resultsFileName + " "
173 if text_to_send:
174 send_email_with_attachments(branch, commit, last_commit, args.emails, text_to_send, results_files, logFileName, args.lowerLimit, have_mutt, have_mail)
inikep9470b872016-06-09 12:54:06 +0200175
176
177if __name__ == '__main__':
178 parser = argparse.ArgumentParser()
inikepc1b154a2016-06-10 12:53:12 +0200179 parser.add_argument('testFileNames', help='file names list for speed benchmark')
180 parser.add_argument('emails', help='list of e-mail addresses to send warnings')
inikep1e375f12016-06-13 10:50:09 +0200181 parser.add_argument('--message', help='attach an additional message to e-mail', default="")
inikepd731de82016-06-21 11:26:17 +0200182 parser.add_argument('--repoURL', help='changes default repository URL', default=default_repo_url)
inikepc1b154a2016-06-10 12:53:12 +0200183 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 +0200184 parser.add_argument('--maxLoadAvg', type=float, help='maximum load average to start testing', default=0.75)
185 parser.add_argument('--lastCLevel', type=int, help='last compression level for testing', default=5)
inikepc1b154a2016-06-10 12:53:12 +0200186 parser.add_argument('--sleepTime', type=int, help='frequency of repository checking in seconds', default=300)
inikep9470b872016-06-09 12:54:06 +0200187 parser.add_argument('--dry-run', dest='dry_run', action='store_true', help='not build', default=False)
inikep8c53ad52016-07-19 15:49:14 +0200188 parser.add_argument('--verbose', action='store_true', help='more verbose logs', default=False)
inikep9470b872016-06-09 12:54:06 +0200189 args = parser.parse_args()
inikep8c53ad52016-07-19 15:49:14 +0200190 verbose = args.verbose
inikep9470b872016-06-09 12:54:06 +0200191
192 # check if test files are accessible
193 testFileNames = args.testFileNames.split()
194 testFilePaths = []
195 for fileName in testFileNames:
inikep47020672016-06-22 17:11:01 +0200196 fileName = os.path.expanduser(fileName)
inikep9470b872016-06-09 12:54:06 +0200197 if os.path.isfile(fileName):
198 testFilePaths.append(os.path.abspath(fileName))
199 else:
inikepd731de82016-06-21 11:26:17 +0200200 log("ERROR: File not found: " + fileName)
201 exit(1)
inikep9470b872016-06-09 12:54:06 +0200202
inikepc1b154a2016-06-10 12:53:12 +0200203 # check availability of e-mail senders
inikep2d9272f2016-06-21 19:28:51 +0200204 have_mutt = does_command_exist("mutt -h");
inikepc1b154a2016-06-10 12:53:12 +0200205 have_mail = does_command_exist("mail -V");
inikepf1690292016-06-10 13:59:08 +0200206 if not have_mutt and not have_mail:
inikepd731de82016-06-21 11:26:17 +0200207 log("ERROR: e-mail senders 'mail' or 'mutt' not found")
208 exit(1)
inikepc1b154a2016-06-10 12:53:12 +0200209
inikep8c53ad52016-07-19 15:49:14 +0200210 if verbose:
211 print("PARAMETERS:\nrepoURL=%s" % args.repoURL)
212 print("working_path=%s" % working_path)
213 print("clone_path=%s" % clone_path)
214 print("testFilePath(%s)=%s" % (len(testFilePaths), testFilePaths))
215 print("message=%s" % args.message)
216 print("emails=%s" % args.emails)
217 print("maxLoadAvg=%s" % args.maxLoadAvg)
218 print("lowerLimit=%s" % args.lowerLimit)
219 print("lastCLevel=%s" % args.lastCLevel)
220 print("sleepTime=%s" % args.sleepTime)
221 print("dry_run=%s" % args.dry_run)
222 print("verbose=%s" % args.verbose)
223 print("have_mutt=%s have_mail=%s" % (have_mutt, have_mail))
inikepc1b154a2016-06-10 12:53:12 +0200224
inikepd731de82016-06-21 11:26:17 +0200225 # clone ZSTD repo if needed
inikep95da7432016-06-22 12:12:35 +0200226 if not os.path.isdir(working_path):
227 os.mkdir(working_path)
inikepd731de82016-06-21 11:26:17 +0200228 if not os.path.isdir(clone_path):
inikep95da7432016-06-22 12:12:35 +0200229 execute.cwd = working_path
inikepd731de82016-06-21 11:26:17 +0200230 execute('git clone ' + args.repoURL)
231 if not os.path.isdir(clone_path):
232 log("ERROR: ZSTD clone not found: " + clone_path)
233 exit(1)
234 execute.cwd = clone_path
235
236 # check if speedTest.pid already exists
inikepd731de82016-06-21 11:26:17 +0200237 pidfile = "./speedTest.pid"
238 if os.path.isfile(pidfile):
239 log("ERROR: %s already exists, exiting" % pidfile)
240 exit(1)
241
inikep47020672016-06-22 17:11:01 +0200242 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 +0200243 file(pidfile, 'w').write(pid)
inikepc364ee72016-06-22 14:01:53 +0200244
inikep9470b872016-06-09 12:54:06 +0200245 while True:
inikepd731de82016-06-21 11:26:17 +0200246 try:
247 loadavg = os.getloadavg()[0]
248 if (loadavg <= args.maxLoadAvg):
inikepbcb9aad2016-06-22 13:07:58 +0200249 branches = git_get_branches()
inikep95da7432016-06-22 12:12:35 +0200250 for branch in branches:
inikep8c53ad52016-07-19 15:49:14 +0200251 commit = execute('git show -s --format=%h ' + branch, verbose)[0]
inikep116128c2016-06-22 18:12:57 +0200252 last_commit = update_config_file(branch, commit)
253 if commit == last_commit:
254 log("skipping branch %s: head %s already processed" % (branch, commit))
255 else:
256 log("build branch %s: head %s is different from prev %s" % (branch, commit, last_commit))
inikep82babfc2016-06-22 20:06:42 +0200257 execute('git checkout -- . && git checkout ' + branch)
258 print(git_get_changes(branch, commit, last_commit))
inikep116128c2016-06-22 18:12:57 +0200259 test_commit(branch, commit, last_commit, args, testFilePaths, have_mutt, have_mail)
inikepd731de82016-06-21 11:26:17 +0200260 else:
261 log("WARNING: main loadavg=%.2f is higher than %s" % (loadavg, args.maxLoadAvg))
inikep8c53ad52016-07-19 15:49:14 +0200262 if verbose:
263 log("sleep for %s seconds" % args.sleepTime)
inikep95da7432016-06-22 12:12:35 +0200264 time.sleep(args.sleepTime)
inikep116128c2016-06-22 18:12:57 +0200265 except Exception as e:
266 stack = traceback.format_exc()
267 email_topic = '%s:%s ERROR in %s:%s' % (email_header, pid, branch, commit)
268 send_email(args.emails, email_topic, stack, have_mutt, have_mail)
269 print(stack)
inikepc364ee72016-06-22 14:01:53 +0200270 except KeyboardInterrupt:
inikepd731de82016-06-21 11:26:17 +0200271 os.unlink(pidfile)
inikep47020672016-06-22 17:11:01 +0200272 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 +0200273 exit(0)