diff --git a/tools/heaplimit_measure.py b/tools/heaplimit_measure.py index 7aae79348..8d372e5b2 100755 --- a/tools/heaplimit_measure.py +++ b/tools/heaplimit_measure.py @@ -13,6 +13,9 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. +from __future__ import print_function +# force // operator to be integer division in Python 2 +from __future__ import division import argparse import json @@ -20,75 +23,66 @@ import os import subprocess import sys -from subprocess import Popen, PIPE, STDOUT +TOOLS_PATH = os.path.dirname(os.path.realpath(__file__)) +BASE_PATH = os.path.join(TOOLS_PATH, '..') - -BIN_JERRY = os.path.join(os.getcwd(), 'build/bin/jerry') FLAG_CLEAN = '--clean' FLAG_DEBUG = '--debug' FLAG_HEAPLIMIT = '--mem-heap' -JERRY_BUILDER = os.path.join(os.getcwd(), 'tools/build.py') -TEST_DIR = os.path.join(os.getcwd(), 'tests') +JERRY_BUILDER = os.path.join(BASE_PATH, 'tools', 'build.py') +JERRY_BIN = os.path.join(BASE_PATH, 'build', 'bin', 'jerry') +TEST_DIR = os.path.join(BASE_PATH, 'tests') + def get_args(): """ Parse input arguments. """ - parser = argparse.ArgumentParser(description='Finds the smallest possible \ - size of JerryHeap without failing to run the given js file') + desc = 'Finds the smallest possible JerryHeap size without failing to run the given js file' + parser = argparse.ArgumentParser(description=desc) parser.add_argument('testfile') parser.add_argument('--heapsize', type=int, default=512, - help='Set the limit of the first heapsize') - - parser.add_argument('--buildtype', type=str, - help='debug build, default release build') + help='set the limit of the first heapsize (default: %(default)d)') + parser.add_argument('--buildtype', choices=['release', 'debug'], default='release', + help='select build type (default: %(default)s)') script_args = parser.parse_args() return script_args -def is_exe(fpath): - """ Check whether the file is exist. """ - return os.path.isfile(fpath) - - -def init(opts): - if not is_exe(JERRY_BUILDER): - sys.exit("Builder script isn't a file!") - if not is_exe(opts.testfile): - sys.exit("Testfile isn't a file!") +def check_files(opts): + files = [JERRY_BUILDER, opts.testfile] + for file in files: + if not os.path.isfile(file): + sys.exit("File not found: %s" % file) def build_bin(heapsize, opts): """ Run tools/build.py script """ - hsize = str(heapsize) + command = [ + JERRY_BUILDER, + FLAG_CLEAN, + FLAG_HEAPLIMIT, + str(heapsize) + ] + if opts.buildtype == 'debug': - subprocess.call([JERRY_BUILDER, FLAG_CLEAN, FLAG_HEAPLIMIT, \ - hsize, FLAG_DEBUG]) - else: - subprocess.call([JERRY_BUILDER, FLAG_CLEAN, FLAG_HEAPLIMIT, hsize]) + command.append(FLAG_DEBUG) - -def get_output(cwd, cmd): - """ Run the given command and return with the output. """ - process = Popen(cmd, stdout=PIPE, stderr=STDOUT, cwd=cwd) - - output = process.communicate()[0] - exitcode = process.returncode - - return output, exitcode + print('Building JerryScript with: %s' % (' '.join(command))) + subprocess.check_output(command) def run_test(opts): - """ Run the testfile for output and exitcode. """ + """ Run the testfile to get the exitcode. """ try: testfile = os.path.abspath(opts.testfile) - run_cmd = [BIN_JERRY, testfile] - output, ret = get_output(TEST_DIR, run_cmd) + run_cmd = [JERRY_BIN, testfile] + # check output will raise an error if the exit code is not 0 + subprocess.check_output(run_cmd, cwd=TEST_DIR) except subprocess.CalledProcessError as err: - output = err.output - ret = err.returncode + return err.returncode - return opts.testfile, output, ret + return 0 def heap_limit(opts): @@ -99,21 +93,24 @@ def heap_limit(opts): while lowheap < hiheap: build_bin(hiheap, opts) - assert is_exe(BIN_JERRY), 'Binary file not exist' + assert os.path.isfile(JERRY_BIN), 'Jerry binary file does not exists' - testfile, output, exitcode = run_test(opts) + exitcode = run_test(opts) if exitcode != 0: lowheap = hiheap - hiheap = (lowheap + goodheap) / 2 + hiheap = (lowheap + goodheap) // 2 else: goodheap = hiheap - hiheap = (lowheap + hiheap) / 2 + hiheap = (lowheap + hiheap) // 2 - return { 'testfile': testfile, 'heaplimit to pass': goodheap } + return { + 'testfile': opts.testfile, + 'heaplimit to pass': goodheap + } def main(options): - init(options) + check_files(options) result = heap_limit(options) print(json.dumps(result, indent=4))