Nir Soffer has uploaded a new change for review.
Change subject: profiling: Add env context manager ......................................................................
profiling: Add env context manager
Instead of having multiple @MonkeyPatch() lines for each test, use a context manager that creates the environment needed for the tests.
Change-Id: I936957c1fe96a69886dae63f45ffb463bb18279f Signed-off-by: Nir Soffer nsoffer@redhat.com --- M tests/cpu_profile_test.py 1 file changed, 114 insertions(+), 113 deletions(-)
git pull ssh://gerrit.ovirt.org:29418/vdsm refs/changes/54/65254/1
diff --git a/tests/cpu_profile_test.py b/tests/cpu_profile_test.py index 850d5e8..457dcda 100644 --- a/tests/cpu_profile_test.py +++ b/tests/cpu_profile_test.py @@ -24,10 +24,12 @@ import time import threading
+from contextlib import contextmanager + from vdsm.profiling import cpu from vdsm.profiling.errors import UsageError
-from monkeypatch import MonkeyPatch +from monkeypatch import MonkeyPatch, MonkeyPatchScope from nose.plugins.skip import SkipTest from testlib import VdsmTestCase, make_config
@@ -55,106 +57,107 @@ raise
-def config(enable='true', format='pstat', clock='cpu', builtins='false'): - return make_config([ +@contextmanager +def env(enable='true', format='pstat', clock='cpu', builtins='false'): + config = make_config([ ('devel', 'cpu_profile_enable', enable), ('devel', 'cpu_profile_format', format), ('devel', 'cpu_profile_clock', clock), ('devel', 'cpu_profile_builtins', builtins), ]) + with MonkeyPatchScope([(cpu, 'config', config)]): + yield
class ApplicationProfileTests(ProfileTests):
- @MonkeyPatch(cpu, 'config', config()) @MonkeyPatch(cpu, '_FILENAME', FILENAME) def test_pstats_format(self): requires_yappi() - cpu.start() - cpu.is_running() # Let if profile something - cpu.stop() - self.assertNotRaises(pstats.Stats, FILENAME) + with env(): + cpu.start() + cpu.is_running() # Let if profile something + cpu.stop() + self.assertNotRaises(pstats.Stats, FILENAME)
- @MonkeyPatch(cpu, 'config', config(format='ystat')) @MonkeyPatch(cpu, '_FILENAME', FILENAME) def test_ystats_format(self): requires_yappi() - cpu.start() - cpu.is_running() # Let if profile something - cpu.stop() - self.assertNotRaises(open_ystats, FILENAME) + with env(format='ystat'): + cpu.start() + cpu.is_running() # Let if profile something + cpu.stop() + self.assertNotRaises(open_ystats, FILENAME)
- @MonkeyPatch(cpu, 'config', config(format='ystat', builtins='true')) @MonkeyPatch(cpu, '_FILENAME', FILENAME) def test_with_builtins(self): requires_yappi() - cpu.start() - dict() - cpu.stop() - stats = open_ystats(FILENAME) - self.assertTrue(find_module(stats, '__builtin__')) + with env(format='ystat', builtins='true'): + cpu.start() + dict() + cpu.stop() + stats = open_ystats(FILENAME) + self.assertTrue(find_module(stats, '__builtin__'))
- @MonkeyPatch(cpu, 'config', config(format='ystat', builtins='false')) @MonkeyPatch(cpu, '_FILENAME', FILENAME) def test_without_builtins(self): requires_yappi() - cpu.start() - dict() - cpu.stop() - stats = open_ystats(FILENAME) - self.assertFalse(find_module(stats, '__builtin__')) + with env(format='ystat', builtins='false'): + cpu.start() + dict() + cpu.stop() + stats = open_ystats(FILENAME) + self.assertFalse(find_module(stats, '__builtin__'))
- @MonkeyPatch(cpu, 'config', - config(format='ystat', clock='cpu', builtins='false')) @MonkeyPatch(cpu, '_FILENAME', FILENAME) def test_cpu_clock(self): requires_yappi() - cpu.start() - self.sleep(0.1) - cpu.stop() - stats = open_ystats(FILENAME) - name = function_name(self.sleep) - func = find_function(stats, __file__, name) - self.assertTrue(func.ttot < 0.1) + with env(format='ystat', clock='cpu', builtins='false'): + cpu.start() + self.sleep(0.1) + cpu.stop() + stats = open_ystats(FILENAME) + name = function_name(self.sleep) + func = find_function(stats, __file__, name) + self.assertTrue(func.ttot < 0.1)
- @MonkeyPatch(cpu, 'config', - config(format='ystat', clock='wall', builtins='false')) @MonkeyPatch(cpu, '_FILENAME', FILENAME) def test_wall_clock(self): requires_yappi() - cpu.start() - self.sleep(0.1) - cpu.stop() - stats = open_ystats(FILENAME) - name = function_name(self.sleep) - func = find_function(stats, __file__, name) - self.assertTrue(func.ttot > 0.1) + with env(format='ystat', clock='wall', builtins='false'): + cpu.start() + self.sleep(0.1) + cpu.stop() + stats = open_ystats(FILENAME) + name = function_name(self.sleep) + func = find_function(stats, __file__, name) + self.assertTrue(func.ttot > 0.1)
- @MonkeyPatch(cpu, 'config', config()) @MonkeyPatch(cpu, '_FILENAME', FILENAME) def test_is_running(self): requires_yappi() - self.assertFalse(cpu.is_running()) - cpu.start() - try: - self.assertTrue(cpu.is_running()) - finally: - cpu.stop() - self.assertFalse(cpu.is_running()) + with env(): + self.assertFalse(cpu.is_running()) + cpu.start() + try: + self.assertTrue(cpu.is_running()) + finally: + cpu.stop() + self.assertFalse(cpu.is_running())
- @MonkeyPatch(cpu, 'config', config(enable='true')) def test_is_enabled(self): requires_yappi() - self.assertTrue(cpu.is_enabled()) + with env(enable='true'): + self.assertTrue(cpu.is_enabled())
# This must succeed even if yappi is not installed - @MonkeyPatch(cpu, 'config', config(enable='false')) def test_disabled(self): - cpu.start() - try: - self.assertFalse(cpu.is_running()) - finally: - cpu.stop() + with env(enable='false'): + cpu.start() + try: + self.assertFalse(cpu.is_running()) + finally: + cpu.stop()
def sleep(self, seconds): time.sleep(seconds) @@ -163,69 +166,67 @@ class FunctionProfileTests(ProfileTests):
# Function profile must succeed if profile is disabled in config. - @MonkeyPatch(cpu, 'config', config(enable='false')) def test_profile_disabled(self): requires_yappi() - self.profiled_function() - self.assertNotRaises(pstats.Stats, FILENAME) + with env(enable='false'): + self.profiled_function() + self.assertNotRaises(pstats.Stats, FILENAME)
# Function profile must fail if profile is enabled in config - we cannot # use application wide profile and function profile in the same time. - @MonkeyPatch(cpu, 'config', config(enable='true')) @MonkeyPatch(cpu, '_FILENAME', FILENAME) def test_fail_if_Profile_is_running(self): requires_yappi() - cpu.start() - try: - self.assertRaises(UsageError, - self.profiled_function) - finally: - cpu.stop() + with env(enable='true'): + cpu.start() + try: + self.assertRaises(UsageError, self.profiled_function) + finally: + cpu.stop()
# It is not possible to call a profiled function from a profiled function. - @MonkeyPatch(cpu, 'config', config(enable='false')) def test_fail_recursive_profile(self): requires_yappi() - self.assertRaises(UsageError, - self.recursive_profile) + with env(enable='false'): + self.assertRaises(UsageError, self.recursive_profile)
- @MonkeyPatch(cpu, 'config', config(enable='false')) def test_ystat_format(self): requires_yappi() - self.ystat_format() - self.assertNotRaises(open_ystats, FILENAME) + with env(enable='false'): + self.ystat_format() + self.assertNotRaises(open_ystats, FILENAME)
- @MonkeyPatch(cpu, 'config', config(enable='false')) def test_with_builtins(self): requires_yappi() - self.with_builtins() - stats = open_ystats(FILENAME) - self.assertTrue(find_module(stats, '__builtin__')) + with env(enable='false'): + self.with_builtins() + stats = open_ystats(FILENAME) + self.assertTrue(find_module(stats, '__builtin__'))
- @MonkeyPatch(cpu, 'config', config(enable='false')) def test_without_builtins(self): requires_yappi() - self.without_builtins() - stats = open_ystats(FILENAME) - self.assertFalse(find_module(stats, '__builtin__')) + with env(enable='false'): + self.without_builtins() + stats = open_ystats(FILENAME) + self.assertFalse(find_module(stats, '__builtin__'))
- @MonkeyPatch(cpu, 'config', config(enable='false')) def test_cpu_clock(self): requires_yappi() - self.cpu_clock() - stats = open_ystats(FILENAME) - name = function_name(self.cpu_clock) - func = find_function(stats, __file__, name) - self.assertTrue(func.ttot < 0.1) + with env(enable='false'): + self.cpu_clock() + stats = open_ystats(FILENAME) + name = function_name(self.cpu_clock) + func = find_function(stats, __file__, name) + self.assertTrue(func.ttot < 0.1)
- @MonkeyPatch(cpu, 'config', config(enable='false')) def test_wall_clock(self): requires_yappi() - self.wall_clock() - stats = open_ystats(FILENAME) - name = function_name(self.wall_clock) - func = find_function(stats, __file__, name) - self.assertTrue(func.ttot > 0.1) + with env(enable='false'): + self.wall_clock() + stats = open_ystats(FILENAME) + name = function_name(self.wall_clock) + func = find_function(stats, __file__, name) + self.assertTrue(func.ttot > 0.1)
@cpu.profile(FILENAME) def profiled_function(self): @@ -263,34 +264,34 @@ self.ready = threading.Event() self.resume = threading.Event()
- @MonkeyPatch(cpu, 'config', config(enable='false')) def test_new_threads(self): # The easy case - threads started after yappi was started requires_yappi() - self.new_threads() - stats = open_ystats(FILENAME) - name = function_name(self.worker_function) - func = find_function(stats, __file__, name) - self.assertEquals(func.ncall, 1) + with env(enable='false'): + self.new_threads() + stats = open_ystats(FILENAME) + name = function_name(self.worker_function) + func = find_function(stats, __file__, name) + self.assertEquals(func.ncall, 1)
- @MonkeyPatch(cpu, 'config', config(enable='false')) def test_running_threads(self): # The harder case - threads started before yappi was started requires_yappi() - self.start_thread() - self.running_threads() - stats = open_ystats(FILENAME) - name = function_name(self.worker_function) - func = find_function(stats, __file__, name) - self.assertEquals(func.ncall, 1) + with env(enable='false'): + self.start_thread() + self.running_threads() + stats = open_ystats(FILENAME) + name = function_name(self.worker_function) + func = find_function(stats, __file__, name) + self.assertEquals(func.ncall, 1)
- @MonkeyPatch(cpu, 'config', config(enable='false')) def test_without_threads(self): requires_yappi() - self.without_threads() - stats = open_ystats(FILENAME) - name = function_name(self.worker_function) - self.assertRaises(NotFound, find_function, stats, __file__, name) + with env(enable='false'): + self.without_threads() + stats = open_ystats(FILENAME) + name = function_name(self.worker_function) + self.assertRaises(NotFound, find_function, stats, __file__, name)
@cpu.profile(FILENAME, format="ystat", threads=True) def new_threads(self):
gerrit-hooks has posted comments on this change.
Change subject: profiling: Add env context manager ......................................................................
Patch Set 1:
* Update tracker: IGNORE, no Bug-Url found * Check Bug-Url::WARN, no bug url found, make sure header matches 'Bug-Url: ' and is a valid url. * Check merged to previous::IGNORE, Not in stable branch (['ovirt-3.6', 'ovirt-4.0'])
Nir Soffer has posted comments on this change.
Change subject: profiling: Add env context manager ......................................................................
Patch Set 1: Verified+1
vdsm-patches@lists.fedorahosted.org