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