Patchwork [V2] perf: add historical portability for util.timer

login
register
mail settings
Submitter Philippe Pepiot
Date April 5, 2017, 4:14 p.m.
Message ID <c0231296470330eeae91.1491408887@sh71.tls.logilab.fr>
Download mbox | patch
Permalink /patch/19972/
State Superseded
Headers show

Comments

Philippe Pepiot - April 5, 2017, 4:14 p.m.
# HG changeset patch
# User Philippe Pepiot <philippe.pepiot@logilab.fr>
# Date 1491391351 -7200
#      Wed Apr 05 13:22:31 2017 +0200
# Node ID c0231296470330eeae91058b9a3d7a6785e16cbe
# Parent  04ec317b81280c189fcea33a05c8cbbac3c186b1
# Available At https://hg.logilab.org/users/ppepiot/hg
#              hg pull https://hg.logilab.org/users/ppepiot/hg -r c02312964703
perf: add historical portability for util.timer

util.timer has been introduced in ae5d60bb and used in perf.py since 22fbca1d.
For historical portability, use locally defined timer.
Yuya Nishihara - April 6, 2017, 12:31 p.m.
On Wed, 05 Apr 2017 18:14:47 +0200, Philippe Pepiot wrote:
> # HG changeset patch
> # User Philippe Pepiot <philippe.pepiot@logilab.fr>
> # Date 1491391351 -7200
> #      Wed Apr 05 13:22:31 2017 +0200
> # Node ID c0231296470330eeae91058b9a3d7a6785e16cbe
> # Parent  04ec317b81280c189fcea33a05c8cbbac3c186b1
> # Available At https://hg.logilab.org/users/ppepiot/hg
> #              hg pull https://hg.logilab.org/users/ppepiot/hg -r c02312964703
> perf: add historical portability for util.timer
> 
> util.timer has been introduced in ae5d60bb and used in perf.py since 22fbca1d.
> For historical portability, use locally defined timer.
> 
> diff --git a/contrib/perf.py b/contrib/perf.py
> --- a/contrib/perf.py
> +++ b/contrib/perf.py
> @@ -67,6 +67,14 @@ def safehasattr(thing, attr):
>  setattr(util, 'safehasattr', safehasattr)
>  
>  # for "historical portability":
> +# use locally defined timer instead of util.timer, because util.timer has been
> +# available since ae5d60bb70c9
> +if safehasattr(time, 'perf_counter'):
> +    timer = time.perf_counter
> +else:
> +    timer = time.time

Any reason to drop time.clock() support?

And I found "timer" is widely used in perf.py for different meaning. Perhaps
we'll need another name. Sorry I didn't notice it yesterday.
Philippe Pepiot - April 6, 2017, 12:55 p.m.
On 04/06/2017 02:31 PM, Yuya Nishihara wrote:
> On Wed, 05 Apr 2017 18:14:47 +0200, Philippe Pepiot wrote:
>> # HG changeset patch
>> # User Philippe Pepiot <philippe.pepiot@logilab.fr>
>> # Date 1491391351 -7200
>> #      Wed Apr 05 13:22:31 2017 +0200
>> # Node ID c0231296470330eeae91058b9a3d7a6785e16cbe
>> # Parent  04ec317b81280c189fcea33a05c8cbbac3c186b1
>> # Available At https://hg.logilab.org/users/ppepiot/hg
>> #              hg pull https://hg.logilab.org/users/ppepiot/hg -r c02312964703
>> perf: add historical portability for util.timer
>>
>> util.timer has been introduced in ae5d60bb and used in perf.py since 22fbca1d.
>> For historical portability, use locally defined timer.
>>
>> diff --git a/contrib/perf.py b/contrib/perf.py
>> --- a/contrib/perf.py
>> +++ b/contrib/perf.py
>> @@ -67,6 +67,14 @@ def safehasattr(thing, attr):
>>  setattr(util, 'safehasattr', safehasattr)
>>
>>  # for "historical portability":
>> +# use locally defined timer instead of util.timer, because util.timer has been
>> +# available since ae5d60bb70c9
>> +if safehasattr(time, 'perf_counter'):
>> +    timer = time.perf_counter
>> +else:
>> +    timer = time.time
>
> Any reason to drop time.clock() support?

I dropped windows support by mistake (previous version of perf.py was 
only using time.time).

>
> And I found "timer" is widely used in perf.py for different meaning. Perhaps
> we'll need another name. Sorry I didn't notice it yesterday.
>

Good catch, sorry to not have seen it before submission. I propose to 
forcibly define util.timer instead (like util.safehasattr).

Will send a V3

Patch

diff --git a/contrib/perf.py b/contrib/perf.py
--- a/contrib/perf.py
+++ b/contrib/perf.py
@@ -67,6 +67,14 @@  def safehasattr(thing, attr):
 setattr(util, 'safehasattr', safehasattr)
 
 # for "historical portability":
+# use locally defined timer instead of util.timer, because util.timer has been
+# available since ae5d60bb70c9
+if safehasattr(time, 'perf_counter'):
+    timer = time.perf_counter
+else:
+    timer = time.time
+
+# for "historical portability":
 # use locally defined empty option list, if formatteropts isn't
 # available, because commands.formatteropts has been available since
 # 3.2 (or 7a7eed5176a4), even though formatting itself has been
@@ -193,13 +201,13 @@  def stub_timer(fm, func, title=None):
 def _timer(fm, func, title=None):
     gc.collect()
     results = []
-    begin = util.timer()
+    begin = timer()
     count = 0
     while True:
         ostart = os.times()
-        cstart = util.timer()
+        cstart = timer()
         r = func()
-        cstop = util.timer()
+        cstop = timer()
         ostop = os.times()
         count += 1
         a, b = ostart, ostop
diff --git a/tests/test-check-code.t b/tests/test-check-code.t
--- a/tests/test-check-code.t
+++ b/tests/test-check-code.t
@@ -9,7 +9,7 @@  New errors are not allowed. Warnings are
 
   $ hg locate -X contrib/python-zstandard -X hgext/fsmonitor/pywatchman |
   > sed 's-\\-/-g' | xargs "$check_code" --warnings --per-file=0 || false
-  contrib/perf.py:859:
+  contrib/perf.py:867:
    >             r.revision(r.node(x))
    don't covert rev to node before passing to revision(nodeorrev)
   Skipping i18n/polib.py it has no-che?k-code (glob)
diff --git a/tests/test-contrib-perf.t b/tests/test-contrib-perf.t
--- a/tests/test-contrib-perf.t
+++ b/tests/test-contrib-perf.t
@@ -165,7 +165,7 @@  Check perf.py for historical portability
   $ (hg files -r 1.2 glob:mercurial/*.c glob:mercurial/*.py;
   >  hg files -r tip glob:mercurial/*.c glob:mercurial/*.py) |
   > "$TESTDIR"/check-perf-code.py contrib/perf.py
-  contrib/perf.py:859:
+  contrib/perf.py:867:
    >             r.revision(r.node(x))
    don't covert rev to node before passing to revision(nodeorrev)
   [1]