| inikep | 9470b87 | 2016-06-09 12:54:06 +0200 | [diff] [blame] | 1 | #! /usr/bin/env python |
| 2 | # execute(), fetch(), notify() are based on https://github.com/getlantern/build-automation/blob/master/build.py |
| 3 | |
| 4 | import argparse |
| 5 | import os |
| 6 | import string |
| 7 | import time |
| 8 | import traceback |
| 9 | from subprocess import Popen, PIPE |
| 10 | |
| inikep | d731de8 | 2016-06-21 11:26:17 +0200 | [diff] [blame] | 11 | default_repo_url = 'https://github.com/Cyan4973/zstd.git' |
| inikep | 9470b87 | 2016-06-09 12:54:06 +0200 | [diff] [blame] | 12 | test_dir_name = 'speedTest' |
| inikep | d731de8 | 2016-06-21 11:26:17 +0200 | [diff] [blame] | 13 | email_header = '[ZSTD_speedTest]' |
| inikep | 9470b87 | 2016-06-09 12:54:06 +0200 | [diff] [blame] | 14 | |
| 15 | def log(text): |
| inikep | 2d9272f | 2016-06-21 19:28:51 +0200 | [diff] [blame^] | 16 | print(time.strftime("%Y/%m/%d %H:%M:%S") + ' - ' + text) |
| inikep | 9470b87 | 2016-06-09 12:54:06 +0200 | [diff] [blame] | 17 | |
| inikep | 2d9272f | 2016-06-21 19:28:51 +0200 | [diff] [blame^] | 18 | |
| 19 | def execute(command, print_output=False, print_error=True, param_shell=True): |
| inikep | 9470b87 | 2016-06-09 12:54:06 +0200 | [diff] [blame] | 20 | log("> " + command) |
| inikep | 2d9272f | 2016-06-21 19:28:51 +0200 | [diff] [blame^] | 21 | popen = Popen(command, stdout=PIPE, stderr=PIPE, shell=param_shell, cwd=execute.cwd) |
| inikep | 9470b87 | 2016-06-09 12:54:06 +0200 | [diff] [blame] | 22 | itout = iter(popen.stdout.readline, b"") |
| 23 | iterr = iter(popen.stderr.readline, b"") |
| 24 | stdout_lines = list(itout) |
| inikep | 9470b87 | 2016-06-09 12:54:06 +0200 | [diff] [blame] | 25 | stderr_lines = list(iterr) |
| inikep | 9470b87 | 2016-06-09 12:54:06 +0200 | [diff] [blame] | 26 | popen.communicate() |
| inikep | 2d9272f | 2016-06-21 19:28:51 +0200 | [diff] [blame^] | 27 | if print_output: |
| 28 | print(''.join(stdout_lines)) |
| 29 | print(''.join(stderr_lines)) |
| inikep | 9470b87 | 2016-06-09 12:54:06 +0200 | [diff] [blame] | 30 | if popen.returncode is not None and popen.returncode != 0: |
| inikep | c1b154a | 2016-06-10 12:53:12 +0200 | [diff] [blame] | 31 | if not print_output and print_error: |
| inikep | 2d9272f | 2016-06-21 19:28:51 +0200 | [diff] [blame^] | 32 | print(''.join(stderr_lines)) |
| 33 | return popen.returncode, stdout_lines, stderr_lines |
| inikep | 9470b87 | 2016-06-09 12:54:06 +0200 | [diff] [blame] | 34 | execute.cwd = None |
| 35 | |
| 36 | |
| inikep | c1b154a | 2016-06-10 12:53:12 +0200 | [diff] [blame] | 37 | def does_command_exist(command): |
| inikep | 2d9272f | 2016-06-21 19:28:51 +0200 | [diff] [blame^] | 38 | result, stdoutdata, stderrdata = execute(command, False, False); |
| 39 | return result == 0 |
| inikep | c1b154a | 2016-06-10 12:53:12 +0200 | [diff] [blame] | 40 | |
| 41 | |
| inikep | 9470b87 | 2016-06-09 12:54:06 +0200 | [diff] [blame] | 42 | def fetch(): |
| 43 | execute('git fetch -p') |
| inikep | 2d9272f | 2016-06-21 19:28:51 +0200 | [diff] [blame^] | 44 | returncode, output, stderrdata = execute('git branch -rl') |
| inikep | 9470b87 | 2016-06-09 12:54:06 +0200 | [diff] [blame] | 45 | for line in output: |
| 46 | if "HEAD" in line: |
| inikep | 2d9272f | 2016-06-21 19:28:51 +0200 | [diff] [blame^] | 47 | output.remove(line) # remove "origin/HEAD -> origin/dev" |
| inikep | 9470b87 | 2016-06-09 12:54:06 +0200 | [diff] [blame] | 48 | branches = map(lambda l: l.strip(), output) |
| inikep | 2d9272f | 2016-06-21 19:28:51 +0200 | [diff] [blame^] | 49 | print("branches=%s" % branches) |
| 50 | return map(lambda b: (b, execute('git show -s --format=%h ' + b)[1][0].strip()), branches) |
| inikep | 9470b87 | 2016-06-09 12:54:06 +0200 | [diff] [blame] | 51 | |
| 52 | |
| 53 | def notify(branch, commit, last_commit): |
| 54 | text_tmpl = string.Template('Changes since $last_commit:\r\n$commits') |
| 55 | branch = branch.split('/')[1] |
| 56 | fmt = '--format="%h: (%an) %s, %ar"' |
| 57 | if last_commit is None: |
| inikep | 2d9272f | 2016-06-21 19:28:51 +0200 | [diff] [blame^] | 58 | returncode, commits, stderrdata = execute('git log -n 10 %s %s' % (fmt, commit)) |
| inikep | 9470b87 | 2016-06-09 12:54:06 +0200 | [diff] [blame] | 59 | else: |
| inikep | 2d9272f | 2016-06-21 19:28:51 +0200 | [diff] [blame^] | 60 | returncode, commits, stderrdata = execute('git --no-pager log %s %s..%s' % (fmt, last_commit, commit)) |
| inikep | 9470b87 | 2016-06-09 12:54:06 +0200 | [diff] [blame] | 61 | text = text_tmpl.substitute({'last_commit': last_commit, 'commits': ''.join(commits)}) |
| inikep | 2d9272f | 2016-06-21 19:28:51 +0200 | [diff] [blame^] | 62 | print(str("commits for %s: %s" % (commit, text))) |
| inikep | 9470b87 | 2016-06-09 12:54:06 +0200 | [diff] [blame] | 63 | |
| inikep | 2d9272f | 2016-06-21 19:28:51 +0200 | [diff] [blame^] | 64 | |
| inikep | 9470b87 | 2016-06-09 12:54:06 +0200 | [diff] [blame] | 65 | def compile(branch, commit, dry_run): |
| 66 | local_branch = string.split(branch, '/')[1] |
| 67 | version = local_branch.rpartition('-')[2] |
| 68 | version = version + '_' + commit |
| 69 | execute('git checkout -- . && git checkout ' + branch) |
| 70 | if not dry_run: |
| 71 | execute('VERSION=' + version + '; make clean zstdprogram') |
| 72 | |
| 73 | |
| 74 | def get_last_commit(resultsFileName): |
| 75 | if not os.path.isfile(resultsFileName): |
| 76 | return None, None, None |
| 77 | commit = None |
| 78 | cspeed = [] |
| 79 | dspeed = [] |
| 80 | with open(resultsFileName,'r') as f: |
| 81 | for line in f: |
| 82 | words = line.split() |
| 83 | if len(words) == 2: # branch + commit |
| 84 | commit = words[1]; |
| 85 | cspeed = [] |
| 86 | dspeed = [] |
| 87 | if (len(words) == 8): |
| 88 | cspeed.append(float(words[3])) |
| 89 | dspeed.append(float(words[5])) |
| 90 | #if commit != None: |
| 91 | # print "commit=%s cspeed=%s dspeed=%s" % (commit, cspeed, dspeed) |
| 92 | return commit, cspeed, dspeed |
| 93 | |
| 94 | |
| inikep | c1b154a | 2016-06-10 12:53:12 +0200 | [diff] [blame] | 95 | def benchmark_and_compare(branch, commit, resultsFileName, lastCLevel, testFilePath, fileName, last_cspeed, last_dspeed, lower_limit, maxLoadAvg, message): |
| 96 | sleepTime = 30 |
| inikep | 9470b87 | 2016-06-09 12:54:06 +0200 | [diff] [blame] | 97 | while os.getloadavg()[0] > maxLoadAvg: |
| inikep | c1b154a | 2016-06-10 12:53:12 +0200 | [diff] [blame] | 98 | log("WARNING: bench loadavg=%.2f is higher than %s, sleeping for %s seconds" % (os.getloadavg()[0], maxLoadAvg, sleepTime)) |
| 99 | time.sleep(sleepTime) |
| inikep | 9470b87 | 2016-06-09 12:54:06 +0200 | [diff] [blame] | 100 | start_load = str(os.getloadavg()) |
| inikep | 2d9272f | 2016-06-21 19:28:51 +0200 | [diff] [blame^] | 101 | returncode, result, stderrdata = execute('programs/zstd -qi5b1e' + str(lastCLevel) + ' ' + testFilePath) |
| inikep | 9470b87 | 2016-06-09 12:54:06 +0200 | [diff] [blame] | 102 | end_load = str(os.getloadavg()) |
| 103 | linesExpected = lastCLevel + 2; |
| 104 | if len(result) != linesExpected: |
| inikep | c1b154a | 2016-06-10 12:53:12 +0200 | [diff] [blame] | 105 | log("ERROR: number of result lines=%d is different that expected %d" % (len(result), linesExpected)) |
| inikep | 9470b87 | 2016-06-09 12:54:06 +0200 | [diff] [blame] | 106 | return "" |
| 107 | with open(resultsFileName, "a") as myfile: |
| 108 | myfile.write(branch + " " + commit + "\n") |
| 109 | myfile.writelines(result) |
| 110 | myfile.close() |
| 111 | if (last_cspeed == None): |
| 112 | return "" |
| 113 | commit, cspeed, dspeed = get_last_commit(resultsFileName) |
| 114 | text = "" |
| 115 | for i in range(0, min(len(cspeed), len(last_cspeed))): |
| 116 | if (cspeed[i]/last_cspeed[i] < lower_limit): |
| 117 | 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]) |
| 118 | if (dspeed[i]/last_dspeed[i] < lower_limit): |
| 119 | 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]) |
| 120 | if text: |
| inikep | c1b154a | 2016-06-10 12:53:12 +0200 | [diff] [blame] | 121 | text = message + ("\nmaxLoadAvg=%s load average at start=%s end=%s\n" % (maxLoadAvg, start_load, end_load)) + text |
| inikep | 9470b87 | 2016-06-09 12:54:06 +0200 | [diff] [blame] | 122 | return text |
| 123 | |
| 124 | |
| inikep | d731de8 | 2016-06-21 11:26:17 +0200 | [diff] [blame] | 125 | def send_simple_email(emails, email_topic, have_mutt, have_mail): |
| 126 | if have_mutt: |
| 127 | execute('mutt -s "' + email_header + ' ' + email_topic + '" ' + emails + ' </dev/null') |
| 128 | elif have_mail: |
| 129 | execute('mail -s "' + email_header + ' ' + email_topic + '" ' + emails + ' </dev/null') |
| 130 | else: |
| 131 | log("e-mail cannot be sent (mail and mutt not found)") |
| 132 | |
| 133 | |
| inikep | c1b154a | 2016-06-10 12:53:12 +0200 | [diff] [blame] | 134 | def send_email(branch, commit, last_commit, emails, text, results_files, logFileName, lower_limit, have_mutt, have_mail): |
| inikep | 9470b87 | 2016-06-09 12:54:06 +0200 | [diff] [blame] | 135 | with open(logFileName, "w") as myfile: |
| 136 | myfile.writelines(text) |
| 137 | myfile.close() |
| inikep | c1b154a | 2016-06-10 12:53:12 +0200 | [diff] [blame] | 138 | if have_mutt: |
| inikep | d731de8 | 2016-06-21 11:26:17 +0200 | [diff] [blame] | 139 | execute('mutt -s "' + email_header + ' Warning for branch=' + branch + ' commit=' + commit + ' last_commit=' + last_commit + ' speed<' + str(lower_limit) + '" ' + emails + ' -a ' + results_files + ' < ' + logFileName) |
| inikep | c1b154a | 2016-06-10 12:53:12 +0200 | [diff] [blame] | 140 | elif have_mail: |
| inikep | d731de8 | 2016-06-21 11:26:17 +0200 | [diff] [blame] | 141 | execute('mail -s "' + email_header + ' Warning for branch=' + branch + ' commit=' + commit + ' last_commit=' + last_commit + ' speed<' + str(lower_limit) + '" ' + emails + ' < ' + logFileName) |
| inikep | c1b154a | 2016-06-10 12:53:12 +0200 | [diff] [blame] | 142 | else: |
| 143 | log("e-mail cannot be sent (mail and mutt not found)") |
| inikep | 9470b87 | 2016-06-09 12:54:06 +0200 | [diff] [blame] | 144 | |
| 145 | |
| inikep | c1b154a | 2016-06-10 12:53:12 +0200 | [diff] [blame] | 146 | def check_branches(args, test_path, testFilePaths, have_mutt, have_mail): |
| inikep | 9470b87 | 2016-06-09 12:54:06 +0200 | [diff] [blame] | 147 | for branch, commit in fetch(): |
| inikep | 9470b87 | 2016-06-09 12:54:06 +0200 | [diff] [blame] | 148 | try: |
| 149 | commitFileName = test_path + "/commit_" + branch.replace("/", "_") |
| 150 | if os.path.isfile(commitFileName): |
| 151 | last_commit = file(commitFileName, 'r').read() |
| 152 | else: |
| 153 | last_commit = None |
| 154 | file(commitFileName, 'w').write(commit) |
| 155 | |
| 156 | if commit == last_commit: |
| 157 | log("skipping branch %s: head %s already processed" % (branch, commit)) |
| 158 | else: |
| 159 | log("build branch %s: head %s is different from prev %s" % (branch, commit, last_commit)) |
| 160 | compile(branch, commit, args.dry_run) |
| 161 | |
| 162 | logFileName = test_path + "/log_" + branch.replace("/", "_") |
| 163 | text_to_send = [] |
| 164 | results_files = "" |
| 165 | for filePath in testFilePaths: |
| 166 | fileName = filePath.rpartition('/')[2] |
| 167 | resultsFileName = test_path + "/results_" + branch.replace("/", "_") + "_" + fileName |
| 168 | last_commit, cspeed, dspeed = get_last_commit(resultsFileName) |
| 169 | |
| 170 | if not args.dry_run: |
| inikep | c1b154a | 2016-06-10 12:53:12 +0200 | [diff] [blame] | 171 | text = benchmark_and_compare(branch, commit, resultsFileName, args.lastCLevel, filePath, fileName, cspeed, dspeed, args.lowerLimit, args.maxLoadAvg, args.message) |
| inikep | 9470b87 | 2016-06-09 12:54:06 +0200 | [diff] [blame] | 172 | if text: |
| inikep | c1b154a | 2016-06-10 12:53:12 +0200 | [diff] [blame] | 173 | text = benchmark_and_compare(branch, commit, resultsFileName, args.lastCLevel, filePath, fileName, cspeed, dspeed, args.lowerLimit, args.maxLoadAvg, args.message) |
| inikep | 9470b87 | 2016-06-09 12:54:06 +0200 | [diff] [blame] | 174 | if text: |
| 175 | text_to_send.append(text) |
| 176 | results_files += resultsFileName + " " |
| 177 | if text_to_send: |
| inikep | c1b154a | 2016-06-10 12:53:12 +0200 | [diff] [blame] | 178 | send_email(branch, commit, last_commit, args.emails, text_to_send, results_files, logFileName, args.lowerLimit, have_mutt, have_mail) |
| inikep | 9470b87 | 2016-06-09 12:54:06 +0200 | [diff] [blame] | 179 | notify(branch, commit, last_commit) |
| 180 | except Exception as e: |
| 181 | stack = traceback.format_exc() |
| inikep | c1b154a | 2016-06-10 12:53:12 +0200 | [diff] [blame] | 182 | log("ERROR: build %s, error %s" % (branch, str(e)) ) |
| inikep | 2d9272f | 2016-06-21 19:28:51 +0200 | [diff] [blame^] | 183 | print(stack) |
| inikep | 9470b87 | 2016-06-09 12:54:06 +0200 | [diff] [blame] | 184 | |
| 185 | |
| 186 | if __name__ == '__main__': |
| 187 | parser = argparse.ArgumentParser() |
| inikep | c1b154a | 2016-06-10 12:53:12 +0200 | [diff] [blame] | 188 | parser.add_argument('testFileNames', help='file names list for speed benchmark') |
| 189 | parser.add_argument('emails', help='list of e-mail addresses to send warnings') |
| inikep | 1e375f1 | 2016-06-13 10:50:09 +0200 | [diff] [blame] | 190 | parser.add_argument('--message', help='attach an additional message to e-mail', default="") |
| inikep | d731de8 | 2016-06-21 11:26:17 +0200 | [diff] [blame] | 191 | parser.add_argument('--repoURL', help='changes default repository URL', default=default_repo_url) |
| inikep | c1b154a | 2016-06-10 12:53:12 +0200 | [diff] [blame] | 192 | parser.add_argument('--lowerLimit', type=float, help='send email if speed is lower than given limit', default=0.98) |
| inikep | 9470b87 | 2016-06-09 12:54:06 +0200 | [diff] [blame] | 193 | parser.add_argument('--maxLoadAvg', type=float, help='maximum load average to start testing', default=0.75) |
| 194 | parser.add_argument('--lastCLevel', type=int, help='last compression level for testing', default=5) |
| inikep | c1b154a | 2016-06-10 12:53:12 +0200 | [diff] [blame] | 195 | parser.add_argument('--sleepTime', type=int, help='frequency of repository checking in seconds', default=300) |
| inikep | 9470b87 | 2016-06-09 12:54:06 +0200 | [diff] [blame] | 196 | parser.add_argument('--dry-run', dest='dry_run', action='store_true', help='not build', default=False) |
| 197 | args = parser.parse_args() |
| 198 | |
| 199 | # check if test files are accessible |
| 200 | testFileNames = args.testFileNames.split() |
| 201 | testFilePaths = [] |
| 202 | for fileName in testFileNames: |
| 203 | if os.path.isfile(fileName): |
| 204 | testFilePaths.append(os.path.abspath(fileName)) |
| 205 | else: |
| inikep | d731de8 | 2016-06-21 11:26:17 +0200 | [diff] [blame] | 206 | log("ERROR: File not found: " + fileName) |
| 207 | exit(1) |
| inikep | 9470b87 | 2016-06-09 12:54:06 +0200 | [diff] [blame] | 208 | |
| 209 | test_path = os.getcwd() + '/' + test_dir_name # /path/to/zstd/tests/speedTest |
| 210 | clone_path = test_path + '/' + 'zstd' # /path/to/zstd/tests/speedTest/zstd |
| inikep | 9470b87 | 2016-06-09 12:54:06 +0200 | [diff] [blame] | 211 | |
| inikep | c1b154a | 2016-06-10 12:53:12 +0200 | [diff] [blame] | 212 | # check availability of e-mail senders |
| inikep | 2d9272f | 2016-06-21 19:28:51 +0200 | [diff] [blame^] | 213 | have_mutt = does_command_exist("mutt -h"); |
| inikep | c1b154a | 2016-06-10 12:53:12 +0200 | [diff] [blame] | 214 | have_mail = does_command_exist("mail -V"); |
| inikep | f169029 | 2016-06-10 13:59:08 +0200 | [diff] [blame] | 215 | if not have_mutt and not have_mail: |
| inikep | d731de8 | 2016-06-21 11:26:17 +0200 | [diff] [blame] | 216 | log("ERROR: e-mail senders 'mail' or 'mutt' not found") |
| 217 | exit(1) |
| inikep | c1b154a | 2016-06-10 12:53:12 +0200 | [diff] [blame] | 218 | |
| inikep | 2d9272f | 2016-06-21 19:28:51 +0200 | [diff] [blame^] | 219 | print("PARAMETERS:\nrepoURL=%s" % args.repoURL) |
| 220 | print("test_path=%s" % test_path) |
| 221 | print("clone_path=%s" % clone_path) |
| 222 | print("testFilePath(%s)=%s" % (len(testFilePaths), testFilePaths)) |
| 223 | print("message=%s" % args.message) |
| 224 | print("emails=%s" % args.emails) |
| 225 | print("maxLoadAvg=%s" % args.maxLoadAvg) |
| 226 | print("lowerLimit=%s" % args.lowerLimit) |
| 227 | print("lastCLevel=%s" % args.lastCLevel) |
| 228 | print("sleepTime=%s" % args.sleepTime) |
| 229 | print("dry_run=%s" % args.dry_run) |
| 230 | print("have_mutt=%s have_mail=%s" % (have_mutt, have_mail)) |
| inikep | c1b154a | 2016-06-10 12:53:12 +0200 | [diff] [blame] | 231 | |
| inikep | d731de8 | 2016-06-21 11:26:17 +0200 | [diff] [blame] | 232 | # clone ZSTD repo if needed |
| 233 | if not os.path.isdir(test_path): |
| 234 | os.mkdir(test_path) |
| 235 | if not os.path.isdir(clone_path): |
| 236 | execute.cwd = test_path |
| 237 | execute('git clone ' + args.repoURL) |
| 238 | if not os.path.isdir(clone_path): |
| 239 | log("ERROR: ZSTD clone not found: " + clone_path) |
| 240 | exit(1) |
| 241 | execute.cwd = clone_path |
| 242 | |
| 243 | # check if speedTest.pid already exists |
| 244 | pid = str(os.getpid()) |
| 245 | pidfile = "./speedTest.pid" |
| 246 | if os.path.isfile(pidfile): |
| 247 | log("ERROR: %s already exists, exiting" % pidfile) |
| 248 | exit(1) |
| 249 | |
| 250 | send_simple_email(args.emails, "test-zstd-speed.py(%s) has been started" % pid, have_mutt, have_mail) |
| inikep | 9470b87 | 2016-06-09 12:54:06 +0200 | [diff] [blame] | 251 | while True: |
| inikep | d731de8 | 2016-06-21 11:26:17 +0200 | [diff] [blame] | 252 | file(pidfile, 'w').write(pid) |
| 253 | try: |
| 254 | loadavg = os.getloadavg()[0] |
| 255 | if (loadavg <= args.maxLoadAvg): |
| 256 | check_branches(args, test_path, testFilePaths, have_mutt, have_mail) |
| 257 | else: |
| 258 | log("WARNING: main loadavg=%.2f is higher than %s" % (loadavg, args.maxLoadAvg)) |
| 259 | finally: |
| 260 | os.unlink(pidfile) |
| inikep | c1b154a | 2016-06-10 12:53:12 +0200 | [diff] [blame] | 261 | log("sleep for %s seconds" % args.sleepTime) |
| inikep | 9470b87 | 2016-06-09 12:54:06 +0200 | [diff] [blame] | 262 | time.sleep(args.sleepTime) |