| inikep | 9470b87 | 2016-06-09 12:54:06 +0200 | [diff] [blame] | 1 | #! /usr/bin/env python |
| inikep | 9470b87 | 2016-06-09 12:54:06 +0200 | [diff] [blame] | 2 | |
| 3 | import argparse |
| 4 | import os |
| 5 | import string |
| 6 | import time |
| 7 | import traceback |
| inikep | 95da743 | 2016-06-22 12:12:35 +0200 | [diff] [blame] | 8 | import subprocess |
| inikep | 9470b87 | 2016-06-09 12:54:06 +0200 | [diff] [blame] | 9 | |
| inikep | d731de8 | 2016-06-21 11:26:17 +0200 | [diff] [blame] | 10 | default_repo_url = 'https://github.com/Cyan4973/zstd.git' |
| inikep | 95da743 | 2016-06-22 12:12:35 +0200 | [diff] [blame] | 11 | working_dir_name = 'speedTest' |
| 12 | working_path = os.getcwd() + '/' + working_dir_name # /path/to/zstd/tests/speedTest |
| 13 | clone_path = working_path + '/' + 'zstd' # /path/to/zstd/tests/speedTest/zstd |
| inikep | d731de8 | 2016-06-21 11:26:17 +0200 | [diff] [blame] | 14 | email_header = '[ZSTD_speedTest]' |
| inikep | 95da743 | 2016-06-22 12:12:35 +0200 | [diff] [blame] | 15 | pid = str(os.getpid()) |
| 16 | |
| inikep | 9470b87 | 2016-06-09 12:54:06 +0200 | [diff] [blame] | 17 | |
| 18 | def log(text): |
| inikep | 2d9272f | 2016-06-21 19:28:51 +0200 | [diff] [blame] | 19 | print(time.strftime("%Y/%m/%d %H:%M:%S") + ' - ' + text) |
| inikep | 9470b87 | 2016-06-09 12:54:06 +0200 | [diff] [blame] | 20 | |
| inikep | 2d9272f | 2016-06-21 19:28:51 +0200 | [diff] [blame] | 21 | |
| 22 | def execute(command, print_output=False, print_error=True, param_shell=True): |
| inikep | 9470b87 | 2016-06-09 12:54:06 +0200 | [diff] [blame] | 23 | log("> " + command) |
| inikep | 95da743 | 2016-06-22 12:12:35 +0200 | [diff] [blame] | 24 | 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() |
| inikep | 2d9272f | 2016-06-21 19:28:51 +0200 | [diff] [blame] | 27 | if print_output: |
| inikep | 95da743 | 2016-06-22 12:12:35 +0200 | [diff] [blame] | 28 | print('\n'.join(stdout_lines)) |
| inikep | 9470b87 | 2016-06-09 12:54:06 +0200 | [diff] [blame] | 29 | if popen.returncode is not None and popen.returncode != 0: |
| inikep | c1b154a | 2016-06-10 12:53:12 +0200 | [diff] [blame] | 30 | if not print_output and print_error: |
| inikep | 95da743 | 2016-06-22 12:12:35 +0200 | [diff] [blame] | 31 | print('\n'.join(stdout_lines)) |
| 32 | raise RuntimeError('\n'.join(stdout_lines)) |
| 33 | return stdout_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 | 95da743 | 2016-06-22 12:12:35 +0200 | [diff] [blame] | 38 | try: |
| 39 | execute(command, False, False); |
| 40 | except Exception as e: |
| 41 | return False |
| 42 | return True |
| inikep | c1b154a | 2016-06-10 12:53:12 +0200 | [diff] [blame] | 43 | |
| 44 | |
| inikep | 95da743 | 2016-06-22 12:12:35 +0200 | [diff] [blame] | 45 | def send_email(emails, topic, text, have_mutt, have_mail): |
| 46 | logFileName = working_path + '/' + 'tmpEmailContent' |
| inikep | 9470b87 | 2016-06-09 12:54:06 +0200 | [diff] [blame] | 47 | with open(logFileName, "w") as myfile: |
| 48 | myfile.writelines(text) |
| 49 | myfile.close() |
| inikep | c1b154a | 2016-06-10 12:53:12 +0200 | [diff] [blame] | 50 | if have_mutt: |
| inikep | 95da743 | 2016-06-22 12:12:35 +0200 | [diff] [blame] | 51 | execute('mutt -s "' + topic + '" ' + emails + ' < ' + logFileName) |
| inikep | c1b154a | 2016-06-10 12:53:12 +0200 | [diff] [blame] | 52 | elif have_mail: |
| inikep | 95da743 | 2016-06-22 12:12:35 +0200 | [diff] [blame] | 53 | execute('mail -s "' + topic + '" ' + emails + ' < ' + logFileName) |
| inikep | c1b154a | 2016-06-10 12:53:12 +0200 | [diff] [blame] | 54 | else: |
| inikep | 95da743 | 2016-06-22 12:12:35 +0200 | [diff] [blame] | 55 | log("e-mail cannot be sent (mail or mutt not found)") |
| inikep | 9470b87 | 2016-06-09 12:54:06 +0200 | [diff] [blame] | 56 | |
| 57 | |
| inikep | 95da743 | 2016-06-22 12:12:35 +0200 | [diff] [blame] | 58 | def 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() |
| inikep | bcb9aad | 2016-06-22 13:07:58 +0200 | [diff] [blame^] | 62 | email_topic = '%s:%s Warning for %s:%s last_commit=%s speed<%s' % (email_header, pid, branch, commit, last_commit, lower_limit) |
| inikep | 95da743 | 2016-06-22 12:12:35 +0200 | [diff] [blame] | 63 | 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 | |
| inikep | bcb9aad | 2016-06-22 13:07:58 +0200 | [diff] [blame^] | 71 | def 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 | |
| 80 | def 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 | |
| 89 | def 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 | |
| 99 | def 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 | |
| 118 | def 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 | |
| inikep | 95da743 | 2016-06-22 12:12:35 +0200 | [diff] [blame] | 149 | def check_branch(branch, args, testFilePaths, have_mutt, have_mail): |
| 150 | commits = execute('git show -s --format=%h ' + branch)[0] |
| 151 | for commit in [commits]: |
| inikep | 9470b87 | 2016-06-09 12:54:06 +0200 | [diff] [blame] | 152 | try: |
| inikep | 95da743 | 2016-06-22 12:12:35 +0200 | [diff] [blame] | 153 | commitFileName = working_path + "/commit_" + branch.replace("/", "_") |
| inikep | 9470b87 | 2016-06-09 12:54:06 +0200 | [diff] [blame] | 154 | 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)) |
| inikep | bcb9aad | 2016-06-22 13:07:58 +0200 | [diff] [blame^] | 164 | compile(branch, commit, last_commit, args.dry_run) |
| inikep | 9470b87 | 2016-06-09 12:54:06 +0200 | [diff] [blame] | 165 | |
| inikep | 95da743 | 2016-06-22 12:12:35 +0200 | [diff] [blame] | 166 | logFileName = working_path + "/log_" + branch.replace("/", "_") |
| inikep | 9470b87 | 2016-06-09 12:54:06 +0200 | [diff] [blame] | 167 | text_to_send = [] |
| 168 | results_files = "" |
| 169 | for filePath in testFilePaths: |
| 170 | fileName = filePath.rpartition('/')[2] |
| inikep | 95da743 | 2016-06-22 12:12:35 +0200 | [diff] [blame] | 171 | resultsFileName = working_path + "/results_" + branch.replace("/", "_") + "_" + fileName |
| inikep | 9470b87 | 2016-06-09 12:54:06 +0200 | [diff] [blame] | 172 | last_commit, cspeed, dspeed = get_last_commit(resultsFileName) |
| 173 | |
| 174 | if not args.dry_run: |
| inikep | c1b154a | 2016-06-10 12:53:12 +0200 | [diff] [blame] | 175 | 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] | 176 | if text: |
| inikep | 95da743 | 2016-06-22 12:12:35 +0200 | [diff] [blame] | 177 | log("WARNING: redoing tests for branch %s: commit %s" % (branch, commit)) |
| inikep | c1b154a | 2016-06-10 12:53:12 +0200 | [diff] [blame] | 178 | 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] | 179 | if text: |
| 180 | text_to_send.append(text) |
| 181 | results_files += resultsFileName + " " |
| 182 | if text_to_send: |
| inikep | 95da743 | 2016-06-22 12:12:35 +0200 | [diff] [blame] | 183 | send_email_with_attachments(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] | 184 | except Exception as e: |
| 185 | stack = traceback.format_exc() |
| inikep | bcb9aad | 2016-06-22 13:07:58 +0200 | [diff] [blame^] | 186 | email_topic = '%s:%s ERROR in %s:%s' % (email_header, pid, branch, commit) |
| inikep | 95da743 | 2016-06-22 12:12:35 +0200 | [diff] [blame] | 187 | send_email(args.emails, email_topic, stack, have_mutt, have_mail) |
| inikep | 2d9272f | 2016-06-21 19:28:51 +0200 | [diff] [blame] | 188 | print(stack) |
| inikep | 9470b87 | 2016-06-09 12:54:06 +0200 | [diff] [blame] | 189 | |
| 190 | |
| 191 | if __name__ == '__main__': |
| 192 | parser = argparse.ArgumentParser() |
| inikep | c1b154a | 2016-06-10 12:53:12 +0200 | [diff] [blame] | 193 | 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') |
| inikep | 1e375f1 | 2016-06-13 10:50:09 +0200 | [diff] [blame] | 195 | parser.add_argument('--message', help='attach an additional message to e-mail', default="") |
| inikep | d731de8 | 2016-06-21 11:26:17 +0200 | [diff] [blame] | 196 | parser.add_argument('--repoURL', help='changes default repository URL', default=default_repo_url) |
| inikep | c1b154a | 2016-06-10 12:53:12 +0200 | [diff] [blame] | 197 | 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] | 198 | 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) |
| inikep | c1b154a | 2016-06-10 12:53:12 +0200 | [diff] [blame] | 200 | 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] | 201 | 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: |
| inikep | d731de8 | 2016-06-21 11:26:17 +0200 | [diff] [blame] | 211 | log("ERROR: File not found: " + fileName) |
| 212 | exit(1) |
| inikep | 9470b87 | 2016-06-09 12:54:06 +0200 | [diff] [blame] | 213 | |
| inikep | c1b154a | 2016-06-10 12:53:12 +0200 | [diff] [blame] | 214 | # check availability of e-mail senders |
| inikep | 2d9272f | 2016-06-21 19:28:51 +0200 | [diff] [blame] | 215 | have_mutt = does_command_exist("mutt -h"); |
| inikep | c1b154a | 2016-06-10 12:53:12 +0200 | [diff] [blame] | 216 | have_mail = does_command_exist("mail -V"); |
| inikep | f169029 | 2016-06-10 13:59:08 +0200 | [diff] [blame] | 217 | if not have_mutt and not have_mail: |
| inikep | d731de8 | 2016-06-21 11:26:17 +0200 | [diff] [blame] | 218 | log("ERROR: e-mail senders 'mail' or 'mutt' not found") |
| 219 | exit(1) |
| inikep | c1b154a | 2016-06-10 12:53:12 +0200 | [diff] [blame] | 220 | |
| inikep | 2d9272f | 2016-06-21 19:28:51 +0200 | [diff] [blame] | 221 | print("PARAMETERS:\nrepoURL=%s" % args.repoURL) |
| inikep | 95da743 | 2016-06-22 12:12:35 +0200 | [diff] [blame] | 222 | print("working_path=%s" % working_path) |
| inikep | 2d9272f | 2016-06-21 19:28:51 +0200 | [diff] [blame] | 223 | 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)) |
| inikep | c1b154a | 2016-06-10 12:53:12 +0200 | [diff] [blame] | 233 | |
| inikep | d731de8 | 2016-06-21 11:26:17 +0200 | [diff] [blame] | 234 | # clone ZSTD repo if needed |
| inikep | 95da743 | 2016-06-22 12:12:35 +0200 | [diff] [blame] | 235 | if not os.path.isdir(working_path): |
| 236 | os.mkdir(working_path) |
| inikep | d731de8 | 2016-06-21 11:26:17 +0200 | [diff] [blame] | 237 | if not os.path.isdir(clone_path): |
| inikep | 95da743 | 2016-06-22 12:12:35 +0200 | [diff] [blame] | 238 | execute.cwd = working_path |
| inikep | d731de8 | 2016-06-21 11:26:17 +0200 | [diff] [blame] | 239 | 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 |
| inikep | d731de8 | 2016-06-21 11:26:17 +0200 | [diff] [blame] | 246 | pidfile = "./speedTest.pid" |
| 247 | if os.path.isfile(pidfile): |
| 248 | log("ERROR: %s already exists, exiting" % pidfile) |
| 249 | exit(1) |
| 250 | |
| inikep | 95da743 | 2016-06-22 12:12:35 +0200 | [diff] [blame] | 251 | 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) |
| inikep | 9470b87 | 2016-06-09 12:54:06 +0200 | [diff] [blame] | 254 | while True: |
| inikep | d731de8 | 2016-06-21 11:26:17 +0200 | [diff] [blame] | 255 | try: |
| 256 | loadavg = os.getloadavg()[0] |
| 257 | if (loadavg <= args.maxLoadAvg): |
| inikep | bcb9aad | 2016-06-22 13:07:58 +0200 | [diff] [blame^] | 258 | branches = git_get_branches() |
| inikep | 95da743 | 2016-06-22 12:12:35 +0200 | [diff] [blame] | 259 | for branch in branches: |
| 260 | check_branch(branch, args, testFilePaths, have_mutt, have_mail) |
| inikep | d731de8 | 2016-06-21 11:26:17 +0200 | [diff] [blame] | 261 | else: |
| 262 | log("WARNING: main loadavg=%.2f is higher than %s" % (loadavg, args.maxLoadAvg)) |
| inikep | 95da743 | 2016-06-22 12:12:35 +0200 | [diff] [blame] | 263 | log("sleep for %s seconds" % args.sleepTime) |
| 264 | time.sleep(args.sleepTime) |
| inikep | d731de8 | 2016-06-21 11:26:17 +0200 | [diff] [blame] | 265 | finally: |
| 266 | os.unlink(pidfile) |
| inikep | 95da743 | 2016-06-22 12:12:35 +0200 | [diff] [blame] | 267 | send_email(args.emails, email_header + ':%s test-zstd-speed.py has been stopped' % pid, '', have_mutt, have_mail) |