1 1.1 christos #! /usr/bin/env python3 2 1.1 christos # THIS BENCHMARK IS BEING REPLACED BY automated-bencmarking.py 3 1.1 christos 4 1.1 christos # ################################################################ 5 1.1 christos # Copyright (c) Meta Platforms, Inc. and affiliates. 6 1.1 christos # All rights reserved. 7 1.1 christos # 8 1.1 christos # This source code is licensed under both the BSD-style license (found in the 9 1.1 christos # LICENSE file in the root directory of this source tree) and the GPLv2 (found 10 1.1 christos # in the COPYING file in the root directory of this source tree). 11 1.1 christos # You may select, at your option, one of the above-listed licenses. 12 1.1 christos # ########################################################################## 13 1.1 christos 14 1.1 christos # Limitations: 15 1.1 christos # - doesn't support filenames with spaces 16 1.1 christos # - dir1/zstd and dir2/zstd will be merged in a single results file 17 1.1 christos 18 1.1 christos import argparse 19 1.1 christos import os # getloadavg 20 1.1 christos import string 21 1.1 christos import subprocess 22 1.1 christos import time # strftime 23 1.1 christos import traceback 24 1.1 christos import hashlib 25 1.1 christos import platform # system 26 1.1 christos 27 1.1 christos script_version = 'v1.1.2 (2017-03-26)' 28 1.1 christos default_repo_url = 'https://github.com/facebook/zstd.git' 29 1.1 christos working_dir_name = 'speedTest' 30 1.1 christos working_path = os.getcwd() + '/' + working_dir_name # /path/to/zstd/tests/speedTest 31 1.1 christos clone_path = working_path + '/' + 'zstd' # /path/to/zstd/tests/speedTest/zstd 32 1.1 christos email_header = 'ZSTD_speedTest' 33 1.1 christos pid = str(os.getpid()) 34 1.1 christos verbose = False 35 1.1 christos clang_version = "unknown" 36 1.1 christos gcc_version = "unknown" 37 1.1 christos args = None 38 1.1 christos 39 1.1 christos 40 1.1 christos def hashfile(hasher, fname, blocksize=65536): 41 1.1 christos with open(fname, "rb") as f: 42 1.1 christos for chunk in iter(lambda: f.read(blocksize), b""): 43 1.1 christos hasher.update(chunk) 44 1.1 christos return hasher.hexdigest() 45 1.1 christos 46 1.1 christos 47 1.1 christos def log(text): 48 1.1 christos print(time.strftime("%Y/%m/%d %H:%M:%S") + ' - ' + text) 49 1.1 christos 50 1.1 christos 51 1.1 christos def execute(command, print_command=True, print_output=False, print_error=True, param_shell=True): 52 1.1 christos if print_command: 53 1.1 christos log("> " + command) 54 1.1 christos popen = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=param_shell, cwd=execute.cwd) 55 1.1 christos stdout_lines, stderr_lines = popen.communicate(timeout=args.timeout) 56 1.1 christos stderr_lines = stderr_lines.decode("utf-8") 57 1.1 christos stdout_lines = stdout_lines.decode("utf-8") 58 1.1 christos if print_output: 59 1.1 christos if stdout_lines: 60 1.1 christos print(stdout_lines) 61 1.1 christos if stderr_lines: 62 1.1 christos print(stderr_lines) 63 1.1 christos if popen.returncode is not None and popen.returncode != 0: 64 1.1 christos if stderr_lines and not print_output and print_error: 65 1.1 christos print(stderr_lines) 66 1.1 christos raise RuntimeError(stdout_lines + stderr_lines) 67 1.1 christos return (stdout_lines + stderr_lines).splitlines() 68 1.1 christos execute.cwd = None 69 1.1 christos 70 1.1 christos 71 1.1 christos def does_command_exist(command): 72 1.1 christos try: 73 1.1 christos execute(command, verbose, False, False) 74 1.1 christos except Exception: 75 1.1 christos return False 76 1.1 christos return True 77 1.1 christos 78 1.1 christos 79 1.1 christos def send_email(emails, topic, text, have_mutt, have_mail): 80 1.1 christos logFileName = working_path + '/' + 'tmpEmailContent' 81 1.1 christos with open(logFileName, "w") as myfile: 82 1.1 christos myfile.writelines(text) 83 1.1 christos myfile.close() 84 1.1 christos if have_mutt: 85 1.1 christos execute('mutt -s "' + topic + '" ' + emails + ' < ' + logFileName, verbose) 86 1.1 christos elif have_mail: 87 1.1 christos execute('mail -s "' + topic + '" ' + emails + ' < ' + logFileName, verbose) 88 1.1 christos else: 89 1.1 christos log("e-mail cannot be sent (mail or mutt not found)") 90 1.1 christos 91 1.1 christos 92 1.1 christos def send_email_with_attachments(branch, commit, last_commit, args, text, results_files, 93 1.1 christos logFileName, have_mutt, have_mail): 94 1.1 christos with open(logFileName, "w") as myfile: 95 1.1 christos myfile.writelines(text) 96 1.1 christos myfile.close() 97 1.1 christos email_topic = '[%s:%s] Warning for %s:%s last_commit=%s speed<%s ratio<%s' \ 98 1.1 christos % (email_header, pid, branch, commit, last_commit, 99 1.1 christos args.lowerLimit, args.ratioLimit) 100 1.1 christos if have_mutt: 101 1.1 christos execute('mutt -s "' + email_topic + '" ' + args.emails + ' -a ' + results_files 102 1.1 christos + ' < ' + logFileName) 103 1.1 christos elif have_mail: 104 1.1 christos execute('mail -s "' + email_topic + '" ' + args.emails + ' < ' + logFileName) 105 1.1 christos else: 106 1.1 christos log("e-mail cannot be sent (mail or mutt not found)") 107 1.1 christos 108 1.1 christos 109 1.1 christos def git_get_branches(): 110 1.1 christos execute('git fetch -p', verbose) 111 1.1 christos branches = execute('git branch -rl', verbose) 112 1.1 christos output = [] 113 1.1 christos for line in branches: 114 1.1 christos if ("HEAD" not in line) and ("coverity_scan" not in line) and ("gh-pages" not in line): 115 1.1 christos output.append(line.strip()) 116 1.1 christos return output 117 1.1 christos 118 1.1 christos 119 1.1 christos def git_get_changes(branch, commit, last_commit): 120 1.1 christos fmt = '--format="%h: (%an) %s, %ar"' 121 1.1 christos if last_commit is None: 122 1.1 christos commits = execute('git log -n 10 %s %s' % (fmt, commit)) 123 1.1 christos else: 124 1.1 christos commits = execute('git --no-pager log %s %s..%s' % (fmt, last_commit, commit)) 125 1.1 christos return str('Changes in %s since %s:\n' % (branch, last_commit)) + '\n'.join(commits) 126 1.1 christos 127 1.1 christos 128 1.1 christos def get_last_results(resultsFileName): 129 1.1 christos if not os.path.isfile(resultsFileName): 130 1.1 christos return None, None, None, None 131 1.1 christos commit = None 132 1.1 christos csize = [] 133 1.1 christos cspeed = [] 134 1.1 christos dspeed = [] 135 1.1 christos with open(resultsFileName, 'r') as f: 136 1.1 christos for line in f: 137 1.1 christos words = line.split() 138 1.1 christos if len(words) <= 4: # branch + commit + compilerVer + md5 139 1.1 christos commit = words[1] 140 1.1 christos csize = [] 141 1.1 christos cspeed = [] 142 1.1 christos dspeed = [] 143 1.1 christos if (len(words) == 8) or (len(words) == 9): # results: "filename" or "XX files" 144 1.1 christos csize.append(int(words[1])) 145 1.1 christos cspeed.append(float(words[3])) 146 1.1 christos dspeed.append(float(words[5])) 147 1.1 christos return commit, csize, cspeed, dspeed 148 1.1 christos 149 1.1 christos 150 1.1 christos def benchmark_and_compare(branch, commit, last_commit, args, executableName, md5sum, compilerVersion, resultsFileName, 151 1.1 christos testFilePath, fileName, last_csize, last_cspeed, last_dspeed): 152 1.1 christos sleepTime = 30 153 1.1 christos while os.getloadavg()[0] > args.maxLoadAvg: 154 1.1 christos log("WARNING: bench loadavg=%.2f is higher than %s, sleeping for %s seconds" 155 1.1 christos % (os.getloadavg()[0], args.maxLoadAvg, sleepTime)) 156 1.1 christos time.sleep(sleepTime) 157 1.1 christos start_load = str(os.getloadavg()) 158 1.1 christos osType = platform.system() 159 1.1 christos if osType == 'Linux': 160 1.1 christos cpuSelector = "taskset --cpu-list 0" 161 1.1 christos else: 162 1.1 christos cpuSelector = "" 163 1.1 christos if args.dictionary: 164 1.1 christos result = execute('%s programs/%s -rqi5b1e%s -D %s %s' % (cpuSelector, executableName, args.lastCLevel, args.dictionary, testFilePath), print_output=True) 165 1.1 christos else: 166 1.1 christos result = execute('%s programs/%s -rqi5b1e%s %s' % (cpuSelector, executableName, args.lastCLevel, testFilePath), print_output=True) 167 1.1 christos end_load = str(os.getloadavg()) 168 1.1 christos linesExpected = args.lastCLevel + 1 169 1.1 christos if len(result) != linesExpected: 170 1.1 christos raise RuntimeError("ERROR: number of result lines=%d is different that expected %d\n%s" % (len(result), linesExpected, '\n'.join(result))) 171 1.1 christos with open(resultsFileName, "a") as myfile: 172 1.1 christos myfile.write('%s %s %s md5=%s\n' % (branch, commit, compilerVersion, md5sum)) 173 1.1 christos myfile.write('\n'.join(result) + '\n') 174 1.1 christos myfile.close() 175 1.1 christos if (last_cspeed == None): 176 1.1 christos log("WARNING: No data for comparison for branch=%s file=%s " % (branch, fileName)) 177 1.1 christos return "" 178 1.1 christos commit, csize, cspeed, dspeed = get_last_results(resultsFileName) 179 1.1 christos text = "" 180 1.1 christos for i in range(0, min(len(cspeed), len(last_cspeed))): 181 1.1 christos print("%s:%s -%d cSpeed=%6.2f cLast=%6.2f cDiff=%1.4f dSpeed=%6.2f dLast=%6.2f dDiff=%1.4f ratioDiff=%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], float(last_csize[i])/csize[i], fileName)) 182 1.1 christos if (cspeed[i]/last_cspeed[i] < args.lowerLimit): 183 1.1 christos text += "WARNING: %s -%d cSpeed=%.2f cLast=%.2f cDiff=%.4f %s\n" % (executableName, i+1, cspeed[i], last_cspeed[i], cspeed[i]/last_cspeed[i], fileName) 184 1.1 christos if (dspeed[i]/last_dspeed[i] < args.lowerLimit): 185 1.1 christos text += "WARNING: %s -%d dSpeed=%.2f dLast=%.2f dDiff=%.4f %s\n" % (executableName, i+1, dspeed[i], last_dspeed[i], dspeed[i]/last_dspeed[i], fileName) 186 1.1 christos if (float(last_csize[i])/csize[i] < args.ratioLimit): 187 1.1 christos text += "WARNING: %s -%d cSize=%d last_cSize=%d diff=%.4f %s\n" % (executableName, i+1, csize[i], last_csize[i], float(last_csize[i])/csize[i], fileName) 188 1.1 christos if text: 189 1.1 christos text = args.message + ("\nmaxLoadAvg=%s load average at start=%s end=%s\n%s last_commit=%s md5=%s\n" % (args.maxLoadAvg, start_load, end_load, compilerVersion, last_commit, md5sum)) + text 190 1.1 christos return text 191 1.1 christos 192 1.1 christos 193 1.1 christos def update_config_file(branch, commit): 194 1.1 christos last_commit = None 195 1.1 christos commitFileName = working_path + "/commit_" + branch.replace("/", "_") + ".txt" 196 1.1 christos if os.path.isfile(commitFileName): 197 1.1 christos with open(commitFileName, 'r') as infile: 198 1.1 christos last_commit = infile.read() 199 1.1 christos with open(commitFileName, 'w') as outfile: 200 1.1 christos outfile.write(commit) 201 1.1 christos return last_commit 202 1.1 christos 203 1.1 christos 204 1.1 christos def double_check(branch, commit, args, executableName, md5sum, compilerVersion, resultsFileName, filePath, fileName): 205 1.1 christos last_commit, csize, cspeed, dspeed = get_last_results(resultsFileName) 206 1.1 christos if not args.dry_run: 207 1.1 christos text = benchmark_and_compare(branch, commit, last_commit, args, executableName, md5sum, compilerVersion, resultsFileName, filePath, fileName, csize, cspeed, dspeed) 208 1.1 christos if text: 209 1.1 christos log("WARNING: redoing tests for branch %s: commit %s" % (branch, commit)) 210 1.1 christos text = benchmark_and_compare(branch, commit, last_commit, args, executableName, md5sum, compilerVersion, resultsFileName, filePath, fileName, csize, cspeed, dspeed) 211 1.1 christos return text 212 1.1 christos 213 1.1 christos 214 1.1 christos def test_commit(branch, commit, last_commit, args, testFilePaths, have_mutt, have_mail): 215 1.1 christos local_branch = branch.split('/')[1] 216 1.1 christos version = local_branch.rpartition('-')[2] + '_' + commit 217 1.1 christos if not args.dry_run: 218 1.1 christos execute('make -C programs clean zstd CC=clang MOREFLAGS="-Werror -Wconversion -Wno-sign-conversion -DZSTD_GIT_COMMIT=%s" && ' % version + 219 1.1 christos 'mv programs/zstd programs/zstd_clang && ' + 220 1.1 christos 'make -C programs clean zstd zstd32 MOREFLAGS="-DZSTD_GIT_COMMIT=%s"' % version) 221 1.1 christos md5_zstd = hashfile(hashlib.md5(), clone_path + '/programs/zstd') 222 1.1 christos md5_zstd32 = hashfile(hashlib.md5(), clone_path + '/programs/zstd32') 223 1.1 christos md5_zstd_clang = hashfile(hashlib.md5(), clone_path + '/programs/zstd_clang') 224 1.1 christos print("md5(zstd)=%s\nmd5(zstd32)=%s\nmd5(zstd_clang)=%s" % (md5_zstd, md5_zstd32, md5_zstd_clang)) 225 1.1 christos print("gcc_version=%s clang_version=%s" % (gcc_version, clang_version)) 226 1.1 christos 227 1.1 christos logFileName = working_path + "/log_" + branch.replace("/", "_") + ".txt" 228 1.1 christos text_to_send = [] 229 1.1 christos results_files = "" 230 1.1 christos if args.dictionary: 231 1.1 christos dictName = args.dictionary.rpartition('/')[2] 232 1.1 christos else: 233 1.1 christos dictName = None 234 1.1 christos 235 1.1 christos for filePath in testFilePaths: 236 1.1 christos fileName = filePath.rpartition('/')[2] 237 1.1 christos if dictName: 238 1.1 christos resultsFileName = working_path + "/" + dictName.replace(".", "_") + "_" + branch.replace("/", "_") + "_" + fileName.replace(".", "_") + ".txt" 239 1.1 christos else: 240 1.1 christos resultsFileName = working_path + "/results_" + branch.replace("/", "_") + "_" + fileName.replace(".", "_") + ".txt" 241 1.1 christos text = double_check(branch, commit, args, 'zstd', md5_zstd, 'gcc_version='+gcc_version, resultsFileName, filePath, fileName) 242 1.1 christos if text: 243 1.1 christos text_to_send.append(text) 244 1.1 christos results_files += resultsFileName + " " 245 1.1 christos resultsFileName = working_path + "/results32_" + branch.replace("/", "_") + "_" + fileName.replace(".", "_") + ".txt" 246 1.1 christos text = double_check(branch, commit, args, 'zstd32', md5_zstd32, 'gcc_version='+gcc_version, resultsFileName, filePath, fileName) 247 1.1 christos if text: 248 1.1 christos text_to_send.append(text) 249 1.1 christos results_files += resultsFileName + " " 250 1.1 christos resultsFileName = working_path + "/resultsClang_" + branch.replace("/", "_") + "_" + fileName.replace(".", "_") + ".txt" 251 1.1 christos text = double_check(branch, commit, args, 'zstd_clang', md5_zstd_clang, 'clang_version='+clang_version, resultsFileName, filePath, fileName) 252 1.1 christos if text: 253 1.1 christos text_to_send.append(text) 254 1.1 christos results_files += resultsFileName + " " 255 1.1 christos if text_to_send: 256 1.1 christos send_email_with_attachments(branch, commit, last_commit, args, text_to_send, results_files, logFileName, have_mutt, have_mail) 257 1.1 christos 258 1.1 christos 259 1.1 christos if __name__ == '__main__': 260 1.1 christos parser = argparse.ArgumentParser() 261 1.1 christos parser.add_argument('testFileNames', help='file or directory names list for speed benchmark') 262 1.1 christos parser.add_argument('emails', help='list of e-mail addresses to send warnings') 263 1.1 christos parser.add_argument('--dictionary', '-D', help='path to the dictionary') 264 1.1 christos parser.add_argument('--message', '-m', help='attach an additional message to e-mail', default="") 265 1.1 christos parser.add_argument('--repoURL', help='changes default repository URL', default=default_repo_url) 266 1.1 christos parser.add_argument('--lowerLimit', '-l', type=float, help='send email if speed is lower than given limit', default=0.98) 267 1.1 christos parser.add_argument('--ratioLimit', '-r', type=float, help='send email if ratio is lower than given limit', default=0.999) 268 1.1 christos parser.add_argument('--maxLoadAvg', type=float, help='maximum load average to start testing', default=0.75) 269 1.1 christos parser.add_argument('--lastCLevel', type=int, help='last compression level for testing', default=5) 270 1.1 christos parser.add_argument('--sleepTime', '-s', type=int, help='frequency of repository checking in seconds', default=300) 271 1.1 christos parser.add_argument('--timeout', '-t', type=int, help='timeout for executing shell commands', default=1800) 272 1.1 christos parser.add_argument('--dry-run', dest='dry_run', action='store_true', help='not build', default=False) 273 1.1 christos parser.add_argument('--verbose', '-v', action='store_true', help='more verbose logs', default=False) 274 1.1 christos args = parser.parse_args() 275 1.1 christos verbose = args.verbose 276 1.1 christos 277 1.1 christos # check if test files are accessible 278 1.1 christos testFileNames = args.testFileNames.split() 279 1.1 christos testFilePaths = [] 280 1.1 christos for fileName in testFileNames: 281 1.1 christos fileName = os.path.expanduser(fileName) 282 1.1 christos if os.path.isfile(fileName) or os.path.isdir(fileName): 283 1.1 christos testFilePaths.append(os.path.abspath(fileName)) 284 1.1 christos else: 285 1.1 christos log("ERROR: File/directory not found: " + fileName) 286 1.1 christos exit(1) 287 1.1 christos 288 1.1 christos # check if dictionary is accessible 289 1.1 christos if args.dictionary: 290 1.1 christos args.dictionary = os.path.abspath(os.path.expanduser(args.dictionary)) 291 1.1 christos if not os.path.isfile(args.dictionary): 292 1.1 christos log("ERROR: Dictionary not found: " + args.dictionary) 293 1.1 christos exit(1) 294 1.1 christos 295 1.1 christos # check availability of e-mail senders 296 1.1 christos have_mutt = does_command_exist("mutt -h") 297 1.1 christos have_mail = does_command_exist("mail -V") 298 1.1 christos if not have_mutt and not have_mail: 299 1.1 christos log("ERROR: e-mail senders 'mail' or 'mutt' not found") 300 1.1 christos exit(1) 301 1.1 christos 302 1.1 christos clang_version = execute("clang -v 2>&1 | grep ' version ' | sed -e 's:.*version \\([0-9.]*\\).*:\\1:' -e 's:\\.\\([0-9][0-9]\\):\\1:g'", verbose)[0]; 303 1.1 christos gcc_version = execute("gcc -dumpversion", verbose)[0]; 304 1.1 christos 305 1.1 christos if verbose: 306 1.1 christos print("PARAMETERS:\nrepoURL=%s" % args.repoURL) 307 1.1 christos print("working_path=%s" % working_path) 308 1.1 christos print("clone_path=%s" % clone_path) 309 1.1 christos print("testFilePath(%s)=%s" % (len(testFilePaths), testFilePaths)) 310 1.1 christos print("message=%s" % args.message) 311 1.1 christos print("emails=%s" % args.emails) 312 1.1 christos print("dictionary=%s" % args.dictionary) 313 1.1 christos print("maxLoadAvg=%s" % args.maxLoadAvg) 314 1.1 christos print("lowerLimit=%s" % args.lowerLimit) 315 1.1 christos print("ratioLimit=%s" % args.ratioLimit) 316 1.1 christos print("lastCLevel=%s" % args.lastCLevel) 317 1.1 christos print("sleepTime=%s" % args.sleepTime) 318 1.1 christos print("timeout=%s" % args.timeout) 319 1.1 christos print("dry_run=%s" % args.dry_run) 320 1.1 christos print("verbose=%s" % args.verbose) 321 1.1 christos print("have_mutt=%s have_mail=%s" % (have_mutt, have_mail)) 322 1.1 christos 323 1.1 christos # clone ZSTD repo if needed 324 1.1 christos if not os.path.isdir(working_path): 325 1.1 christos os.mkdir(working_path) 326 1.1 christos if not os.path.isdir(clone_path): 327 1.1 christos execute.cwd = working_path 328 1.1 christos execute('git clone ' + args.repoURL) 329 1.1 christos if not os.path.isdir(clone_path): 330 1.1 christos log("ERROR: ZSTD clone not found: " + clone_path) 331 1.1 christos exit(1) 332 1.1 christos execute.cwd = clone_path 333 1.1 christos 334 1.1 christos # check if speedTest.pid already exists 335 1.1 christos pidfile = "./speedTest.pid" 336 1.1 christos if os.path.isfile(pidfile): 337 1.1 christos log("ERROR: %s already exists, exiting" % pidfile) 338 1.1 christos exit(1) 339 1.1 christos 340 1.1 christos send_email(args.emails, '[%s:%s] test-zstd-speed.py %s has been started' % (email_header, pid, script_version), args.message, have_mutt, have_mail) 341 1.1 christos with open(pidfile, 'w') as the_file: 342 1.1 christos the_file.write(pid) 343 1.1 christos 344 1.1 christos branch = "" 345 1.1 christos commit = "" 346 1.1 christos first_time = True 347 1.1 christos while True: 348 1.1 christos try: 349 1.1 christos if first_time: 350 1.1 christos first_time = False 351 1.1 christos else: 352 1.1 christos time.sleep(args.sleepTime) 353 1.1 christos loadavg = os.getloadavg()[0] 354 1.1 christos if (loadavg <= args.maxLoadAvg): 355 1.1 christos branches = git_get_branches() 356 1.1 christos for branch in branches: 357 1.1 christos commit = execute('git show -s --format=%h ' + branch, verbose)[0] 358 1.1 christos last_commit = update_config_file(branch, commit) 359 1.1 christos if commit == last_commit: 360 1.1 christos log("skipping branch %s: head %s already processed" % (branch, commit)) 361 1.1 christos else: 362 1.1 christos log("build branch %s: head %s is different from prev %s" % (branch, commit, last_commit)) 363 1.1 christos execute('git checkout -- . && git checkout ' + branch) 364 1.1 christos print(git_get_changes(branch, commit, last_commit)) 365 1.1 christos test_commit(branch, commit, last_commit, args, testFilePaths, have_mutt, have_mail) 366 1.1 christos else: 367 1.1 christos log("WARNING: main loadavg=%.2f is higher than %s" % (loadavg, args.maxLoadAvg)) 368 1.1 christos if verbose: 369 1.1 christos log("sleep for %s seconds" % args.sleepTime) 370 1.1 christos except Exception as e: 371 1.1 christos stack = traceback.format_exc() 372 1.1 christos email_topic = '[%s:%s] ERROR in %s:%s' % (email_header, pid, branch, commit) 373 1.1 christos send_email(args.emails, email_topic, stack, have_mutt, have_mail) 374 1.1 christos print(stack) 375 1.1 christos except KeyboardInterrupt: 376 1.1 christos os.unlink(pidfile) 377 1.1 christos send_email(args.emails, '[%s:%s] test-zstd-speed.py %s has been stopped' % (email_header, pid, script_version), args.message, have_mutt, have_mail) 378 1.1 christos exit(0) 379