Change in vdsm[master]: profile: Add couple of tests

nsoffer at redhat.com nsoffer at redhat.com
Fri May 2 20:01:03 UTC 2014


Nir Soffer has uploaded a new change for review.

Change subject: profile: Add couple of tests
......................................................................

profile: Add couple of tests

We plan to modify the profiler so it will be useable for the functional
tests, where we like to have one profile per test, instead of the
application wide profile.

This patch adds couple of tests to make it easier to modify the profiler
without breaking anything.

Change-Id: I0dedc21993972ec25881f46f0b365445ed494aac
Signed-off-by: Nir Soffer <nsoffer at redhat.com>
---
M lib/vdsm/profile.py
M tests/Makefile.am
A tests/profileTests.py
3 files changed, 113 insertions(+), 2 deletions(-)


  git pull ssh://gerrit.ovirt.org:29418/vdsm refs/changes/28/27328/1

diff --git a/lib/vdsm/profile.py b/lib/vdsm/profile.py
index 1f3d4b1..9c50c89 100644
--- a/lib/vdsm/profile.py
+++ b/lib/vdsm/profile.py
@@ -31,6 +31,9 @@
 # Import yappi only if profile is enabled
 yappi = None
 
+_FILENAME = os.path.join(constants.P_VDSM_RUN, 'vdsmd.prof')
+_FORMAT = config.get('vars', 'profile_format')
+
 
 def start():
     global yappi
@@ -45,8 +48,7 @@
         logging.debug("Stopping profiling")
         yappi.stop()
         stats = yappi.get_func_stats()
-        path = os.path.join(constants.P_VDSM_RUN, 'vdsmd.prof')
-        stats.save(path, config.get('vars', 'profile_format'))
+        stats.save(_FILENAME, _FORMAT)
 
 
 def is_enabled():
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 21b3885..f34065b 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -60,6 +60,7 @@
 	parted_utils_tests.py \
 	permutationTests.py \
 	persistentDictTests.py \
+	profileTests.py \
 	remoteFileHandlerTests.py \
 	resourceManagerTests.py \
 	samplingTests.py \
diff --git a/tests/profileTests.py b/tests/profileTests.py
new file mode 100644
index 0000000..980d2ed
--- /dev/null
+++ b/tests/profileTests.py
@@ -0,0 +1,108 @@
+#
+# Copyright 2014 Red Hat, Inc.
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA
+#
+# Refer to the README and COPYING files for full details of the license
+#
+
+import ConfigParser
+import errno
+import os
+import pstats
+
+from vdsm import profile
+
+from monkeypatch import MonkeyPatch
+from nose.plugins.skip import SkipTest
+from testrunner import VdsmTestCase
+
+yappi = None
+try:
+    import yappi
+except ImportError:
+    pass
+
+FILENAME = __file__ + '.prof'
+
+
+def make_config(enable='false', format='pstat'):
+    cfg = ConfigParser.ConfigParser()
+    cfg.add_section('vars')
+    cfg.set('vars', 'profile_enable', enable)
+    cfg.set('vars', 'profile_format', format)
+    return cfg
+
+
+def requires_yappi():
+    if yappi is None:
+        raise SkipTest('yappi is not installed')
+
+
+class ApplicationProfileTests(VdsmTestCase):
+
+    def tearDown(self):
+        try:
+            os.unlink(FILENAME)
+        except OSError as e:
+            if e.errno != errno.ENOENT:
+                raise
+
+    @MonkeyPatch(profile, 'config', make_config(enable='true'))
+    @MonkeyPatch(profile, '_FILENAME', FILENAME)
+    @MonkeyPatch(profile, '_FORMAT', 'pstat')
+    def test_pstats_format(self):
+        requires_yappi()
+        profile.start()
+        profile.is_running()  # Let if profile somthing
+        profile.stop()
+        pstats.Stats(FILENAME)
+
+    @MonkeyPatch(profile, 'config', make_config(enable='true'))
+    @MonkeyPatch(profile, '_FILENAME', FILENAME)
+    @MonkeyPatch(profile, '_FORMAT', 'ystat')
+    def test_ystats_format(self):
+        requires_yappi()
+        profile.start()
+        profile.is_running()  # Let if profile somthing
+        profile.stop()
+        stats = yappi.YFuncStats()
+        stats.add(FILENAME)
+
+    @MonkeyPatch(profile, 'config', make_config(enable='true'))
+    @MonkeyPatch(profile, '_FILENAME', FILENAME)
+    def test_is_running(self):
+        requires_yappi()
+        self.assertFalse(profile.is_running())
+        profile.start()
+        try:
+            self.assertTrue(profile.is_running())
+        finally:
+            profile.stop()
+        self.assertFalse(profile.is_running())
+
+    @MonkeyPatch(profile, 'config', make_config(enable='true'))
+    def test_is_enbaled(self):
+        requires_yappi()
+        self.assertTrue(profile.is_enabled())
+
+    # This must succeed even if yappi is not installed
+    @MonkeyPatch(profile, 'config', make_config(enable='false'))
+    def test_disabled(self):
+        profile.start()
+        try:
+            self.assertFalse(profile.is_running())
+        finally:
+            profile.stop()


-- 
To view, visit http://gerrit.ovirt.org/27328
To unsubscribe, visit http://gerrit.ovirt.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I0dedc21993972ec25881f46f0b365445ed494aac
Gerrit-PatchSet: 1
Gerrit-Project: vdsm
Gerrit-Branch: master
Gerrit-Owner: Nir Soffer <nsoffer at redhat.com>


More information about the vdsm-patches mailing list