blob: f3fb3260716be5c04cf498fdea3a3fdeb1dc4209 [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 send_email(emails, topic, text, have_mutt, have_mail):
46 logFileName = working_path + '/' + 'tmpEmailContent'
inikep9470b872016-06-09 12:54:06 +020047 with open(logFileName, "w") as myfile:
48 myfile.writelines(text)
49 myfile.close()
inikepc1b154a2016-06-10 12:53:12 +020050 if have_mutt:
inikep95da7432016-06-22 12:12:35 +020051 execute('mutt -s "' + topic + '" ' + emails + ' < ' + logFileName)
inikepc1b154a2016-06-10 12:53:12 +020052 elif have_mail:
inikep95da7432016-06-22 12:12:35 +020053 execute('mail -s "' + topic + '" ' + emails + ' < ' + logFileName)
inikepc1b154a2016-06-10 12:53:12 +020054 else:
inikep95da7432016-06-22 12:12:35 +020055 log("e-mail cannot be sent (mail or mutt not found)")
inikep9470b872016-06-09 12:54:06 +020056
57
inikep95da7432016-06-22 12:12:35 +020058def send_email_with_attachments(branch, commit, last_commit, emails, text, results_files, logFileName, lower_limit, have_mutt, have_mail):
59 with open(logFileName, "w") as myfile:
60 myfile.writelines(text)
61 myfile.close()
inikepbcb9aad2016-06-22 13:07:58 +020062 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 +020063 if have_mutt:
64 execute('mutt -s "' + email_topic + '" ' + emails + ' -a ' + results_files + ' < ' + logFileName)
65 elif have_mail:
66 execute('mail -s "' + email_topic + '" ' + emails + ' < ' + logFileName)
67 else:
68 log("e-mail cannot be sent (mail or mutt not found)")
69
70
inikepbcb9aad2016-06-22 13:07:58 +020071def git_get_branches():
72 execute('git fetch -p')
73 output = execute('git branch -rl')
74 for line in output:
75 if "HEAD" in line:
76 output.remove(line) # remove "origin/HEAD -> origin/dev"
77 return map(lambda l: l.strip(), output)
78
79
80def git_get_changes(commit, last_commit):
81 fmt = '--format="%h: (%an) %s, %ar"'
82 if last_commit is None:
83 commits = execute('git log -n 10 %s %s' % (fmt, commit))
84 else:
85 commits = execute('git --no-pager log %s %s..%s' % (fmt, last_commit, commit))
86 return str('Changes since %s:' % (last_commit)) + '\n'.join(commits)
87
88
89def compile(branch, commit, last_commit, dry_run):
90 local_branch = string.split(branch, '/')[1]
91 version = local_branch.rpartition('-')[2]
92 version = version + '_' + commit
93 execute('git checkout -- . && git checkout ' + branch)
94 print(git_get_changes(commit, last_commit))
95 if not dry_run:
96 execute('VERSION=' + version + '; make clean zstdprogram')
97
98
99def get_last_commit(resultsFileName):
100 if not os.path.isfile(resultsFileName):
101 return None, None, None
102 commit = None
103 cspeed = []
104 dspeed = []
105 with open(resultsFileName,'r') as f:
106 for line in f:
107 words = line.split()
108 if len(words) == 2: # branch + commit
109 commit = words[1];
110 cspeed = []
111 dspeed = []
112 if (len(words) == 8): # results
113 cspeed.append(float(words[3]))
114 dspeed.append(float(words[5]))
115 return commit, cspeed, dspeed
116
117
118def benchmark_and_compare(branch, commit, resultsFileName, lastCLevel, testFilePath, fileName, last_cspeed, last_dspeed, lower_limit, maxLoadAvg, message):
119 sleepTime = 30
120 while os.getloadavg()[0] > maxLoadAvg:
121 log("WARNING: bench loadavg=%.2f is higher than %s, sleeping for %s seconds" % (os.getloadavg()[0], maxLoadAvg, sleepTime))
122 time.sleep(sleepTime)
123 start_load = str(os.getloadavg())
124 result = execute('programs/zstd -qi5b1e' + str(lastCLevel) + ' ' + testFilePath, print_output=True)
125 end_load = str(os.getloadavg())
126 linesExpected = lastCLevel + 2;
127 if len(result) != linesExpected:
128 raise RuntimeError("ERROR: number of result lines=%d is different that expected %d\n%s" % (len(result), linesExpected, '\n'.join(result)))
129 with open(resultsFileName, "a") as myfile:
130 myfile.write(branch + " " + commit + "\n")
131 myfile.write('\n'.join(result) + '\n')
132 myfile.close()
133 if (last_cspeed == None):
134 log("WARNING: No data for comparison for branch=%s file=%s " % (branch, fileName))
135 return ""
136 commit, cspeed, dspeed = get_last_commit(resultsFileName)
137 text = ""
138 for i in range(0, min(len(cspeed), len(last_cspeed))):
139 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))
140 if (cspeed[i]/last_cspeed[i] < lower_limit):
141 text += "WARNING: -%d cspeed=%.2f clast=%.2f cdiff=%.4f %s\n" % (i+1, cspeed[i], last_cspeed[i], cspeed[i]/last_cspeed[i], fileName)
142 if (dspeed[i]/last_dspeed[i] < lower_limit):
143 text += "WARNING: -%d dspeed=%.2f dlast=%.2f ddiff=%.4f %s\n" % (i+1, dspeed[i], last_dspeed[i], dspeed[i]/last_dspeed[i], fileName)
144 if text:
145 text = message + ("\nmaxLoadAvg=%s load average at start=%s end=%s\n" % (maxLoadAvg, start_load, end_load)) + text
146 return text
147
148
inikep95da7432016-06-22 12:12:35 +0200149def check_branch(branch, args, testFilePaths, have_mutt, have_mail):
150 commits = execute('git show -s --format=%h ' + branch)[0]
151 for commit in [commits]:
inikep9470b872016-06-09 12:54:06 +0200152 try:
inikep95da7432016-06-22 12:12:35 +0200153 commitFileName = working_path + "/commit_" + branch.replace("/", "_")
inikep9470b872016-06-09 12:54:06 +0200154 if os.path.isfile(commitFileName):
155 last_commit = file(commitFileName, 'r').read()
156 else:
157 last_commit = None
158 file(commitFileName, 'w').write(commit)
159
160 if commit == last_commit:
161 log("skipping branch %s: head %s already processed" % (branch, commit))
162 else:
163 log("build branch %s: head %s is different from prev %s" % (branch, commit, last_commit))
inikepbcb9aad2016-06-22 13:07:58 +0200164 compile(branch, commit, last_commit, args.dry_run)
inikep9470b872016-06-09 12:54:06 +0200165
inikep95da7432016-06-22 12:12:35 +0200166 logFileName = working_path + "/log_" + branch.replace("/", "_")
inikep9470b872016-06-09 12:54:06 +0200167 text_to_send = []
168 results_files = ""
169 for filePath in testFilePaths:
170 fileName = filePath.rpartition('/')[2]
inikep95da7432016-06-22 12:12:35 +0200171 resultsFileName = working_path + "/results_" + branch.replace("/", "_") + "_" + fileName
inikep9470b872016-06-09 12:54:06 +0200172 last_commit, cspeed, dspeed = get_last_commit(resultsFileName)
173
174 if not args.dry_run:
inikepc1b154a2016-06-10 12:53:12 +0200175 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 +0200176 if text:
inikep95da7432016-06-22 12:12:35 +0200177 log("WARNING: redoing tests for branch %s: commit %s" % (branch, commit))
inikepc1b154a2016-06-10 12:53:12 +0200178 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 +0200179 if text:
180 text_to_send.append(text)
181 results_files += resultsFileName + " "
182 if text_to_send:
inikep95da7432016-06-22 12:12:35 +0200183 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 +0200184 except Exception as e:
185 stack = traceback.format_exc()
inikepbcb9aad2016-06-22 13:07:58 +0200186 email_topic = '%s:%s ERROR in %s:%s' % (email_header, pid, branch, commit)
inikep95da7432016-06-22 12:12:35 +0200187 send_email(args.emails, email_topic, stack, have_mutt, have_mail)
inikep2d9272f2016-06-21 19:28:51 +0200188 print(stack)
inikep9470b872016-06-09 12:54:06 +0200189
190
191if __name__ == '__main__':
192 parser = argparse.ArgumentParser()
inikepc1b154a2016-06-10 12:53:12 +0200193 parser.add_argument('testFileNames', help='file names list for speed benchmark')
194 parser.add_argument('emails', help='list of e-mail addresses to send warnings')
inikep1e375f12016-06-13 10:50:09 +0200195 parser.add_argument('--message', help='attach an additional message to e-mail', default="")
inikepd731de82016-06-21 11:26:17 +0200196 parser.add_argument('--repoURL', help='changes default repository URL', default=default_repo_url)
inikepc1b154a2016-06-10 12:53:12 +0200197 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 +0200198 parser.add_argument('--maxLoadAvg', type=float, help='maximum load average to start testing', default=0.75)
199 parser.add_argument('--lastCLevel', type=int, help='last compression level for testing', default=5)
inikepc1b154a2016-06-10 12:53:12 +0200200 parser.add_argument('--sleepTime', type=int, help='frequency of repository checking in seconds', default=300)
inikep9470b872016-06-09 12:54:06 +0200201 parser.add_argument('--dry-run', dest='dry_run', action='store_true', help='not build', default=False)
202 args = parser.parse_args()
203
204 # check if test files are accessible
205 testFileNames = args.testFileNames.split()
206 testFilePaths = []
207 for fileName in testFileNames:
208 if os.path.isfile(fileName):
209 testFilePaths.append(os.path.abspath(fileName))
210 else:
inikepd731de82016-06-21 11:26:17 +0200211 log("ERROR: File not found: " + fileName)
212 exit(1)
inikep9470b872016-06-09 12:54:06 +0200213
inikepc1b154a2016-06-10 12:53:12 +0200214 # check availability of e-mail senders
inikep2d9272f2016-06-21 19:28:51 +0200215 have_mutt = does_command_exist("mutt -h");
inikepc1b154a2016-06-10 12:53:12 +0200216 have_mail = does_command_exist("mail -V");
inikepf1690292016-06-10 13:59:08 +0200217 if not have_mutt and not have_mail:
inikepd731de82016-06-21 11:26:17 +0200218 log("ERROR: e-mail senders 'mail' or 'mutt' not found")
219 exit(1)
inikepc1b154a2016-06-10 12:53:12 +0200220
inikep2d9272f2016-06-21 19:28:51 +0200221 print("PARAMETERS:\nrepoURL=%s" % args.repoURL)
inikep95da7432016-06-22 12:12:35 +0200222 print("working_path=%s" % working_path)
inikep2d9272f2016-06-21 19:28:51 +0200223 print("clone_path=%s" % clone_path)
224 print("testFilePath(%s)=%s" % (len(testFilePaths), testFilePaths))
225 print("message=%s" % args.message)
226 print("emails=%s" % args.emails)
227 print("maxLoadAvg=%s" % args.maxLoadAvg)
228 print("lowerLimit=%s" % args.lowerLimit)
229 print("lastCLevel=%s" % args.lastCLevel)
230 print("sleepTime=%s" % args.sleepTime)
231 print("dry_run=%s" % args.dry_run)
232 print("have_mutt=%s have_mail=%s" % (have_mutt, have_mail))
inikepc1b154a2016-06-10 12:53:12 +0200233
inikepd731de82016-06-21 11:26:17 +0200234 # clone ZSTD repo if needed
inikep95da7432016-06-22 12:12:35 +0200235 if not os.path.isdir(working_path):
236 os.mkdir(working_path)
inikepd731de82016-06-21 11:26:17 +0200237 if not os.path.isdir(clone_path):
inikep95da7432016-06-22 12:12:35 +0200238 execute.cwd = working_path
inikepd731de82016-06-21 11:26:17 +0200239 execute('git clone ' + args.repoURL)
240 if not os.path.isdir(clone_path):
241 log("ERROR: ZSTD clone not found: " + clone_path)
242 exit(1)
243 execute.cwd = clone_path
244
245 # check if speedTest.pid already exists
inikepd731de82016-06-21 11:26:17 +0200246 pidfile = "./speedTest.pid"
247 if os.path.isfile(pidfile):
248 log("ERROR: %s already exists, exiting" % pidfile)
249 exit(1)
250
inikep95da7432016-06-22 12:12:35 +0200251 send_email(args.emails, email_header + ':%s test-zstd-speed.py has been started' % pid, '', have_mutt, have_mail)
252
253 file(pidfile, 'w').write(pid)
inikep9470b872016-06-09 12:54:06 +0200254 while True:
inikepd731de82016-06-21 11:26:17 +0200255 try:
256 loadavg = os.getloadavg()[0]
257 if (loadavg <= args.maxLoadAvg):
inikepbcb9aad2016-06-22 13:07:58 +0200258 branches = git_get_branches()
inikep95da7432016-06-22 12:12:35 +0200259 for branch in branches:
260 check_branch(branch, args, testFilePaths, have_mutt, have_mail)
inikepd731de82016-06-21 11:26:17 +0200261 else:
262 log("WARNING: main loadavg=%.2f is higher than %s" % (loadavg, args.maxLoadAvg))
inikep95da7432016-06-22 12:12:35 +0200263 log("sleep for %s seconds" % args.sleepTime)
264 time.sleep(args.sleepTime)
inikepd731de82016-06-21 11:26:17 +0200265 finally:
266 os.unlink(pidfile)
inikep95da7432016-06-22 12:12:35 +0200267 send_email(args.emails, email_header + ':%s test-zstd-speed.py has been stopped' % pid, '', have_mutt, have_mail)