blob: 2ef4d6ba5657b62ea19be86bc797fdeb97e14680 [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:
inikepd7d251c2016-06-22 16:13:25 +020098 execute('make clean zstdprogram MOREFLAGS="-DZSTD_GIT_COMMIT=%s"' % version)
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())
inikep116128c2016-06-22 18:12:57 +0200126 result = execute('programs/zstd -qi5b1e%s %s' % (lastCLevel, testFilePath), print_output=True)
inikepbcb9aad2016-06-22 13:07:58 +0200127 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
inikep116128c2016-06-22 18:12:57 +0200151def update_config_file(branch, commit):
152 last_commit = None
153 commitFileName = working_path + "/commit_" + branch.replace("/", "_") + ".txt"
154 if os.path.isfile(commitFileName):
155 last_commit = file(commitFileName, 'r').read()
156 file(commitFileName, 'w').write(commit)
157 return last_commit
inikep9470b872016-06-09 12:54:06 +0200158
inikep9470b872016-06-09 12:54:06 +0200159
inikep116128c2016-06-22 18:12:57 +0200160def test_commit(branch, commit, last_commit, args, testFilePaths, have_mutt, have_mail):
161 compile(branch, commit, last_commit, args.dry_run)
162 logFileName = working_path + "/log_" + branch.replace("/", "_") + ".txt"
163 text_to_send = []
164 results_files = ""
165 for filePath in testFilePaths:
166 fileName = filePath.rpartition('/')[2]
167 resultsFileName = working_path + "/results_" + branch.replace("/", "_") + "_" + fileName.replace(".", "_") + ".txt"
168 last_commit, cspeed, dspeed = get_last_results(resultsFileName)
inikep9470b872016-06-09 12:54:06 +0200169
inikep116128c2016-06-22 18:12:57 +0200170 if not args.dry_run:
171 text = benchmark_and_compare(branch, commit, resultsFileName, args.lastCLevel, filePath, fileName, cspeed, dspeed, args.lowerLimit, args.maxLoadAvg, args.message)
172 if text:
173 log("WARNING: redoing tests for branch %s: commit %s" % (branch, commit))
inikepd7d251c2016-06-22 16:13:25 +0200174 text = benchmark_and_compare(branch, commit, resultsFileName, args.lastCLevel, filePath, fileName, cspeed, dspeed, args.lowerLimit, args.maxLoadAvg, args.message)
175 if text:
inikep116128c2016-06-22 18:12:57 +0200176 text_to_send.append(text)
177 results_files += resultsFileName + " "
178 if text_to_send:
179 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 +0200180
181
182if __name__ == '__main__':
183 parser = argparse.ArgumentParser()
inikepc1b154a2016-06-10 12:53:12 +0200184 parser.add_argument('testFileNames', help='file names list for speed benchmark')
185 parser.add_argument('emails', help='list of e-mail addresses to send warnings')
inikep1e375f12016-06-13 10:50:09 +0200186 parser.add_argument('--message', help='attach an additional message to e-mail', default="")
inikepd731de82016-06-21 11:26:17 +0200187 parser.add_argument('--repoURL', help='changes default repository URL', default=default_repo_url)
inikepc1b154a2016-06-10 12:53:12 +0200188 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 +0200189 parser.add_argument('--maxLoadAvg', type=float, help='maximum load average to start testing', default=0.75)
190 parser.add_argument('--lastCLevel', type=int, help='last compression level for testing', default=5)
inikepc1b154a2016-06-10 12:53:12 +0200191 parser.add_argument('--sleepTime', type=int, help='frequency of repository checking in seconds', default=300)
inikep9470b872016-06-09 12:54:06 +0200192 parser.add_argument('--dry-run', dest='dry_run', action='store_true', help='not build', default=False)
193 args = parser.parse_args()
194
195 # check if test files are accessible
196 testFileNames = args.testFileNames.split()
197 testFilePaths = []
198 for fileName in testFileNames:
inikep47020672016-06-22 17:11:01 +0200199 fileName = os.path.expanduser(fileName)
inikep9470b872016-06-09 12:54:06 +0200200 if os.path.isfile(fileName):
201 testFilePaths.append(os.path.abspath(fileName))
202 else:
inikepd731de82016-06-21 11:26:17 +0200203 log("ERROR: File not found: " + fileName)
204 exit(1)
inikep9470b872016-06-09 12:54:06 +0200205
inikepc1b154a2016-06-10 12:53:12 +0200206 # check availability of e-mail senders
inikep2d9272f2016-06-21 19:28:51 +0200207 have_mutt = does_command_exist("mutt -h");
inikepc1b154a2016-06-10 12:53:12 +0200208 have_mail = does_command_exist("mail -V");
inikepf1690292016-06-10 13:59:08 +0200209 if not have_mutt and not have_mail:
inikepd731de82016-06-21 11:26:17 +0200210 log("ERROR: e-mail senders 'mail' or 'mutt' not found")
211 exit(1)
inikepc1b154a2016-06-10 12:53:12 +0200212
inikep2d9272f2016-06-21 19:28:51 +0200213 print("PARAMETERS:\nrepoURL=%s" % args.repoURL)
inikep95da7432016-06-22 12:12:35 +0200214 print("working_path=%s" % working_path)
inikep2d9272f2016-06-21 19:28:51 +0200215 print("clone_path=%s" % clone_path)
216 print("testFilePath(%s)=%s" % (len(testFilePaths), testFilePaths))
217 print("message=%s" % args.message)
218 print("emails=%s" % args.emails)
219 print("maxLoadAvg=%s" % args.maxLoadAvg)
220 print("lowerLimit=%s" % args.lowerLimit)
221 print("lastCLevel=%s" % args.lastCLevel)
222 print("sleepTime=%s" % args.sleepTime)
223 print("dry_run=%s" % args.dry_run)
224 print("have_mutt=%s have_mail=%s" % (have_mutt, have_mail))
inikepc1b154a2016-06-10 12:53:12 +0200225
inikepd731de82016-06-21 11:26:17 +0200226 # clone ZSTD repo if needed
inikep95da7432016-06-22 12:12:35 +0200227 if not os.path.isdir(working_path):
228 os.mkdir(working_path)
inikepd731de82016-06-21 11:26:17 +0200229 if not os.path.isdir(clone_path):
inikep95da7432016-06-22 12:12:35 +0200230 execute.cwd = working_path
inikepd731de82016-06-21 11:26:17 +0200231 execute('git clone ' + args.repoURL)
232 if not os.path.isdir(clone_path):
233 log("ERROR: ZSTD clone not found: " + clone_path)
234 exit(1)
235 execute.cwd = clone_path
236
237 # check if speedTest.pid already exists
inikepd731de82016-06-21 11:26:17 +0200238 pidfile = "./speedTest.pid"
239 if os.path.isfile(pidfile):
240 log("ERROR: %s already exists, exiting" % pidfile)
241 exit(1)
242
inikep47020672016-06-22 17:11:01 +0200243 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 +0200244 file(pidfile, 'w').write(pid)
inikepc364ee72016-06-22 14:01:53 +0200245
inikep9470b872016-06-09 12:54:06 +0200246 while True:
inikepd731de82016-06-21 11:26:17 +0200247 try:
248 loadavg = os.getloadavg()[0]
249 if (loadavg <= args.maxLoadAvg):
inikepbcb9aad2016-06-22 13:07:58 +0200250 branches = git_get_branches()
inikep95da7432016-06-22 12:12:35 +0200251 for branch in branches:
inikep116128c2016-06-22 18:12:57 +0200252 commit = execute('git show -s --format=%h ' + branch)[0]
253 last_commit = update_config_file(branch, commit)
254 if commit == last_commit:
255 log("skipping branch %s: head %s already processed" % (branch, commit))
256 else:
257 log("build branch %s: head %s is different from prev %s" % (branch, commit, last_commit))
258 test_commit(branch, commit, last_commit, args, testFilePaths, have_mutt, have_mail)
inikepd731de82016-06-21 11:26:17 +0200259 else:
260 log("WARNING: main loadavg=%.2f is higher than %s" % (loadavg, args.maxLoadAvg))
inikep95da7432016-06-22 12:12:35 +0200261 log("sleep for %s seconds" % args.sleepTime)
262 time.sleep(args.sleepTime)
inikep116128c2016-06-22 18:12:57 +0200263 except Exception as e:
264 stack = traceback.format_exc()
265 email_topic = '%s:%s ERROR in %s:%s' % (email_header, pid, branch, commit)
266 send_email(args.emails, email_topic, stack, have_mutt, have_mail)
267 print(stack)
inikepc364ee72016-06-22 14:01:53 +0200268 except KeyboardInterrupt:
inikepd731de82016-06-21 11:26:17 +0200269 os.unlink(pidfile)
inikep47020672016-06-22 17:11:01 +0200270 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 +0200271 exit(0)