blob: c693b2188e44be1481a3373be0f7cfaa620c5d7a [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
inikep9470b872016-06-09 12:54:06 +02009
inikepd731de82016-06-21 11:26:17 +020010default_repo_url = 'https://github.com/Cyan4973/zstd.git'
inikep95da7432016-06-22 12:12:35 +020011working_dir_name = 'speedTest'
12working_path = os.getcwd() + '/' + working_dir_name # /path/to/zstd/tests/speedTest
13clone_path = working_path + '/' + 'zstd' # /path/to/zstd/tests/speedTest/zstd
inikepd731de82016-06-21 11:26:17 +020014email_header = '[ZSTD_speedTest]'
inikep95da7432016-06-22 12:12:35 +020015pid = str(os.getpid())
16
inikep9470b872016-06-09 12:54:06 +020017
18def log(text):
inikep2d9272f2016-06-21 19:28:51 +020019 print(time.strftime("%Y/%m/%d %H:%M:%S") + ' - ' + text)
inikep9470b872016-06-09 12:54:06 +020020
inikep2d9272f2016-06-21 19:28:51 +020021
22def execute(command, print_output=False, print_error=True, param_shell=True):
inikep9470b872016-06-09 12:54:06 +020023 log("> " + command)
inikep95da7432016-06-22 12:12:35 +020024 popen = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=param_shell, cwd=execute.cwd)
25 stdout = popen.communicate()[0]
26 stdout_lines = stdout.splitlines()
inikep2d9272f2016-06-21 19:28:51 +020027 if print_output:
inikep95da7432016-06-22 12:12:35 +020028 print('\n'.join(stdout_lines))
inikep9470b872016-06-09 12:54:06 +020029 if popen.returncode is not None and popen.returncode != 0:
inikepc1b154a2016-06-10 12:53:12 +020030 if not print_output and print_error:
inikep95da7432016-06-22 12:12:35 +020031 print('\n'.join(stdout_lines))
32 raise RuntimeError('\n'.join(stdout_lines))
33 return stdout_lines
inikep9470b872016-06-09 12:54:06 +020034execute.cwd = None
35
36
inikepc1b154a2016-06-10 12:53:12 +020037def does_command_exist(command):
inikep95da7432016-06-22 12:12:35 +020038 try:
39 execute(command, False, False);
40 except Exception as e:
41 return False
42 return True
inikepc1b154a2016-06-10 12:53:12 +020043
44
inikep95da7432016-06-22 12:12:35 +020045def get_branches():
inikep9470b872016-06-09 12:54:06 +020046 execute('git fetch -p')
inikep95da7432016-06-22 12:12:35 +020047 output = execute('git branch -rl')
inikep9470b872016-06-09 12:54:06 +020048 for line in output:
49 if "HEAD" in line:
inikep2d9272f2016-06-21 19:28:51 +020050 output.remove(line) # remove "origin/HEAD -> origin/dev"
inikep95da7432016-06-22 12:12:35 +020051 return map(lambda l: l.strip(), output)
inikep9470b872016-06-09 12:54:06 +020052
53
54def notify(branch, commit, last_commit):
55 text_tmpl = string.Template('Changes since $last_commit:\r\n$commits')
56 branch = branch.split('/')[1]
57 fmt = '--format="%h: (%an) %s, %ar"'
58 if last_commit is None:
inikep95da7432016-06-22 12:12:35 +020059 commits = execute('git log -n 10 %s %s' % (fmt, commit))
inikep9470b872016-06-09 12:54:06 +020060 else:
inikep95da7432016-06-22 12:12:35 +020061 commits = execute('git --no-pager log %s %s..%s' % (fmt, last_commit, commit))
62 text = text_tmpl.substitute({'last_commit': last_commit, 'commits': '\n'.join(commits)})
inikep2d9272f2016-06-21 19:28:51 +020063 print(str("commits for %s: %s" % (commit, text)))
inikep9470b872016-06-09 12:54:06 +020064
inikep2d9272f2016-06-21 19:28:51 +020065
inikep9470b872016-06-09 12:54:06 +020066def compile(branch, commit, dry_run):
67 local_branch = string.split(branch, '/')[1]
68 version = local_branch.rpartition('-')[2]
69 version = version + '_' + commit
70 execute('git checkout -- . && git checkout ' + branch)
71 if not dry_run:
72 execute('VERSION=' + version + '; make clean zstdprogram')
73
74
75def get_last_commit(resultsFileName):
76 if not os.path.isfile(resultsFileName):
77 return None, None, None
78 commit = None
79 cspeed = []
80 dspeed = []
81 with open(resultsFileName,'r') as f:
82 for line in f:
83 words = line.split()
84 if len(words) == 2: # branch + commit
85 commit = words[1];
86 cspeed = []
87 dspeed = []
88 if (len(words) == 8):
89 cspeed.append(float(words[3]))
90 dspeed.append(float(words[5]))
91 #if commit != None:
92 # print "commit=%s cspeed=%s dspeed=%s" % (commit, cspeed, dspeed)
93 return commit, cspeed, dspeed
94
95
inikepc1b154a2016-06-10 12:53:12 +020096def benchmark_and_compare(branch, commit, resultsFileName, lastCLevel, testFilePath, fileName, last_cspeed, last_dspeed, lower_limit, maxLoadAvg, message):
97 sleepTime = 30
inikep9470b872016-06-09 12:54:06 +020098 while os.getloadavg()[0] > maxLoadAvg:
inikepc1b154a2016-06-10 12:53:12 +020099 log("WARNING: bench loadavg=%.2f is higher than %s, sleeping for %s seconds" % (os.getloadavg()[0], maxLoadAvg, sleepTime))
100 time.sleep(sleepTime)
inikep9470b872016-06-09 12:54:06 +0200101 start_load = str(os.getloadavg())
inikep95da7432016-06-22 12:12:35 +0200102 result = execute('programs/zstd -qi5b1e' + str(lastCLevel) + ' ' + testFilePath, print_output=True)
inikep9470b872016-06-09 12:54:06 +0200103 end_load = str(os.getloadavg())
104 linesExpected = lastCLevel + 2;
105 if len(result) != linesExpected:
inikep95da7432016-06-22 12:12:35 +0200106 raise RuntimeError("ERROR: number of result lines=%d is different that expected %d" % (len(result), linesExpected))
inikep9470b872016-06-09 12:54:06 +0200107 with open(resultsFileName, "a") as myfile:
108 myfile.write(branch + " " + commit + "\n")
inikep95da7432016-06-22 12:12:35 +0200109 myfile.write('\n'.join(result) + '\n')
inikep9470b872016-06-09 12:54:06 +0200110 myfile.close()
111 if (last_cspeed == None):
inikep95da7432016-06-22 12:12:35 +0200112 log("WARNING: No data for comparison for branch=%s file=%s " % (branch, fileName))
inikep9470b872016-06-09 12:54:06 +0200113 return ""
114 commit, cspeed, dspeed = get_last_commit(resultsFileName)
115 text = ""
116 for i in range(0, min(len(cspeed), len(last_cspeed))):
inikep95da7432016-06-22 12:12:35 +0200117 print("%s: -%d cspeed=%6.2f clast=%6.2f cdiff=%1.4f dspeed=%6.2f dlast=%6.2f ddiff=%1.4f %s" % (branch, i+1, cspeed[i], last_cspeed[i], cspeed[i]/last_cspeed[i], dspeed[i], last_dspeed[i], dspeed[i]/last_dspeed[i], fileName))
inikep9470b872016-06-09 12:54:06 +0200118 if (cspeed[i]/last_cspeed[i] < lower_limit):
119 text += "WARNING: File=%s level=%d cspeed=%s last=%s diff=%s\n" % (fileName, i+1, cspeed[i], last_cspeed[i], cspeed[i]/last_cspeed[i])
120 if (dspeed[i]/last_dspeed[i] < lower_limit):
121 text += "WARNING: File=%s level=%d dspeed=%s last=%s diff=%s\n" % (fileName, i+1, dspeed[i], last_dspeed[i], dspeed[i]/last_dspeed[i])
122 if text:
inikepc1b154a2016-06-10 12:53:12 +0200123 text = message + ("\nmaxLoadAvg=%s load average at start=%s end=%s\n" % (maxLoadAvg, start_load, end_load)) + text
inikep9470b872016-06-09 12:54:06 +0200124 return text
125
126
inikep95da7432016-06-22 12:12:35 +0200127def send_email(emails, topic, text, have_mutt, have_mail):
128 logFileName = working_path + '/' + 'tmpEmailContent'
inikep9470b872016-06-09 12:54:06 +0200129 with open(logFileName, "w") as myfile:
130 myfile.writelines(text)
131 myfile.close()
inikepc1b154a2016-06-10 12:53:12 +0200132 if have_mutt:
inikep95da7432016-06-22 12:12:35 +0200133 execute('mutt -s "' + topic + '" ' + emails + ' < ' + logFileName)
inikepc1b154a2016-06-10 12:53:12 +0200134 elif have_mail:
inikep95da7432016-06-22 12:12:35 +0200135 execute('mail -s "' + topic + '" ' + emails + ' < ' + logFileName)
inikepc1b154a2016-06-10 12:53:12 +0200136 else:
inikep95da7432016-06-22 12:12:35 +0200137 log("e-mail cannot be sent (mail or mutt not found)")
inikep9470b872016-06-09 12:54:06 +0200138
139
inikep95da7432016-06-22 12:12:35 +0200140def send_email_with_attachments(branch, commit, last_commit, emails, text, results_files, logFileName, lower_limit, have_mutt, have_mail):
141 with open(logFileName, "w") as myfile:
142 myfile.writelines(text)
143 myfile.close()
144 email_topic = '%s:%s Warning for branch=%s commit=%s last_commit=%s speed<%s' % (email_header, pid, branch, commit, last_commit, lower_limit)
145 if have_mutt:
146 execute('mutt -s "' + email_topic + '" ' + emails + ' -a ' + results_files + ' < ' + logFileName)
147 elif have_mail:
148 execute('mail -s "' + email_topic + '" ' + emails + ' < ' + logFileName)
149 else:
150 log("e-mail cannot be sent (mail or mutt not found)")
151
152
153def check_branch(branch, args, testFilePaths, have_mutt, have_mail):
154 commits = execute('git show -s --format=%h ' + branch)[0]
155 for commit in [commits]:
inikep9470b872016-06-09 12:54:06 +0200156 try:
inikep95da7432016-06-22 12:12:35 +0200157 commitFileName = working_path + "/commit_" + branch.replace("/", "_")
inikep9470b872016-06-09 12:54:06 +0200158 if os.path.isfile(commitFileName):
159 last_commit = file(commitFileName, 'r').read()
160 else:
161 last_commit = None
162 file(commitFileName, 'w').write(commit)
163
164 if commit == last_commit:
165 log("skipping branch %s: head %s already processed" % (branch, commit))
166 else:
167 log("build branch %s: head %s is different from prev %s" % (branch, commit, last_commit))
168 compile(branch, commit, args.dry_run)
169
inikep95da7432016-06-22 12:12:35 +0200170 logFileName = working_path + "/log_" + branch.replace("/", "_")
inikep9470b872016-06-09 12:54:06 +0200171 text_to_send = []
172 results_files = ""
173 for filePath in testFilePaths:
174 fileName = filePath.rpartition('/')[2]
inikep95da7432016-06-22 12:12:35 +0200175 resultsFileName = working_path + "/results_" + branch.replace("/", "_") + "_" + fileName
inikep9470b872016-06-09 12:54:06 +0200176 last_commit, cspeed, dspeed = get_last_commit(resultsFileName)
177
178 if not args.dry_run:
inikepc1b154a2016-06-10 12:53:12 +0200179 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 +0200180 if text:
inikep95da7432016-06-22 12:12:35 +0200181 log("WARNING: redoing tests for branch %s: commit %s" % (branch, commit))
inikepc1b154a2016-06-10 12:53:12 +0200182 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 +0200183 if text:
184 text_to_send.append(text)
185 results_files += resultsFileName + " "
186 if text_to_send:
inikep95da7432016-06-22 12:12:35 +0200187 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 +0200188 notify(branch, commit, last_commit)
189 except Exception as e:
190 stack = traceback.format_exc()
inikep95da7432016-06-22 12:12:35 +0200191 email_topic = '%s:%s ERROR in branch=%s commit=%s' % (email_header, pid, branch, commit)
192 send_email(args.emails, email_topic, stack, have_mutt, have_mail)
inikep2d9272f2016-06-21 19:28:51 +0200193 print(stack)
inikep9470b872016-06-09 12:54:06 +0200194
195
196if __name__ == '__main__':
197 parser = argparse.ArgumentParser()
inikepc1b154a2016-06-10 12:53:12 +0200198 parser.add_argument('testFileNames', help='file names list for speed benchmark')
199 parser.add_argument('emails', help='list of e-mail addresses to send warnings')
inikep1e375f12016-06-13 10:50:09 +0200200 parser.add_argument('--message', help='attach an additional message to e-mail', default="")
inikepd731de82016-06-21 11:26:17 +0200201 parser.add_argument('--repoURL', help='changes default repository URL', default=default_repo_url)
inikepc1b154a2016-06-10 12:53:12 +0200202 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 +0200203 parser.add_argument('--maxLoadAvg', type=float, help='maximum load average to start testing', default=0.75)
204 parser.add_argument('--lastCLevel', type=int, help='last compression level for testing', default=5)
inikepc1b154a2016-06-10 12:53:12 +0200205 parser.add_argument('--sleepTime', type=int, help='frequency of repository checking in seconds', default=300)
inikep9470b872016-06-09 12:54:06 +0200206 parser.add_argument('--dry-run', dest='dry_run', action='store_true', help='not build', default=False)
207 args = parser.parse_args()
208
209 # check if test files are accessible
210 testFileNames = args.testFileNames.split()
211 testFilePaths = []
212 for fileName in testFileNames:
213 if os.path.isfile(fileName):
214 testFilePaths.append(os.path.abspath(fileName))
215 else:
inikepd731de82016-06-21 11:26:17 +0200216 log("ERROR: File not found: " + fileName)
217 exit(1)
inikep9470b872016-06-09 12:54:06 +0200218
inikepc1b154a2016-06-10 12:53:12 +0200219 # check availability of e-mail senders
inikep2d9272f2016-06-21 19:28:51 +0200220 have_mutt = does_command_exist("mutt -h");
inikepc1b154a2016-06-10 12:53:12 +0200221 have_mail = does_command_exist("mail -V");
inikepf1690292016-06-10 13:59:08 +0200222 if not have_mutt and not have_mail:
inikepd731de82016-06-21 11:26:17 +0200223 log("ERROR: e-mail senders 'mail' or 'mutt' not found")
224 exit(1)
inikepc1b154a2016-06-10 12:53:12 +0200225
inikep2d9272f2016-06-21 19:28:51 +0200226 print("PARAMETERS:\nrepoURL=%s" % args.repoURL)
inikep95da7432016-06-22 12:12:35 +0200227 print("working_path=%s" % working_path)
inikep2d9272f2016-06-21 19:28:51 +0200228 print("clone_path=%s" % clone_path)
229 print("testFilePath(%s)=%s" % (len(testFilePaths), testFilePaths))
230 print("message=%s" % args.message)
231 print("emails=%s" % args.emails)
232 print("maxLoadAvg=%s" % args.maxLoadAvg)
233 print("lowerLimit=%s" % args.lowerLimit)
234 print("lastCLevel=%s" % args.lastCLevel)
235 print("sleepTime=%s" % args.sleepTime)
236 print("dry_run=%s" % args.dry_run)
237 print("have_mutt=%s have_mail=%s" % (have_mutt, have_mail))
inikepc1b154a2016-06-10 12:53:12 +0200238
inikepd731de82016-06-21 11:26:17 +0200239 # clone ZSTD repo if needed
inikep95da7432016-06-22 12:12:35 +0200240 if not os.path.isdir(working_path):
241 os.mkdir(working_path)
inikepd731de82016-06-21 11:26:17 +0200242 if not os.path.isdir(clone_path):
inikep95da7432016-06-22 12:12:35 +0200243 execute.cwd = working_path
inikepd731de82016-06-21 11:26:17 +0200244 execute('git clone ' + args.repoURL)
245 if not os.path.isdir(clone_path):
246 log("ERROR: ZSTD clone not found: " + clone_path)
247 exit(1)
248 execute.cwd = clone_path
249
250 # check if speedTest.pid already exists
inikepd731de82016-06-21 11:26:17 +0200251 pidfile = "./speedTest.pid"
252 if os.path.isfile(pidfile):
253 log("ERROR: %s already exists, exiting" % pidfile)
254 exit(1)
255
inikep95da7432016-06-22 12:12:35 +0200256 send_email(args.emails, email_header + ':%s test-zstd-speed.py has been started' % pid, '', have_mutt, have_mail)
257
258 file(pidfile, 'w').write(pid)
inikep9470b872016-06-09 12:54:06 +0200259 while True:
inikepd731de82016-06-21 11:26:17 +0200260 try:
261 loadavg = os.getloadavg()[0]
262 if (loadavg <= args.maxLoadAvg):
inikep95da7432016-06-22 12:12:35 +0200263 branches = get_branches()
264 for branch in branches:
265 check_branch(branch, args, testFilePaths, have_mutt, have_mail)
inikepd731de82016-06-21 11:26:17 +0200266 else:
267 log("WARNING: main loadavg=%.2f is higher than %s" % (loadavg, args.maxLoadAvg))
inikep95da7432016-06-22 12:12:35 +0200268 log("sleep for %s seconds" % args.sleepTime)
269 time.sleep(args.sleepTime)
inikepd731de82016-06-21 11:26:17 +0200270 finally:
271 os.unlink(pidfile)
inikep95da7432016-06-22 12:12:35 +0200272 send_email(args.emails, email_header + ':%s test-zstd-speed.py has been stopped' % pid, '', have_mutt, have_mail)