Title: [279377] trunk/Tools
Revision
279377
Author
jbed...@apple.com
Date
2021-06-29 11:07:34 -0700 (Tue, 29 Jun 2021)

Log Message

[webkitcorepy] Add MeasureTime python tool
https://bugs.webkit.org/show_bug.cgi?id=227313
<rdar://problem/79689588>

Reviewed by Dewei Zhu.

* Scripts/libraries/webkitcorepy/setup.py: Bump version.
* Scripts/libraries/webkitcorepy/webkitcorepy/__init__.py: Ditto.
* Scripts/libraries/webkitcorepy/webkitcorepy/__init__.py: Export MeasureTime object.
* Scripts/libraries/webkitcorepy/webkitcorepy/measure_time.py: Added.
(MeasureTime): Provide class which logs time elapsed in context.
* Scripts/libraries/webkitcorepy/webkitcorepy/tests/measure_time_unittest.py: Added.
(MeasureTimeTests):

Modified Paths

Added Paths

Diff

Modified: trunk/Tools/ChangeLog (279376 => 279377)


--- trunk/Tools/ChangeLog	2021-06-29 18:00:06 UTC (rev 279376)
+++ trunk/Tools/ChangeLog	2021-06-29 18:07:34 UTC (rev 279377)
@@ -1,3 +1,19 @@
+2021-06-29  Jonathan Bedard  <jbed...@apple.com>
+
+        [webkitcorepy] Add MeasureTime python tool
+        https://bugs.webkit.org/show_bug.cgi?id=227313
+        <rdar://problem/79689588>
+
+        Reviewed by Dewei Zhu.
+
+        * Scripts/libraries/webkitcorepy/setup.py: Bump version.
+        * Scripts/libraries/webkitcorepy/webkitcorepy/__init__.py: Ditto.
+        * Scripts/libraries/webkitcorepy/webkitcorepy/__init__.py: Export MeasureTime object.
+        * Scripts/libraries/webkitcorepy/webkitcorepy/measure_time.py: Added.
+        (MeasureTime): Provide class which logs time elapsed in context.
+        * Scripts/libraries/webkitcorepy/webkitcorepy/tests/measure_time_unittest.py: Added.
+        (MeasureTimeTests):
+
 2021-06-28  Wenson Hsieh  <wenson_hs...@apple.com>
 
         REGRESSION (r279310): Occasional crash when focusing login fields on iPad with a software keyboard

Modified: trunk/Tools/Scripts/libraries/webkitcorepy/setup.py (279376 => 279377)


