blob: 522227a4e12fb0c1a5eb5792c189ab806e2b4c3b [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
inikepc364ee72016-06-22 14:01:53 +020091def get_last_results(resultsFileName):
inikepbcb9aad2016-06-22 13:07:58 +020092 if not os.path.isfile(resultsFileName):
93 return None, None, None
94 commit = None
95 cspeed = []
96 dspeed = []
97 with open(resultsFileName,'r') as f:
98 for line in f:
99 words = line.split()
100 if len(words) == 2: # branch + commit
101 commit = words[1];
102 cspeed = []
103 dspeed = []
104 if (len(words) == 8): # results
105 cspeed.append(float(words[3]))
106 dspeed.append(float(words[5]))
107 return commit, cspeed, dspeed
108
109
110def benchmark_and_compare(branch, commit, resultsFileName, lastCLevel, testFilePath, fileName, last_cspeed, last_dspeed, lower_limit, maxLoadAvg, message):
111 sleepTime = 30
112 while os.getloadavg()[0] > maxLoadAvg:
113 log("WARNING: bench loadavg=%.2f is higher than %s, sleeping for %s seconds" % (os.getloadavg()[0], maxLoadAvg, sleepTime))
114 time.sleep(sleepTime)
115 start_load = str(os.getloadavg())
inikep116128c2016-06-22 18:12:57 +0200116 result = execute('programs/zstd -qi5b1e%s %s' % (lastCLevel, testFilePath), print_output=True)
inikepbcb9aad2016-06-22 13:07:58 +0200117 end_load = str(os.getloadavg())
118 linesExpected = lastCLevel + 2;
119 if len(result) != linesExpected:
120 raise RuntimeError("ERROR: number of result lines=%d is different that expected %d\n%s" % (len(result), linesExpected, '\n'.join(result)))
121 with open(resultsFileName, "a") as myfile:
122 myfile.write(branch + " " + commit + "\n")
123 myfile.write('\n'.join(result) + '\n')
124 myfile.close()
125 if (last_cspeed == None):
126 log("WARNING: No data for comparison for branch=%s file=%s " % (branch, fileName))
127 return ""
inikepc364ee72016-06-22 14:01:53 +0200128 commit, cspeed, dspeed = get_last_results(resultsFileName)
inikepbcb9aad2016-06-22 13:07:58 +0200129 text = ""
130 for i in range(0, min(len(cspeed), len(last_cspeed))):
131 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))
132 if (cspeed[i]/last_cspeed[i] < lower_limit):
133 text += "WARNING: -%d cspeed=%.2f clast=%.2f cdiff=%.4f %s\n" % (i+1, cspeed[i], last_cspeed[i], cspeed[i]/last_cspeed[i], fileName)
134 if (dspeed[i]/last_dspeed[i] < lower_limit):
135 text += "WARNING: -%d dspeed=%.2f dlast=%.2f ddiff=%.4f %s\n" % (i+1, dspeed[i], last_dspeed[i], dspeed[i]/last_dspeed[i], fileName)
136 if text:
137 text = message + ("\nmaxLoadAvg=%s load average at start=%s end=%s\n" % (maxLoadAvg, start_load, end_load)) + text
138 return text
139
140
inikep116128c2016-06-22 18:12:57 +0200141def update_config_file(branch, commit):
142 last_commit = None
143 commitFileName = working_path + "/commit_" + branch.replace("/", "_") + ".txt"
144 if os.path.isfile(commitFileName):
145 last_commit = file(commitFileName, 'r').read()
146 file(commitFileName, 'w').write(commit)
147 return last_commit
inikep9470b872016-06-09 12:54:06 +0200148
inikep9470b872016-06-09 12:54:06 +0200149
inikep116128c2016-06-22 18:12:57 +0200150def test_commit(branch, commit, last_commit, args, testFilePaths, have_mutt, have_mail):
inikep82babfc2016-06-22 20:06:42 +0200151 local_branch = string.split(branch, '/')[1]
152 version = local_branch.rpartition('-')[2] + '_' + commit
153 if not args.dry_run:
154 execute('make clean zstdprogram MOREFLAGS="-DZSTD_GIT_COMMIT=%s"' % version)
inikep116128c2016-06-22 18:12:57 +0200155 logFileName = working_path + "/log_" + branch.replace("/", "_") + ".txt"
156 text_to_send = []
157 results_files = ""
158 for filePath in testFilePaths:
159 fileName = filePath.rpartition('/')[2]
160 resultsFileName = working_path + "/results_" + branch.replace("/", "_") + "_" + fileName.replace(".", "_") + ".txt"
161 last_commit, cspeed, dspeed = get_last_results(resultsFileName)
inikep116128c2016-06-22 18:12:57 +0200162 if not args.dry_run:
163 text = benchmark_and_compare(branch, commit, resultsFileName, args.lastCLevel, filePath, fileName, cspeed, dspeed, args.lowerLimit, args.maxLoadAvg, args.message)
164 if text:
165 log("WARNING: redoing tests for branch %s: commit %s" % (branch, commit))
inikepd7d251c2016-06-22 16:13:25 +0200166 text = benchmark_and_compare(branch, commit, resultsFileName, args.lastCLevel, filePath, fileName, cspeed, dspeed, args.lowerLimit, args.maxLoadAvg, args.message)
167 if text:
inikep116128c2016-06-22 18:12:57 +0200168 text_to_send.append(text)
169 results_files += resultsFileName + " "
170 if text_to_send:
171 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 +0200172
173
174if __name__ == '__main__':
175 parser = argparse.ArgumentParser()
inikepc1b154a2016-06-10 12:53:12 +0200176 parser.add_argument('testFileNames', help='file names list for speed benchmark')
177 parser.add_argument('emails', help='list of e-mail addresses to send warnings')
inikep1e375f12016-06-13 10:50:09 +0200178 parser.add_argument('--message', help='attach an additional message to e-mail', default="")
inikepd731de82016-06-21 11:26:17 +0200179 parser.add_argument('--repoURL', help='changes default repository URL', default=default_repo_url)
inikepc1b154a2016-06-10 12:53:12 +0200180 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 +0200181 parser.add_argument('--maxLoadAvg', type=float, help='maximum load average to start testing', default=0.75)
182 parser.add_argument('--lastCLevel', type=int, help='last compression level for testing', default=5)
inikepc1b154a2016-06-10 12:53:12 +0200183 parser.add_argument('--sleepTime', type=int, help='frequency of repository checking in seconds', default=300)
inikep9470b872016-06-09 12:54:06 +0200184 parser.add_argument('--dry-run', dest='dry_run', action='store_true', help='not build', default=False)
185 args = parser.parse_args()
186
187 # check if test files are accessible
188 testFileNames = args.testFileNames.split()
189 testFilePaths = []
190 for fileName in testFileNames:
inikep47020672016-06-22 17:11:01 +0200191 fileName = os.path.expanduser(fileName)
inikep9470b872016-06-09 12:54:06 +0200192 if os.path.isfile(fileName):
193 testFilePaths.append(os.path.abspath(fileName))
194 else:
inikepd731de82016-06-21 11:26:17 +0200195 log("ERROR: File not found: " + fileName)
196 exit(1)
inikep9470b872016-06-09 12:54:06 +0200197
inikepc1b154a2016-06-10 12:53:12 +0200198 # check availability of e-mail senders
inikep2d9272f2016-06-21 19:28:51 +0200199 have_mutt = does_command_exist("mutt -h");
inikepc1b154a2016-06-10 12:53:12 +0200200 have_mail = does_command_exist("mail -V");
inikepf1690292016-06-10 13:59:08 +0200201 if not have_mutt and not have_mail:
inikepd731de82016-06-21 11:26:17 +0200202 log("ERROR: e-mail senders 'mail' or 'mutt' not found")
203 exit(1)
inikepc1b154a2016-06-10 12:53:12 +0200204
inikep2d9272f2016-06-21 19:28:51 +0200205 print("PARAMETERS:\nrepoURL=%s" % args.repoURL)
inikep95da7432016-06-22 12:12:35 +0200206 print("working_path=%s" % working_path)
inikep2d9272f2016-06-21 19:28:51 +0200207 print("clone_path=%s" % clone_path)
208 print("testFilePath(%s)=%s" % (len(testFilePaths), testFilePaths))
209 print("message=%s" % args.message)
210 print("emails=%s" % args.emails)
211 print("maxLoadAvg=%s" % args.maxLoadAvg)
212 print("lowerLimit=%s" % args.lowerLimit)
213 print("lastCLevel=%s" % args.lastCLevel)
214 print("sleepTime=%s" % args.sleepTime)
215 print("dry_run=%s" % args.dry_run)
216 print("have_mutt=%s have_mail=%s" % (have_mutt, have_mail))
inikepc1b154a2016-06-10 12:53:12 +0200217
inikepd731de82016-06-21 11:26:17 +0200218 # clone ZSTD repo if needed
inikep95da7432016-06-22 12:12:35 +0200219 if not os.path.isdir(working_path):
220 os.mkdir(working_path)
inikepd731de82016-06-21 11:26:17 +0200221 if not os.path.isdir(clone_path):
inikep95da7432016-06-22 12:12:35 +0200222 execute.cwd = working_path
inikepd731de82016-06-21 11:26:17 +0200223 execute('git clone ' + args.repoURL)
224 if not os.path.isdir(clone_path):
225 log("ERROR: ZSTD clone not found: " + clone_path)
226 exit(1)
227 execute.cwd = clone_path
228
229 # check if speedTest.pid already exists
inikepd731de82016-06-21 11:26:17 +0200230 pidfile = "./speedTest.pid"
231 if os.path.isfile(pidfile):
232 log("ERROR: %s already exists, exiting" % pidfile)
233 exit(1)
234
inikep47020672016-06-22 17:11:01 +0200235 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 +0200236 file(pidfile, 'w').write(pid)
inikepc364ee72016-06-22 14:01:53 +0200237
inikep9470b872016-06-09 12:54:06 +0200238 while True:
inikepd731de82016-06-21 11:26:17 +0200239 try:
240 loadavg = os.getloadavg()[0]
241 if (loadavg <= args.maxLoadAvg):
inikepbcb9aad2016-06-22 13:07:58 +0200242 branches = git_get_branches()
inikep95da7432016-06-22 12:12:35 +0200243 for branch in branches:
inikep116128c2016-06-22 18:12:57 +0200244 commit = execute('git show -s --format=%h ' + branch)[0]
245 last_commit = update_config_file(branch, commit)
246 if commit == last_commit:
247 log("skipping branch %s: head %s already processed" % (branch, commit))
248 else:
249 log("build branch %s: head %s is different from prev %s" % (branch, commit, last_commit))
inikep82babfc2016-06-22 20:06:42 +0200250 execute('git checkout -- . && git checkout ' + branch)
251 print(git_get_changes(branch, commit, last_commit))
inikep116128c2016-06-22 18:12:57 +0200252 test_commit(branch, commit, last_commit, args, testFilePaths, have_mutt, have_mail)
inikepd731de82016-06-21 11:26:17 +0200253 else:
254 log("WARNING: main loadavg=%.2f is higher than %s" % (loadavg, args.maxLoadAvg))
inikep95da7432016-06-22 12:12:35 +0200255 log("sleep for %s seconds" % args.sleepTime)
256 time.sleep(args.sleepTime)
inikep116128c2016-06-22 18:12:57 +0200257 except Exception as e:
258 stack = traceback.format_exc()
259 email_topic = '%s:%s ERROR in %s:%s' % (email_header, pid, branch, commit)
260 send_email(args.emails, email_topic, stack, have_mutt, have_mail)
261 print(stack)
inikepc364ee72016-06-22 14:01:53 +0200262 except KeyboardInterrupt:
inikepd731de82016-06-21 11:26:17 +0200263 os.unlink(pidfile)
inikep47020672016-06-22 17:11:01 +0200264 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 +0200265 exit(0)