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