blob: eaeb4bf995ee3637381f12578fb2b222238f1af2 [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')
inikep6e5beea2016-07-19 13:09:00 +020075 branches = execute('git branch -rl')
76 output = []
77 for line in branches:
78 if ("HEAD" not in line) and ("coverity_scan" not in line) and ("gh-pages" not in line):
79 output.append(line.strip())
80 return output
inikepbcb9aad2016-06-22 13:07:58 +020081
82
inikepf2f59d72016-06-22 15:42:26 +020083def git_get_changes(branch, commit, last_commit):
inikepbcb9aad2016-06-22 13:07:58 +020084 fmt = '--format="%h: (%an) %s, %ar"'
85 if last_commit is None:
86 commits = execute('git log -n 10 %s %s' % (fmt, commit))
87 else:
88 commits = execute('git --no-pager log %s %s..%s' % (fmt, last_commit, commit))
inikepf2f59d72016-06-22 15:42:26 +020089 return str('Changes in %s since %s:\n' % (branch, last_commit)) + '\n'.join(commits)
inikepbcb9aad2016-06-22 13:07:58 +020090
91
inikepc364ee72016-06-22 14:01:53 +020092def get_last_results(resultsFileName):
inikepbcb9aad2016-06-22 13:07:58 +020093 if not os.path.isfile(resultsFileName):
94 return None, None, None
95 commit = None
96 cspeed = []
97 dspeed = []
98 with open(resultsFileName,'r') as f:
99 for line in f:
100 words = line.split()
101 if len(words) == 2: # branch + commit
102 commit = words[1];
103 cspeed = []
104 dspeed = []
105 if (len(words) == 8): # results
106 cspeed.append(float(words[3]))
107 dspeed.append(float(words[5]))
108 return commit, cspeed, dspeed
109
110
111def benchmark_and_compare(branch, commit, resultsFileName, lastCLevel, testFilePath, fileName, last_cspeed, last_dspeed, lower_limit, maxLoadAvg, message):
112 sleepTime = 30
113 while os.getloadavg()[0] > maxLoadAvg:
114 log("WARNING: bench loadavg=%.2f is higher than %s, sleeping for %s seconds" % (os.getloadavg()[0], maxLoadAvg, sleepTime))
115 time.sleep(sleepTime)
116 start_load = str(os.getloadavg())
inikep116128c2016-06-22 18:12:57 +0200117 result = execute('programs/zstd -qi5b1e%s %s' % (lastCLevel, testFilePath), print_output=True)
inikepbcb9aad2016-06-22 13:07:58 +0200118 end_load = str(os.getloadavg())
119 linesExpected = lastCLevel + 2;
120 if len(result) != linesExpected:
121 raise RuntimeError("ERROR: number of result lines=%d is different that expected %d\n%s" % (len(result), linesExpected, '\n'.join(result)))
122 with open(resultsFileName, "a") as myfile:
123 myfile.write(branch + " " + commit + "\n")
124 myfile.write('\n'.join(result) + '\n')
125 myfile.close()
126 if (last_cspeed == None):
127 log("WARNING: No data for comparison for branch=%s file=%s " % (branch, fileName))
128 return ""
inikepc364ee72016-06-22 14:01:53 +0200129 commit, cspeed, dspeed = get_last_results(resultsFileName)
inikepbcb9aad2016-06-22 13:07:58 +0200130 text = ""
131 for i in range(0, min(len(cspeed), len(last_cspeed))):
132 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))
133 if (cspeed[i]/last_cspeed[i] < lower_limit):
134 text += "WARNING: -%d cspeed=%.2f clast=%.2f cdiff=%.4f %s\n" % (i+1, cspeed[i], last_cspeed[i], cspeed[i]/last_cspeed[i], fileName)
135 if (dspeed[i]/last_dspeed[i] < lower_limit):
136 text += "WARNING: -%d dspeed=%.2f dlast=%.2f ddiff=%.4f %s\n" % (i+1, dspeed[i], last_dspeed[i], dspeed[i]/last_dspeed[i], fileName)
137 if text:
138 text = message + ("\nmaxLoadAvg=%s load average at start=%s end=%s\n" % (maxLoadAvg, start_load, end_load)) + text
139 return text
140
141
inikep116128c2016-06-22 18:12:57 +0200142def update_config_file(branch, commit):
143 last_commit = None
144 commitFileName = working_path + "/commit_" + branch.replace("/", "_") + ".txt"
145 if os.path.isfile(commitFileName):
146 last_commit = file(commitFileName, 'r').read()
147 file(commitFileName, 'w').write(commit)
148 return last_commit
inikep9470b872016-06-09 12:54:06 +0200149
inikep9470b872016-06-09 12:54:06 +0200150
inikep116128c2016-06-22 18:12:57 +0200151def test_commit(branch, commit, last_commit, args, testFilePaths, have_mutt, have_mail):
inikep82babfc2016-06-22 20:06:42 +0200152 local_branch = string.split(branch, '/')[1]
153 version = local_branch.rpartition('-')[2] + '_' + commit
154 if not args.dry_run:
155 execute('make clean zstdprogram MOREFLAGS="-DZSTD_GIT_COMMIT=%s"' % version)
inikep116128c2016-06-22 18:12:57 +0200156 logFileName = working_path + "/log_" + branch.replace("/", "_") + ".txt"
157 text_to_send = []
158 results_files = ""
159 for filePath in testFilePaths:
160 fileName = filePath.rpartition('/')[2]
161 resultsFileName = working_path + "/results_" + branch.replace("/", "_") + "_" + fileName.replace(".", "_") + ".txt"
162 last_commit, cspeed, dspeed = get_last_results(resultsFileName)
inikep116128c2016-06-22 18:12:57 +0200163 if not args.dry_run:
164 text = benchmark_and_compare(branch, commit, resultsFileName, args.lastCLevel, filePath, fileName, cspeed, dspeed, args.lowerLimit, args.maxLoadAvg, args.message)
165 if text:
166 log("WARNING: redoing tests for branch %s: commit %s" % (branch, commit))
inikepd7d251c2016-06-22 16:13:25 +0200167 text = benchmark_and_compare(branch, commit, resultsFileName, args.lastCLevel, filePath, fileName, cspeed, dspeed, args.lowerLimit, args.maxLoadAvg, args.message)
168 if text:
inikep116128c2016-06-22 18:12:57 +0200169 text_to_send.append(text)
170 results_files += resultsFileName + " "
171 if text_to_send:
172 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 +0200173
174
175if __name__ == '__main__':
176 parser = argparse.ArgumentParser()
inikepc1b154a2016-06-10 12:53:12 +0200177 parser.add_argument('testFileNames', help='file names list for speed benchmark')
178 parser.add_argument('emails', help='list of e-mail addresses to send warnings')
inikep1e375f12016-06-13 10:50:09 +0200179 parser.add_argument('--message', help='attach an additional message to e-mail', default="")
inikepd731de82016-06-21 11:26:17 +0200180 parser.add_argument('--repoURL', help='changes default repository URL', default=default_repo_url)
inikepc1b154a2016-06-10 12:53:12 +0200181 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 +0200182 parser.add_argument('--maxLoadAvg', type=float, help='maximum load average to start testing', default=0.75)
183 parser.add_argument('--lastCLevel', type=int, help='last compression level for testing', default=5)
inikepc1b154a2016-06-10 12:53:12 +0200184 parser.add_argument('--sleepTime', type=int, help='frequency of repository checking in seconds', default=300)
inikep9470b872016-06-09 12:54:06 +0200185 parser.add_argument('--dry-run', dest='dry_run', action='store_true', help='not build', default=False)
186 args = parser.parse_args()
187
188 # check if test files are accessible
189 testFileNames = args.testFileNames.split()
190 testFilePaths = []
191 for fileName in testFileNames:
inikep47020672016-06-22 17:11:01 +0200192 fileName = os.path.expanduser(fileName)
inikep9470b872016-06-09 12:54:06 +0200193 if os.path.isfile(fileName):
194 testFilePaths.append(os.path.abspath(fileName))
195 else:
inikepd731de82016-06-21 11:26:17 +0200196 log("ERROR: File not found: " + fileName)
197 exit(1)
inikep9470b872016-06-09 12:54:06 +0200198
inikepc1b154a2016-06-10 12:53:12 +0200199 # check availability of e-mail senders
inikep2d9272f2016-06-21 19:28:51 +0200200 have_mutt = does_command_exist("mutt -h");
inikepc1b154a2016-06-10 12:53:12 +0200201 have_mail = does_command_exist("mail -V");
inikepf1690292016-06-10 13:59:08 +0200202 if not have_mutt and not have_mail:
inikepd731de82016-06-21 11:26:17 +0200203 log("ERROR: e-mail senders 'mail' or 'mutt' not found")
204 exit(1)
inikepc1b154a2016-06-10 12:53:12 +0200205
inikep2d9272f2016-06-21 19:28:51 +0200206 print("PARAMETERS:\nrepoURL=%s" % args.repoURL)
inikep95da7432016-06-22 12:12:35 +0200207 print("working_path=%s" % working_path)
inikep2d9272f2016-06-21 19:28:51 +0200208 print("clone_path=%s" % clone_path)
209 print("testFilePath(%s)=%s" % (len(testFilePaths), testFilePaths))
210 print("message=%s" % args.message)
211 print("emails=%s" % args.emails)
212 print("maxLoadAvg=%s" % args.maxLoadAvg)
213 print("lowerLimit=%s" % args.lowerLimit)
214 print("lastCLevel=%s" % args.lastCLevel)
215 print("sleepTime=%s" % args.sleepTime)
216 print("dry_run=%s" % args.dry_run)
217 print("have_mutt=%s have_mail=%s" % (have_mutt, have_mail))
inikepc1b154a2016-06-10 12:53:12 +0200218
inikepd731de82016-06-21 11:26:17 +0200219 # clone ZSTD repo if needed
inikep95da7432016-06-22 12:12:35 +0200220 if not os.path.isdir(working_path):
221 os.mkdir(working_path)
inikepd731de82016-06-21 11:26:17 +0200222 if not os.path.isdir(clone_path):
inikep95da7432016-06-22 12:12:35 +0200223 execute.cwd = working_path
inikepd731de82016-06-21 11:26:17 +0200224 execute('git clone ' + args.repoURL)
225 if not os.path.isdir(clone_path):
226 log("ERROR: ZSTD clone not found: " + clone_path)
227 exit(1)
228 execute.cwd = clone_path
229
230 # check if speedTest.pid already exists
inikepd731de82016-06-21 11:26:17 +0200231 pidfile = "./speedTest.pid"
232 if os.path.isfile(pidfile):
233 log("ERROR: %s already exists, exiting" % pidfile)
234 exit(1)
235
inikep47020672016-06-22 17:11:01 +0200236 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 +0200237 file(pidfile, 'w').write(pid)
inikepc364ee72016-06-22 14:01:53 +0200238
inikep9470b872016-06-09 12:54:06 +0200239 while True:
inikepd731de82016-06-21 11:26:17 +0200240 try:
241 loadavg = os.getloadavg()[0]
242 if (loadavg <= args.maxLoadAvg):
inikepbcb9aad2016-06-22 13:07:58 +0200243 branches = git_get_branches()
inikep95da7432016-06-22 12:12:35 +0200244 for branch in branches:
inikep116128c2016-06-22 18:12:57 +0200245 commit = execute('git show -s --format=%h ' + branch)[0]
246 last_commit = update_config_file(branch, commit)
247 if commit == last_commit:
248 log("skipping branch %s: head %s already processed" % (branch, commit))
249 else:
250 log("build branch %s: head %s is different from prev %s" % (branch, commit, last_commit))
inikep82babfc2016-06-22 20:06:42 +0200251 execute('git checkout -- . && git checkout ' + branch)
252 print(git_get_changes(branch, commit, last_commit))
inikep116128c2016-06-22 18:12:57 +0200253 test_commit(branch, commit, last_commit, args, testFilePaths, have_mutt, have_mail)
inikepd731de82016-06-21 11:26:17 +0200254 else:
255 log("WARNING: main loadavg=%.2f is higher than %s" % (loadavg, args.maxLoadAvg))
inikep95da7432016-06-22 12:12:35 +0200256 log("sleep for %s seconds" % args.sleepTime)
257 time.sleep(args.sleepTime)
inikep116128c2016-06-22 18:12:57 +0200258 except Exception as e:
259 stack = traceback.format_exc()
260 email_topic = '%s:%s ERROR in %s:%s' % (email_header, pid, branch, commit)
261 send_email(args.emails, email_topic, stack, have_mutt, have_mail)
262 print(stack)
inikepc364ee72016-06-22 14:01:53 +0200263 except KeyboardInterrupt:
inikepd731de82016-06-21 11:26:17 +0200264 os.unlink(pidfile)
inikep47020672016-06-22 17:11:01 +0200265 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 +0200266 exit(0)