Author: Antonio Cuni <[email protected]>
Branch: vmprof-0.4.10
Changeset: r92967:6c26abf30648
Date: 2017-11-08 01:21 +0100
http://bitbucket.org/pypy/pypy/changeset/6c26abf30648/

Log:    improve test_enable by: 1) make sure that it runs for approximately
        0.5 seconds; 2) check that the number of profiles is what we expect

diff --git a/rpython/rlib/rvmprof/test/test_rvmprof.py 
b/rpython/rlib/rvmprof/test/test_rvmprof.py
--- a/rpython/rlib/rvmprof/test/test_rvmprof.py
+++ b/rpython/rlib/rvmprof/test/test_rvmprof.py
@@ -1,5 +1,6 @@
 import py, os
 import pytest
+import time
 from rpython.tool.udir import udir
 from rpython.rlib import rvmprof
 from rpython.translator.c.test.test_genc import compile
@@ -89,6 +90,10 @@
 
 class RVMProfSamplingTest(RVMProfTest):
 
+    # the kernel will deliver SIGPROF at max 250 Hz. See also
+    # https://github.com/vmprof/vmprof-python/issues/163
+    SAMPLING_INTERVAL = 1/250.0
+
     @pytest.fixture
     def init(self, tmpdir):
         self.tmpdir = tmpdir
@@ -97,41 +102,44 @@
         super(RVMProfSamplingTest, self).init()
 
     ENTRY_POINT_ARGS = (int, float)
-    def entry_point(self, count, period):
+    def entry_point(self, value, delta_t):
         code = self.MyCode('py:code:52:test_enable')
         rvmprof.register_code(code, self.MyCode.get_name)
         fd = os.open(self.tmpfilename, os.O_WRONLY | os.O_CREAT, 0666)
-        rvmprof.enable(fd, period)
-        res = self.main(code, count)
+        rvmprof.enable(fd, self.SAMPLING_INTERVAL)
+        start = time.time()
+        res = 0
+        while time.time() < start+delta_t:
+            res = self.main(code, value)
         rvmprof.disable()
         os.close(fd)
         return res
 
+    def approx_equal(self, a, b, tolerance=0.1):
+        max_diff = (a+b)/2.0 * tolerance
+        return abs(a-b) < max_diff
 
 class TestEnable(RVMProfSamplingTest):
 
     @rvmprof.vmprof_execute_code("xcode1", lambda self, code, count: code)
     def main(self, code, count):
-        print count
         s = 0
         for i in range(count):
             s += (i << 1)
-            if s % 2123423423 == 0:
-                print s
         return s
 
     def test(self):
         from vmprof import read_profile
-        assert self.entry_point(10**4, 0.9) == 99990000
+        assert self.entry_point(10**4, 0.1) == 99990000
         assert self.tmpfile.check()
         self.tmpfile.remove()
         #
-        assert self.rpy_entry_point(10**8, 0.0001) == 9999999900000000
+        assert self.rpy_entry_point(10**4, 0.5) == 99990000
         assert self.tmpfile.check()
         prof = read_profile(self.tmpfilename)
         tree = prof.get_tree()
         assert tree.name == 'py:code:52:test_enable'
-        assert tree.count
+        assert self.approx_equal(tree.count, 0.5/self.SAMPLING_INTERVAL)
 
 
 def test_native():
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to