2016-06-09 03:54:06 -07:00
|
|
|
#! /usr/bin/env python
|
|
|
|
|
|
|
|
import argparse
|
|
|
|
import os
|
|
|
|
import string
|
|
|
|
import time
|
|
|
|
import traceback
|
2016-06-22 03:12:35 -07:00
|
|
|
import subprocess
|
2016-06-22 05:01:53 -07:00
|
|
|
import signal
|
|
|
|
|
2016-06-09 03:54:06 -07:00
|
|
|
|
2016-06-21 02:26:17 -07:00
|
|
|
default_repo_url = 'https://github.com/Cyan4973/zstd.git'
|
2016-06-22 03:12:35 -07:00
|
|
|
working_dir_name = 'speedTest'
|
|
|
|
working_path = os.getcwd() + '/' + working_dir_name # /path/to/zstd/tests/speedTest
|
|
|
|
clone_path = working_path + '/' + 'zstd' # /path/to/zstd/tests/speedTest/zstd
|
2016-06-21 02:26:17 -07:00
|
|
|
email_header = '[ZSTD_speedTest]'
|
2016-06-22 03:12:35 -07:00
|
|
|
pid = str(os.getpid())
|
|
|
|
|
2016-06-09 03:54:06 -07:00
|
|
|
|
|
|
|
def log(text):
|
2016-06-21 10:28:51 -07:00
|
|
|
print(time.strftime("%Y/%m/%d %H:%M:%S") + ' - ' + text)
|
2016-06-09 03:54:06 -07:00
|
|
|
|
2016-06-21 10:28:51 -07:00
|
|
|
|
|
|
|
def execute(command, print_output=False, print_error=True, param_shell=True):
|
2016-06-09 03:54:06 -07:00
|
|
|
log("> " + command)
|
2016-06-22 03:12:35 -07:00
|
|
|
popen = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=param_shell, cwd=execute.cwd)
|
|
|
|
stdout = popen.communicate()[0]
|
|
|
|
stdout_lines = stdout.splitlines()
|
2016-06-21 10:28:51 -07:00
|
|
|
if print_output:
|
2016-06-22 03:12:35 -07:00
|
|
|
print('\n'.join(stdout_lines))
|
2016-06-09 03:54:06 -07:00
|
|
|
if popen.returncode is not None and popen.returncode != 0:
|
2016-06-10 03:53:12 -07:00
|
|
|
if not print_output and print_error:
|
2016-06-22 03:12:35 -07:00
|
|
|
print('\n'.join(stdout_lines))
|
|
|
|
raise RuntimeError('\n'.join(stdout_lines))
|
|
|
|
return stdout_lines
|
2016-06-09 03:54:06 -07:00
|
|
|
execute.cwd = None
|
|
|
|
|
|
|
|
|
2016-06-10 03:53:12 -07:00
|
|
|
def does_command_exist(command):
|
2016-06-22 03:12:35 -07:00
|
|
|
try:
|
|
|
|
execute(command, False, False);
|
|
|
|
except Exception as e:
|
|
|
|
return False
|
|
|
|
return True
|
2016-06-10 03:53:12 -07:00
|
|
|
|
|
|
|
|
2016-06-22 04:07:58 -07:00
|
|
|
def send_email(emails, topic, text, have_mutt, have_mail):
|
|
|
|
logFileName = working_path + '/' + 'tmpEmailContent'
|
|
|
|
with open(logFileName, "w") as myfile:
|
|
|
|
myfile.writelines(text)
|
|
|
|
myfile.close()
|
|
|
|
if have_mutt:
|
|
|
|
execute('mutt -s "' + topic + '" ' + emails + ' < ' + logFileName)
|
|
|
|
elif have_mail:
|
|
|
|
execute('mail -s "' + topic + '" ' + emails + ' < ' + logFileName)
|
|
|
|
else:
|
|
|
|
log("e-mail cannot be sent (mail or mutt not found)")
|
|
|
|
|
|
|
|
|
|
|
|
def send_email_with_attachments(branch, commit, last_commit, emails, text, results_files, logFileName, lower_limit, have_mutt, have_mail):
|
|
|
|
with open(logFileName, "w") as myfile:
|
|
|
|
myfile.writelines(text)
|
|
|
|
myfile.close()
|
|
|
|
email_topic = '%s:%s Warning for %s:%s last_commit=%s speed<%s' % (email_header, pid, branch, commit, last_commit, lower_limit)
|
|
|
|
if have_mutt:
|
|
|
|
execute('mutt -s "' + email_topic + '" ' + emails + ' -a ' + results_files + ' < ' + logFileName)
|
|
|
|
elif have_mail:
|
|
|
|
execute('mail -s "' + email_topic + '" ' + emails + ' < ' + logFileName)
|
|
|
|
else:
|
|
|
|
log("e-mail cannot be sent (mail or mutt not found)")
|
|
|
|
|
|
|
|
|
|
|
|
def git_get_branches():
|
2016-06-09 03:54:06 -07:00
|
|
|
execute('git fetch -p')
|
2016-06-22 03:12:35 -07:00
|
|
|
output = execute('git branch -rl')
|
2016-06-09 03:54:06 -07:00
|
|
|
for line in output:
|
|
|
|
if "HEAD" in line:
|
2016-06-21 10:28:51 -07:00
|
|
|
output.remove(line) # remove "origin/HEAD -> origin/dev"
|
2016-06-22 03:12:35 -07:00
|
|
|
return map(lambda l: l.strip(), output)
|
2016-06-09 03:54:06 -07:00
|
|
|
|
|
|
|
|
2016-06-22 06:42:26 -07:00
|
|
|
def git_get_changes(branch, commit, last_commit):
|
2016-06-09 03:54:06 -07:00
|
|
|
fmt = '--format="%h: (%an) %s, %ar"'
|
|
|
|
if last_commit is None:
|
2016-06-22 03:12:35 -07:00
|
|
|
commits = execute('git log -n 10 %s %s' % (fmt, commit))
|
2016-06-09 03:54:06 -07:00
|
|
|
else:
|
2016-06-22 03:12:35 -07:00
|
|
|
commits = execute('git --no-pager log %s %s..%s' % (fmt, last_commit, commit))
|
2016-06-22 06:42:26 -07:00
|
|
|
return str('Changes in %s since %s:\n' % (branch, last_commit)) + '\n'.join(commits)
|
2016-06-22 04:07:58 -07:00
|
|
|
|
2016-06-09 03:54:06 -07:00
|
|
|
|
2016-06-22 04:07:58 -07:00
|
|
|
def compile(branch, commit, last_commit, dry_run):
|
2016-06-09 03:54:06 -07:00
|
|
|
local_branch = string.split(branch, '/')[1]
|
|
|
|
version = local_branch.rpartition('-')[2]
|
|
|
|
version = version + '_' + commit
|
|
|
|
execute('git checkout -- . && git checkout ' + branch)
|
2016-06-22 06:42:26 -07:00
|
|
|
print(git_get_changes(branch, commit, last_commit))
|
2016-06-09 03:54:06 -07:00
|
|
|
if not dry_run:
|
2016-06-22 07:13:25 -07:00
|
|
|
execute('make clean zstdprogram MOREFLAGS="-DZSTD_GIT_COMMIT=%s"' % version)
|
2016-06-09 03:54:06 -07:00
|
|
|
|
|
|
|
|
2016-06-22 05:01:53 -07:00
|
|
|
def get_last_results(resultsFileName):
|
2016-06-09 03:54:06 -07:00
|
|
|
if not os.path.isfile(resultsFileName):
|
|
|
|
return None, None, None
|
|
|
|
commit = None
|
|
|
|
cspeed = []
|
|
|
|
dspeed = []
|
|
|
|
with open(resultsFileName,'r') as f:
|
|
|
|
for line in f:
|
|
|
|
words = line.split()
|
2016-06-22 04:07:58 -07:00
|
|
|
if len(words) == 2: # branch + commit
|
2016-06-09 03:54:06 -07:00
|
|
|
commit = words[1];
|
|
|
|
cspeed = []
|
|
|
|
dspeed = []
|
2016-06-22 04:07:58 -07:00
|
|
|
if (len(words) == 8): # results
|
2016-06-09 03:54:06 -07:00
|
|
|
cspeed.append(float(words[3]))
|
|
|
|
dspeed.append(float(words[5]))
|
|
|
|
return commit, cspeed, dspeed
|
|
|
|
|
|
|
|
|
2016-06-10 03:53:12 -07:00
|
|
|
def benchmark_and_compare(branch, commit, resultsFileName, lastCLevel, testFilePath, fileName, last_cspeed, last_dspeed, lower_limit, maxLoadAvg, message):
|
|
|
|
sleepTime = 30
|
2016-06-09 03:54:06 -07:00
|
|
|
while os.getloadavg()[0] > maxLoadAvg:
|
2016-06-10 03:53:12 -07:00
|
|
|
log("WARNING: bench loadavg=%.2f is higher than %s, sleeping for %s seconds" % (os.getloadavg()[0], maxLoadAvg, sleepTime))
|
|
|
|
time.sleep(sleepTime)
|
2016-06-09 03:54:06 -07:00
|
|
|
start_load = str(os.getloadavg())
|
2016-06-22 03:12:35 -07:00
|
|
|
result = execute('programs/zstd -qi5b1e' + str(lastCLevel) + ' ' + testFilePath, print_output=True)
|
2016-06-09 03:54:06 -07:00
|
|
|
end_load = str(os.getloadavg())
|
|
|
|
linesExpected = lastCLevel + 2;
|
|
|
|
if len(result) != linesExpected:
|
2016-06-22 04:07:58 -07:00
|
|
|
raise RuntimeError("ERROR: number of result lines=%d is different that expected %d\n%s" % (len(result), linesExpected, '\n'.join(result)))
|
2016-06-09 03:54:06 -07:00
|
|
|
with open(resultsFileName, "a") as myfile:
|
|
|
|
myfile.write(branch + " " + commit + "\n")
|
2016-06-22 03:12:35 -07:00
|
|
|
myfile.write('\n'.join(result) + '\n')
|
2016-06-09 03:54:06 -07:00
|
|
|
myfile.close()
|
|
|
|
if (last_cspeed == None):
|
2016-06-22 03:12:35 -07:00
|
|
|
log("WARNING: No data for comparison for branch=%s file=%s " % (branch, fileName))
|
2016-06-09 03:54:06 -07:00
|
|
|
return ""
|
2016-06-22 05:01:53 -07:00
|
|
|
commit, cspeed, dspeed = get_last_results(resultsFileName)
|
2016-06-09 03:54:06 -07:00
|
|
|
text = ""
|
|
|
|
for i in range(0, min(len(cspeed), len(last_cspeed))):
|
2016-06-22 04:07:58 -07:00
|
|
|
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))
|
2016-06-09 03:54:06 -07:00
|
|
|
if (cspeed[i]/last_cspeed[i] < lower_limit):
|
2016-06-22 04:07:58 -07:00
|
|
|
text += "WARNING: -%d cspeed=%.2f clast=%.2f cdiff=%.4f %s\n" % (i+1, cspeed[i], last_cspeed[i], cspeed[i]/last_cspeed[i], fileName)
|
2016-06-09 03:54:06 -07:00
|
|
|
if (dspeed[i]/last_dspeed[i] < lower_limit):
|
2016-06-22 04:07:58 -07:00
|
|
|
text += "WARNING: -%d dspeed=%.2f dlast=%.2f ddiff=%.4f %s\n" % (i+1, dspeed[i], last_dspeed[i], dspeed[i]/last_dspeed[i], fileName)
|
2016-06-09 03:54:06 -07:00
|
|
|
if text:
|
2016-06-10 03:53:12 -07:00
|
|
|
text = message + ("\nmaxLoadAvg=%s load average at start=%s end=%s\n" % (maxLoadAvg, start_load, end_load)) + text
|
2016-06-09 03:54:06 -07:00
|
|
|
return text
|
|
|
|
|
|
|
|
|
2016-06-22 07:13:25 -07:00
|
|
|
def check_commit(branch, commit, args, testFilePaths, have_mutt, have_mail):
|
|
|
|
try:
|
|
|
|
last_commit = None
|
|
|
|
commitFileName = working_path + "/commit_" + branch.replace("/", "_")
|
|
|
|
if os.path.isfile(commitFileName):
|
|
|
|
last_commit = file(commitFileName, 'r').read()
|
|
|
|
file(commitFileName, 'w').write(commit)
|
|
|
|
|
|
|
|
if commit == last_commit:
|
|
|
|
log("skipping branch %s: head %s already processed" % (branch, commit))
|
|
|
|
return
|
|
|
|
|
|
|
|
log("build branch %s: head %s is different from prev %s" % (branch, commit, last_commit))
|
|
|
|
compile(branch, commit, last_commit, args.dry_run)
|
|
|
|
|
|
|
|
logFileName = working_path + "/log_" + branch.replace("/", "_")
|
|
|
|
text_to_send = []
|
|
|
|
results_files = ""
|
|
|
|
for filePath in testFilePaths:
|
|
|
|
fileName = filePath.rpartition('/')[2]
|
|
|
|
resultsFileName = working_path + "/results_" + branch.replace("/", "_") + "_" + fileName
|
|
|
|
last_commit, cspeed, dspeed = get_last_results(resultsFileName)
|
|
|
|
|
|
|
|
if not args.dry_run:
|
|
|
|
text = benchmark_and_compare(branch, commit, resultsFileName, args.lastCLevel, filePath, fileName, cspeed, dspeed, args.lowerLimit, args.maxLoadAvg, args.message)
|
|
|
|
if text:
|
|
|
|
log("WARNING: redoing tests for branch %s: commit %s" % (branch, commit))
|
2016-06-22 05:01:53 -07:00
|
|
|
text = benchmark_and_compare(branch, commit, resultsFileName, args.lastCLevel, filePath, fileName, cspeed, dspeed, args.lowerLimit, args.maxLoadAvg, args.message)
|
|
|
|
if text:
|
2016-06-22 07:13:25 -07:00
|
|
|
text_to_send.append(text)
|
|
|
|
results_files += resultsFileName + " "
|
|
|
|
if text_to_send:
|
|
|
|
send_email_with_attachments(branch, commit, last_commit, args.emails, text_to_send, results_files, logFileName, args.lowerLimit, have_mutt, have_mail)
|
|
|
|
except Exception as e:
|
|
|
|
stack = traceback.format_exc()
|
|
|
|
email_topic = '%s:%s ERROR in %s:%s' % (email_header, pid, branch, commit)
|
|
|
|
send_email(args.emails, email_topic, stack, have_mutt, have_mail)
|
|
|
|
print(stack)
|
2016-06-09 03:54:06 -07:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
parser = argparse.ArgumentParser()
|
2016-06-10 03:53:12 -07:00
|
|
|
parser.add_argument('testFileNames', help='file names list for speed benchmark')
|
|
|
|
parser.add_argument('emails', help='list of e-mail addresses to send warnings')
|
2016-06-13 01:50:09 -07:00
|
|
|
parser.add_argument('--message', help='attach an additional message to e-mail', default="")
|
2016-06-21 02:26:17 -07:00
|
|
|
parser.add_argument('--repoURL', help='changes default repository URL', default=default_repo_url)
|
2016-06-10 03:53:12 -07:00
|
|
|
parser.add_argument('--lowerLimit', type=float, help='send email if speed is lower than given limit', default=0.98)
|
2016-06-09 03:54:06 -07:00
|
|
|
parser.add_argument('--maxLoadAvg', type=float, help='maximum load average to start testing', default=0.75)
|
|
|
|
parser.add_argument('--lastCLevel', type=int, help='last compression level for testing', default=5)
|
2016-06-10 03:53:12 -07:00
|
|
|
parser.add_argument('--sleepTime', type=int, help='frequency of repository checking in seconds', default=300)
|
2016-06-09 03:54:06 -07:00
|
|
|
parser.add_argument('--dry-run', dest='dry_run', action='store_true', help='not build', default=False)
|
|
|
|
args = parser.parse_args()
|
|
|
|
|
|
|
|
# check if test files are accessible
|
|
|
|
testFileNames = args.testFileNames.split()
|
|
|
|
testFilePaths = []
|
|
|
|
for fileName in testFileNames:
|
|
|
|
if os.path.isfile(fileName):
|
|
|
|
testFilePaths.append(os.path.abspath(fileName))
|
|
|
|
else:
|
2016-06-21 02:26:17 -07:00
|
|
|
log("ERROR: File not found: " + fileName)
|
|
|
|
exit(1)
|
2016-06-09 03:54:06 -07:00
|
|
|
|
2016-06-10 03:53:12 -07:00
|
|
|
# check availability of e-mail senders
|
2016-06-21 10:28:51 -07:00
|
|
|
have_mutt = does_command_exist("mutt -h");
|
2016-06-10 03:53:12 -07:00
|
|
|
have_mail = does_command_exist("mail -V");
|
2016-06-10 04:59:08 -07:00
|
|
|
if not have_mutt and not have_mail:
|
2016-06-21 02:26:17 -07:00
|
|
|
log("ERROR: e-mail senders 'mail' or 'mutt' not found")
|
|
|
|
exit(1)
|
2016-06-09 03:54:06 -07:00
|
|
|
|
2016-06-21 10:28:51 -07:00
|
|
|
print("PARAMETERS:\nrepoURL=%s" % args.repoURL)
|
2016-06-22 03:12:35 -07:00
|
|
|
print("working_path=%s" % working_path)
|
2016-06-21 10:28:51 -07:00
|
|
|
print("clone_path=%s" % clone_path)
|
|
|
|
print("testFilePath(%s)=%s" % (len(testFilePaths), testFilePaths))
|
|
|
|
print("message=%s" % args.message)
|
|
|
|
print("emails=%s" % args.emails)
|
|
|
|
print("maxLoadAvg=%s" % args.maxLoadAvg)
|
|
|
|
print("lowerLimit=%s" % args.lowerLimit)
|
|
|
|
print("lastCLevel=%s" % args.lastCLevel)
|
|
|
|
print("sleepTime=%s" % args.sleepTime)
|
|
|
|
print("dry_run=%s" % args.dry_run)
|
|
|
|
print("have_mutt=%s have_mail=%s" % (have_mutt, have_mail))
|
2016-06-10 03:53:12 -07:00
|
|
|
|
2016-06-21 02:26:17 -07:00
|
|
|
# clone ZSTD repo if needed
|
2016-06-22 03:12:35 -07:00
|
|
|
if not os.path.isdir(working_path):
|
|
|
|
os.mkdir(working_path)
|
2016-06-21 02:26:17 -07:00
|
|
|
if not os.path.isdir(clone_path):
|
2016-06-22 03:12:35 -07:00
|
|
|
execute.cwd = working_path
|
2016-06-21 02:26:17 -07:00
|
|
|
execute('git clone ' + args.repoURL)
|
|
|
|
if not os.path.isdir(clone_path):
|
|
|
|
log("ERROR: ZSTD clone not found: " + clone_path)
|
|
|
|
exit(1)
|
|
|
|
execute.cwd = clone_path
|
|
|
|
|
|
|
|
# check if speedTest.pid already exists
|
|
|
|
pidfile = "./speedTest.pid"
|
|
|
|
if os.path.isfile(pidfile):
|
|
|
|
log("ERROR: %s already exists, exiting" % pidfile)
|
|
|
|
exit(1)
|
|
|
|
|
2016-06-22 05:01:53 -07:00
|
|
|
send_email(args.emails, email_header + ':%s test-zstd-speed.py has been started' % pid, '', have_mutt, have_mail)
|
2016-06-22 03:12:35 -07:00
|
|
|
file(pidfile, 'w').write(pid)
|
2016-06-22 05:01:53 -07:00
|
|
|
|
2016-06-09 03:54:06 -07:00
|
|
|
while True:
|
2016-06-21 02:26:17 -07:00
|
|
|
try:
|
|
|
|
loadavg = os.getloadavg()[0]
|
|
|
|
if (loadavg <= args.maxLoadAvg):
|
2016-06-22 04:07:58 -07:00
|
|
|
branches = git_get_branches()
|
2016-06-22 03:12:35 -07:00
|
|
|
for branch in branches:
|
2016-06-22 07:13:25 -07:00
|
|
|
commits = execute('git show -s --format=%h ' + branch)[0]
|
|
|
|
for commit in [commits]:
|
|
|
|
check_commit(branch, commit, args, testFilePaths, have_mutt, have_mail)
|
2016-06-21 02:26:17 -07:00
|
|
|
else:
|
|
|
|
log("WARNING: main loadavg=%.2f is higher than %s" % (loadavg, args.maxLoadAvg))
|
2016-06-22 03:12:35 -07:00
|
|
|
log("sleep for %s seconds" % args.sleepTime)
|
|
|
|
time.sleep(args.sleepTime)
|
2016-06-22 05:01:53 -07:00
|
|
|
except KeyboardInterrupt:
|
2016-06-21 02:26:17 -07:00
|
|
|
os.unlink(pidfile)
|
2016-06-22 05:01:53 -07:00
|
|
|
send_email(args.emails, email_header + ':%s test-zstd-speed.py has been stopped' % pid, '', have_mutt, have_mail)
|
|
|
|
exit(0)
|