--- trunk/Tools/Scripts/libraries/webkitcorepy/setup.py	2021-06-29 18:00:06 UTC (rev 279376)
+++ trunk/Tools/Scripts/libraries/webkitcorepy/setup.py	2021-06-29 18:07:34 UTC (rev 279377)
@@ -30,7 +30,7 @@
 
 setup(
     name='webkitcorepy',
-    version='0.6.0',
+    version='0.6.1',
     description='Library containing various Python support classes and functions.',
     long_description=readme(),
     classifiers=[

Modified: trunk/Tools/Scripts/libraries/webkitcorepy/webkitcorepy/__init__.py (279376 => 279377)


--- trunk/Tools/Scripts/libraries/webkitcorepy/webkitcorepy/__init__.py	2021-06-29 18:00:06 UTC (rev 279376)
+++ trunk/Tools/Scripts/libraries/webkitcorepy/webkitcorepy/__init__.py	2021-06-29 18:07:34 UTC (rev 279377)
@@ -36,8 +36,9 @@
 from webkitcorepy.output_capture import LoggerCapture, OutputCapture, OutputDuplicate
 from webkitcorepy.task_pool import TaskPool
 from webkitcorepy.credentials import credentials
+from webkitcorepy.measure_time import MeasureTime
 
-version = Version(0, 6, 0)
+version = Version(0, 6, 1)
 
 from webkitcorepy.autoinstall import Package, AutoInstall
 if sys.version_info > (3, 0):

Copied: trunk/Tools/Scripts/libraries/webkitcorepy/webkitcorepy/measure_time.py (from rev 279375, trunk/Tools/Scripts/libraries/webkitcorepy/setup.py) (0 => 279377)


--- trunk/Tools/Scripts/libraries/webkitcorepy/webkitcorepy/measure_time.py	                        (rev 0)
+++ trunk/Tools/Scripts/libraries/webkitcorepy/webkitcorepy/measure_time.py	2021-06-29 18:07:34 UTC (rev 279377)
@@ -0,0 +1,53 @@
+# Copyright (C) 2021 Apple Inc. All rights reserved.
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions
+# are met:
+# 1.  Redistributions of source code must retain the above copyright
+#    notice, this list of conditions and the following disclaimer.
+# 2.  Redistributions in binary form must reproduce the above copyright
+#    notice, this list of conditions and the following disclaimer in the
+#    documentation and/or other materials provided with the distribution.
+#
+# THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND ANY
+# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+# DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY
+# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
+# ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+import time
+
+from webkitcorepy import log
+
+
+class MeasureTime(object):
+    def __init__(self, log=False, name=None):
+        self.started = None
+        self.ended = None
+        self.log = log
+        self.name = name
+
+    @property
+    def elapsed(self):
+        if not self.started:
+            return None
+        if not self.ended:
+            return time.time() - self.started
+        return self.ended - self.started
+
+    def __enter__(self):
+        self.ended = None
+        self.started = time.time()
+        return self
+
+    def __exit__(self, *args, **kwargs):
+        self.ended = time.time()
+        if self.log and self.name:
+            log.critical('{}: {} seconds elapsed'.format(self.name, self.elapsed or 'No'))
+        elif self.log:
+            log.critical('{} seconds elapsed'.format(self.elapsed or 'No'))

Copied: trunk/Tools/Scripts/libraries/webkitcorepy/webkitcorepy/tests/measure_time_unittest.py (from rev 279375, trunk/Tools/Scripts/libraries/webkitcorepy/setup.py) (0 => 279377)


--- trunk/Tools/Scripts/libraries/webkitcorepy/webkitcorepy/tests/measure_time_unittest.py	                        (rev 0)
+++ trunk/Tools/Scripts/libraries/webkitcorepy/webkitcorepy/tests/measure_time_unittest.py	2021-06-29 18:07:34 UTC (rev 279377)
@@ -0,0 +1,49 @@
+# Copyright (C) 2021 Apple Inc. All rights reserved.
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions
+# are met:
+# 1.  Redistributions of source code must retain the above copyright
+#    notice, this list of conditions and the following disclaimer.
+# 2.  Redistributions in binary form must reproduce the above copyright
+#    notice, this list of conditions and the following disclaimer in the
+#    documentation and/or other materials provided with the distribution.
+#
+# THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND ANY
+# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+# DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY
+# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
+# ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+import time
+import unittest
+
+from webkitcorepy import LoggerCapture, MeasureTime, mocks
+
+
+class MeasureTimeTests(unittest.TestCase):
+    def test_basic(self):
+        with mocks.Time, LoggerCapture() as captured:
+            with MeasureTime() as measured:
+                self.assertEqual(measured.elapsed, 0)
+                time.sleep(1)
+                self.assertEqual(measured.elapsed, 1)
+            self.assertEqual(measured.elapsed, 1)
+        self.assertEqual(captured.log.getvalue(), '')
+
+    def test_log(self):
+        with mocks.Time, LoggerCapture() as captured:
+            with MeasureTime(log=True):
+                time.sleep(1)
+        self.assertEqual(captured.log.getvalue(), '1.0 seconds elapsed\n')
+
+    def test_log_name(self):
+        with mocks.Time, LoggerCapture() as captured:
+            with MeasureTime(name='Example', log=True):
+                time.sleep(1)
+        self.assertEqual(captured.log.getvalue(), 'Example: 1.0 seconds elapsed\n')
_______________________________________________
webkit-changes mailing list
webkit-changes@lists.webkit.org
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to