diff options
author | Tiancong Wang <tcwang@google.com> | 2019-08-07 15:18:51 -0700 |
---|---|---|
committer | Tiancong Wang <tcwang@google.com> | 2019-08-09 01:10:17 +0000 |
commit | eb729be4771ebbbc520231e28efef32a5db7f8af (patch) | |
tree | 3a846dd34584fb9af9b703e9ab6fae66a749f393 /crosperf | |
parent | e05c52149e50172f7011ad5c2194b6852f3e1e90 (diff) | |
download | toolchain-utils-eb729be4771ebbbc520231e28efef32a5db7f8af.tar.gz |
crosperf: Print traceback instead of exception type.
I found that the error message of crosperf is sometimes frustrating
that when you trigger an exception somewhere in the crosperf tool,
it will always return the exception type. Sometimes it's really hard
to find out where the exception/error happens in your code.
I think it might make sense to handle all exceptions with a
try...except block, to make the error message always appear at last,
so raising the original exception prints whole traceback, instead of
dropping it to general SystemExit.
BUG=None
TEST=Tested locally, and added unittest.
Change-Id: Id772af444bf3788011c74504c71939d2514f1752
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/third_party/toolchain-utils/+/1743146
Commit-Queue: Tiancong Wang <tcwang@google.com>
Tested-by: Tiancong Wang <tcwang@google.com>
Reviewed-by: George Burgess <gbiv@chromium.org>
Diffstat (limited to 'crosperf')
-rwxr-xr-x | crosperf/crosperf.py | 15 | ||||
-rwxr-xr-x | crosperf/crosperf_test.py | 44 | ||||
-rwxr-xr-x | crosperf/crosperf_unittest.py | 24 |
3 files changed, 30 insertions, 53 deletions
diff --git a/crosperf/crosperf.py b/crosperf/crosperf.py index af0a502f..fddd18b9 100755 --- a/crosperf/crosperf.py +++ b/crosperf/crosperf.py @@ -1,6 +1,9 @@ #!/usr/bin/env python2 +# -*- coding: utf-8 -*- +# Copyright 2011 The Chromium OS Authors. All rights reserved. +# Use of this source code is governed by a BSD-style license that can be +# found in the LICENSE file. -# Copyright 2011 Google Inc. All Rights Reserved. """The driver script for running performance benchmarks on ChromeOS.""" from __future__ import print_function @@ -10,6 +13,7 @@ import atexit import os import signal import sys + from experiment_runner import ExperimentRunner from experiment_runner import MockExperimentRunner from experiment_factory import ExperimentFactory @@ -137,14 +141,11 @@ def RunCrosperf(argv): def Main(argv): try: RunCrosperf(argv) - except Exception as ex: + except Exception: # Flush buffers before exiting to avoid out of order printing sys.stdout.flush() - sys.stderr.flush() - print('Crosperf error: %s' % repr(ex)) - sys.stdout.flush() - sys.stderr.flush() - sys.exit(1) + # Raise exception prints out traceback + raise if __name__ == '__main__': diff --git a/crosperf/crosperf_test.py b/crosperf/crosperf_test.py deleted file mode 100755 index 085efafe..00000000 --- a/crosperf/crosperf_test.py +++ /dev/null @@ -1,44 +0,0 @@ -#!/usr/bin/env python2 - -# Copyright 2011 Google Inc. All Rights Reserved. -"""Test for crosperf.""" - -from __future__ import print_function - -import os -import tempfile -import unittest -import crosperf -from cros_utils.file_utils import FileUtils - -EXPERIMENT_FILE_1 = """ - board: x86-alex - remote: chromeos-alex3 - - benchmark: PageCycler { - iterations: 3 - } - - image1 { - chromeos_image: /usr/local/google/cros_image1.bin - } - - image2 { - chromeos_image: /usr/local/google/cros_image2.bin - } - """ - - -class CrosPerfTest(unittest.TestCase): - """Class to test Crosperf.""" - - def testDryRun(self): - filehandle, filename = tempfile.mkstemp() - os.write(filehandle, EXPERIMENT_FILE_1) - crosperf.Main(['', filename, '--dry_run']) - os.remove(filename) - - -if __name__ == '__main__': - FileUtils.Configure(True) - unittest.main() diff --git a/crosperf/crosperf_unittest.py b/crosperf/crosperf_unittest.py index c89c59f1..65a587a5 100755 --- a/crosperf/crosperf_unittest.py +++ b/crosperf/crosperf_unittest.py @@ -10,8 +10,10 @@ from __future__ import print_function import argparse +import mock +import tempfile +import os import StringIO - import unittest import crosperf @@ -44,7 +46,13 @@ class CrosperfTest(unittest.TestCase): input_file = StringIO.StringIO(EXPERIMENT_FILE_1) self.exp_file = experiment_file.ExperimentFile(input_file) - def test_convert_options_to_settings(self): + def testDryRun(self): + filehandle, filename = tempfile.mkstemp() + os.write(filehandle, EXPERIMENT_FILE_1) + crosperf.Main(['', filename, '--dry_run']) + os.remove(filename) + + def testConvertOptionsToSettings(self): parser = argparse.ArgumentParser() parser.add_argument( '-l', @@ -66,6 +74,18 @@ class CrosperfTest(unittest.TestCase): settings = crosperf.ConvertOptionsToSettings(options) self.assertFalse(settings.GetField('rerun')) + def testExceptionPrintTraceback(self): + """Test the main function can print traceback in exception.""" + + def mock_RunCrosperf(*_args, **_kwargs): + return 10 / 0 + + with mock.patch('crosperf.RunCrosperf', new=mock_RunCrosperf): + with self.assertRaises(ZeroDivisionError) as context: + crosperf.Main([]) + self.assertEqual('integer division or modulo by zero', + str(context.exception)) + if __name__ == '__main__': unittest.main() |