1 # Copyright (C) The Arvados Authors. All rights reserved.
3 # SPDX-License-Identifier: Apache-2.0
5 # Use the "profiled" decorator on a test to get profiling data.
8 # from performance_profiler import profiled
10 # # See report in tmp/profile/foobar
15 # See "test_a_sample.py" for a working example.
17 # Performance tests run as part of regular test suite.
18 # You can also run only the performance tests using one of the following:
19 # python -m unittest discover tests.performance
20 # ./run-tests.sh WORKSPACE=~/arvados --only sdk/python sdk/python_test="--test-suite=tests.performance"
28 import cProfile as profile
32 output_dir = os.path.abspath(os.path.join('tmp', 'profile'))
33 if not os.path.exists(output_dir):
34 os.makedirs(output_dir)
36 def profiled(function):
37 @functools.wraps(function)
38 def profiled_function(*args, **kwargs):
39 outfile = open(os.path.join(output_dir, function.__name__), "w")
41 pr = profile.Profile()
44 return function(*args, **kwargs)
47 ps = pstats.Stats(pr, stream=outfile)
48 ps.sort_stats('time').print_stats()
49 return profiled_function