blob: 075dd472badbab6db18e969bed0ff53d36a3f9c2 [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())
18
inikep9470b872016-06-09 12:54:06 +020019
20def log(text):
inikep2d9272f2016-06-21 19:28:51 +020021 print(time.strftime("%Y/%m/%d %H:%M:%S") + ' - ' + text)
inikep9470b872016-06-09 12:54:06 +020022
inikep2d9272f2016-06-21 19:28:51 +020023
24def execute(command, print_output=False, print_error=True, param_shell=True):
inikep9470b872016-06-09 12:54:06 +020025 log("> " + command)
inikep95da7432016-06-22 12:12:35 +020026 popen = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=param_shell, cwd=execute.cwd)
27 stdout = popen.communicate()[0]
28 stdout_lines = stdout.splitlines()
inikep2d9272f2016-06-21 19:28:51 +020029 if print_output:
inikep95da7432016-06-22 12:12:35 +020030 print('\n'.join(stdout_lines))
inikep9470b872016-06-09 12:54:06 +020031 if popen.returncode is not None and popen.returncode != 0:
inikepc1b154a2016-06-10 12:53:12 +020032 if not print_output and print_error:
inikep95da7432016-06-22 12:12:35 +020033 print('\n'.join(stdout_lines))
34 raise RuntimeError('\n'.join(stdout_lines))
35 return stdout_lines
inikep9470b872016-06-09 12:54:06 +020036execute.cwd = None
37
38
inikepc1b154a2016-06-10 12:53:12 +020039def does_command_exist(command):
inikep95da7432016-06-22 12:12:35 +020040 try:
41 execute(command, False, False);
42 except Exception as e:
43 return False
44 return True
inikepc1b154a2016-06-10 12:53:12 +020045
46
inikep95da7432016-06-22 12:12:35 +020047def send_email(emails, topic, text, have_mutt, have_mail):
48 logFileName = working_path + '/' + 'tmpEmailContent'
inikep9470b872016-06-09 12:54:06 +020049 with open(logFileName, "w") as myfile:
50 myfile.writelines(text)
51 myfile.close()
inikepc1b154a2016-06-10 12:53:12 +020052 if have_mutt:
inikep95da7432016-06-22 12:12:35 +020053 execute('mutt -s "' + topic + '" ' + emails + ' < ' + logFileName)
inikepc1b154a2016-06-10 12:53:12 +020054 elif have_mail:
inikep95da7432016-06-22 12:12:35 +020055 execute('mail -s "' + topic + '" ' + emails + ' < ' + logFileName)
inikepc1b154a2016-06-10 12:53:12 +020056 else:
inikep95da7432016-06-22 12:12:35 +020057 log("e-mail cannot be sent (mail or mutt not found)")
inikep9470b872016-06-09 12:54:06 +020058
59
inikep95da7432016-06-22 12:12:35 +020060def send_email_with_attachments(branch, commit, last_commit, emails, text, results_files, logFileName, lower_limit, have_mutt, have_mail):
61 with open(logFileName, "w") as myfile:
62 myfile.writelines(text)
63 myfile.close()
inikepbcb9aad2016-06-22 13:07:58 +020064 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 +020065 if have_mutt:
66 execute('mutt -s "' + email_topic + '" ' + emails + ' -a ' + results_files + ' < ' + logFileName)
67 elif have_mail:
68 execute('mail -s "' + email_topic + '" ' + emails + ' < ' + logFileName)
69 else:
70 log("e-mail cannot be sent (mail or mutt not found)")
71
72
inikepbcb9aad2016-06-22 13:07:58 +020073def git_get_branches():
74 execute('git fetch -p')
75 output = execute('git branch -rl')
76 for line in output:
77 if "HEAD" in line:
78 output.remove(line) # remove "origin/HEAD -> origin/dev"
79 return map(lambda l: l.strip(), output)
80
81
inikepf2f59d72016-06-22 15:42:26 +020082def git_get_changes(branch, commit, last_commit):
inikepbcb9aad2016-06-22 13:07:58 +020083 fmt = '--format="%h: (%an) %s, %ar"'
84 if last_commit is None:
85 commits = execute('git log -n 10 %s %s' % (fmt, commit))
86 else:
87 commits = execute('git --no-pager log %s %s..%s' % (fmt, last_commit, commit))
inikepf2f59d72016-06-22 15:42:26 +020088 return str('Changes in %s since %s:\n' % (branch, last_commit)) + '\n'.join(commits)
inikepbcb9aad2016-06-22 13:07:58 +020089
90
91def compile(branch, commit, last_commit, dry_run):
92 local_branch = string.split(branch, '/')[1]
93 version = local_branch.rpartition('-')[2]
94 version = version + '_' + commit
95 execute('git checkout -- . && git checkout ' + branch)
inikepf2f59d72016-06-22 15:42:26 +020096 print(git_get_changes(branch, commit, last_commit))
inikepbcb9aad2016-06-22 13:07:58 +020097 if not dry_run:
inikepf2f59d72016-06-22 15:42:26 +020098 execute('make clean zstdprogram MOREFLAGS="-DZSTD_GIT_COMMIT=%s"' % version, print_output=True)
inikepbcb9aad2016-06-22 13:07:58 +020099
100
inikepc364ee72016-06-22 14:01:53 +0200101def get_last_results(resultsFileName):
inikepbcb9aad2016-06-22 13:07:58 +0200102 if not os.path.isfile(resultsFileName):
103 return None, None, None
104 commit = None
105 cspeed = []
106 dspeed = []
107 with open(resultsFileName,'r') as f:
108 for line in f:
109 words = line.split()
110 if len(words) == 2: # branch + commit
111 commit = words[1];
112 cspeed = []
113 dspeed = []
114 if (len(words) == 8): # results
115 cspeed.append(float(words[3]))
116 dspeed.append(float(words[5]))
117 return commit, cspeed, dspeed
118
119
120def benchmark_and_compare(branch, commit, resultsFileName, lastCLevel, testFilePath, fileName, last_cspeed, last_dspeed, lower_limit, maxLoadAvg, message):
121 sleepTime = 30
122 while os.getloadavg()[0] > maxLoadAvg:
123 log("WARNING: bench loadavg=%.2f is higher than %s, sleeping for %s seconds" % (os.getloadavg()[0], maxLoadAvg, sleepTime))
124 time.sleep(sleepTime)
125 start_load = str(os.getloadavg())
126 result = execute('programs/zstd -qi5b1e' + str(lastCLevel) + ' ' + testFilePath, print_output=True)
127 end_load = str(os.getloadavg())
128 linesExpected = lastCLevel + 2;
129 if len(result) != linesExpected:
130 raise RuntimeError("ERROR: number of result lines=%d is different that expected %d\n%s" % (len(result), linesExpected, '\n'.join(result)))
131 with open(resultsFileName, "a") as myfile:
132 myfile.write(branch + " " + commit + "\n")
133 myfile.write('\n'.join(result) + '\n')
134 myfile.close()
135 if (last_cspeed == None):
136 log("WARNING: No data for comparison for branch=%s file=%s " % (branch, fileName))
137 return ""
inikepc364ee72016-06-22 14:01:53 +0200138 commit, cspeed, dspeed = get_last_results(resultsFileName)
inikepbcb9aad2016-06-22 13:07:58 +0200139 text = ""
140 for i in range(0, min(len(cspeed), len(last_cspeed))):
141 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))
142 if (cspeed[i]/last_cspeed[i] < lower_limit):
143 text += "WARNING: -%d cspeed=%.2f clast=%.2f cdiff=%.4f %s\n" % (i+1, cspeed[i], last_cspeed[i], cspeed[i]/last_cspeed[i], fileName)
144 if (dspeed[i]/last_dspeed[i] < lower_limit):
145 text += "WARNING: -%d dspeed=%.2f dlast=%.2f ddiff=%.4f %s\n" % (i+1, dspeed[i], last_dspeed[i], dspeed[i]/last_dspeed[i], fileName)
146 if text:
147 text = message + ("\nmaxLoadAvg=%s load average at start=%s end=%s\n" % (maxLoadAvg, start_load, end_load)) + text
148 return text
149
150
inikep95da7432016-06-22 12:12:35 +0200151def check_branch(branch, args, testFilePaths, have_mutt, have_mail):
152 commits = execute('git show -s --format=%h ' + branch)[0]
153 for commit in [commits]:
inikep9470b872016-06-09 12:54:06 +0200154 try:
inikepc364ee72016-06-22 14:01:53 +0200155 last_commit = None
inikep95da7432016-06-22 12:12:35 +0200156 commitFileName = working_path + "/commit_" + branch.replace("/", "_")
inikep9470b872016-06-09 12:54:06 +0200157 if os.path.isfile(commitFileName):
158 last_commit = file(commitFileName, 'r').read()
inikep9470b872016-06-09 12:54:06 +0200159 file(commitFileName, 'w').write(commit)
160
161 if commit == last_commit:
162 log("skipping branch %s: head %s already processed" % (branch, commit))
inikepc364ee72016-06-22 14:01:53 +0200163 continue
inikep9470b872016-06-09 12:54:06 +0200164
inikepc364ee72016-06-22 14:01:53 +0200165 log("build branch %s: head %s is different from prev %s" % (branch, commit, last_commit))
166 compile(branch, commit, last_commit, args.dry_run)
inikep9470b872016-06-09 12:54:06 +0200167
inikepc364ee72016-06-22 14:01:53 +0200168 logFileName = working_path + "/log_" + branch.replace("/", "_")
169 text_to_send = []
170 results_files = ""
171 for filePath in testFilePaths:
172 fileName = filePath.rpartition('/')[2]
173 resultsFileName = working_path + "/results_" + branch.replace("/", "_") + "_" + fileName
174 last_commit, cspeed, dspeed = get_last_results(resultsFileName)
175
176 if not args.dry_run:
177 text = benchmark_and_compare(branch, commit, resultsFileName, args.lastCLevel, filePath, fileName, cspeed, dspeed, args.lowerLimit, args.maxLoadAvg, args.message)
178 if text:
179 log("WARNING: redoing tests for branch %s: commit %s" % (branch, commit))
inikepc1b154a2016-06-10 12:53:12 +0200180 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 +0200181 if text:
inikepc364ee72016-06-22 14:01:53 +0200182 text_to_send.append(text)
183 results_files += resultsFileName + " "
184 if text_to_send:
185 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 +0200186 except Exception as e:
187 stack = traceback.format_exc()
inikepbcb9aad2016-06-22 13:07:58 +0200188 email_topic = '%s:%s ERROR in %s:%s' % (email_header, pid, branch, commit)
inikepc364ee72016-06-22 14:01:53 +0200189 send_email(args.emails, email_topic, stack, have_mutt, have_mail)
inikep2d9272f2016-06-21 19:28:51 +0200190 print(stack)
inikep9470b872016-06-09 12:54:06 +0200191
192
193if __name__ == '__main__':
194 parser = argparse.ArgumentParser()
inikepc1b154a2016-06-10 12:53:12 +0200195 parser.add_argument('testFileNames', help='file names list for speed benchmark')
196 parser.add_argument('emails', help='list of e-mail addresses to send warnings')
inikep1e375f12016-06-13 10:50:09 +0200197 parser.add_argument('--message', help='attach an additional message to e-mail', default="")
inikepd731de82016-06-21 11:26:17 +0200198 parser.add_argument('--repoURL', help='changes default repository URL', default=default_repo_url)
inikepc1b154a2016-06-10 12:53:12 +0200199 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 +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)
204 args = parser.parse_args()
205
206 # check if test files are accessible
207 testFileNames = args.testFileNames.split()
208 testFilePaths = []
209 for fileName in testFileNames:
210 if os.path.isfile(fileName):
211 testFilePaths.append(os.path.abspath(fileName))
212 else:
inikepd731de82016-06-21 11:26:17 +0200213 log("ERROR: File not found: " + fileName)
214 exit(1)
inikep9470b872016-06-09 12:54:06 +0200215
inikepc1b154a2016-06-10 12:53:12 +0200216 # check availability of e-mail senders
inikep2d9272f2016-06-21 19:28:51 +0200217 have_mutt = does_command_exist("mutt -h");
inikepc1b154a2016-06-10 12:53:12 +0200218 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
inikep2d9272f2016-06-21 19:28:51 +0200223 print("PARAMETERS:\nrepoURL=%s" % args.repoURL)
inikep95da7432016-06-22 12:12:35 +0200224 print("working_path=%s" % working_path)
inikep2d9272f2016-06-21 19:28:51 +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))
inikepc1b154a2016-06-10 12:53:12 +0200235
inikepd731de82016-06-21 11:26:17 +0200236 # clone ZSTD repo if needed
inikep95da7432016-06-22 12:12:35 +0200237 if not os.path.isdir(working_path):
238 os.mkdir(working_path)
inikepd731de82016-06-21 11:26:17 +0200239 if not os.path.isdir(clone_path):
inikep95da7432016-06-22 12:12:35 +0200240 execute.cwd = working_path
inikepd731de82016-06-21 11:26:17 +0200241 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
inikepd731de82016-06-21 11:26:17 +0200248 pidfile = "./speedTest.pid"
249 if os.path.isfile(pidfile):
250 log("ERROR: %s already exists, exiting" % pidfile)
251 exit(1)
252
inikepc364ee72016-06-22 14:01:53 +0200253 send_email(args.emails, email_header + ':%s test-zstd-speed.py has been started' % pid, '', have_mutt, have_mail)
inikep95da7432016-06-22 12:12:35 +0200254 file(pidfile, 'w').write(pid)
inikepc364ee72016-06-22 14:01:53 +0200255
inikep9470b872016-06-09 12:54:06 +0200256 while True:
inikepd731de82016-06-21 11:26:17 +0200257 try:
258 loadavg = os.getloadavg()[0]
259 if (loadavg <= args.maxLoadAvg):
inikepbcb9aad2016-06-22 13:07:58 +0200260 branches = git_get_branches()
inikep95da7432016-06-22 12:12:35 +0200261 for branch in branches:
262 check_branch(branch, args, testFilePaths, have_mutt, have_mail)
inikepd731de82016-06-21 11:26:17 +0200263 else:
264 log("WARNING: main loadavg=%.2f is higher than %s" % (loadavg, args.maxLoadAvg))
inikep95da7432016-06-22 12:12:35 +0200265 log("sleep for %s seconds" % args.sleepTime)
266 time.sleep(args.sleepTime)
inikepc364ee72016-06-22 14:01:53 +0200267 except KeyboardInterrupt:
inikepd731de82016-06-21 11:26:17 +0200268 os.unlink(pidfile)
inikepc364ee72016-06-22 14:01:53 +0200269 send_email(args.emails, email_header + ':%s test-zstd-speed.py has been stopped' % pid, '', have_mutt, have_mail)
270 exit(0)