Patchwork D10469: profiling: add --no-profile to disable profiling enabled via config

login
register
mail settings
Submitter phabricator
Date April 19, 2021, 10:32 p.m.
Message ID <differential-rev-PHID-DREV-3jblwvem3bh6eajjqt4q-req@mercurial-scm.org>
Download mbox | patch
Permalink /patch/48787/
State Superseded
Headers show

Comments

phabricator - April 19, 2021, 10:32 p.m.
spectral created this revision.
Herald added a reviewer: hg-reviewers.
Herald added a subscriber: mercurial-patches.

REPOSITORY
  rHG Mercurial

BRANCH
  default

REVISION DETAIL
  https://phab.mercurial-scm.org/D10469

AFFECTED FILES
  mercurial/dispatch.py
  tests/test-chg.t

CHANGE DETAILS




To: spectral, #hg-reviewers
Cc: mercurial-patches, mercurial-devel

Patch

diff --git a/tests/test-chg.t b/tests/test-chg.t
--- a/tests/test-chg.t
+++ b/tests/test-chg.t
@@ -468,3 +468,72 @@ 
   LC_ALL=
   LC_CTYPE=
   LANG=
+
+Profiling isn't permanently enabled or carried over between chg invocations that
+share the same server
+  $ cp $HGRCPATH.orig $HGRCPATH
+  $ hg init $TESTTMP/profiling
+  $ cd $TESTTMP/profiling
+  $ filteredchg() {
+  >   CHGDEBUG=1 chg "$@" 2>&1 | egrep 'Sample count|start cmdserver' || true
+  > }
+  $ newchg() {
+  >   chg --kill-chg-daemon
+  >   filteredchg "$@" | egrep -v 'start cmdserver' || true
+  > }
+(--profile isn't permanently on just because it was specified when chg was
+started)
+  $ newchg log -r . --profile
+  Sample count: * (glob)
+  $ filteredchg log -r .
+(enabling profiling via config works, even on the first chg command that starts
+a cmdserver)
+  $ cat >> $HGRCPATH <<EOF
+  > [profiling]
+  > type=stat
+  > enabled=1
+  > EOF
+  $ newchg log -r .
+  Sample count: * (glob)
+  $ filteredchg log -r .
+  Sample count: * (glob)
+(test that we aren't accumulating more and more samples each run)
+  $ cat > $TESTTMP/debugsleep.py <<EOF
+  > import time
+  > from mercurial import registrar
+  > cmdtable = {}
+  > command = registrar.command(cmdtable)
+  > @command(b'debugsleep', [], b'', norepo=True)
+  > def debugsleep(ui):
+  >   start = time.time()
+  >   x = 0
+  >   while time.time() < start + 0.5:
+  >     time.sleep(.1)
+  >     x += 1
+  >   ui.status(b'%d debugsleep iterations in %.03fs\n' % (x, time.time() - start))
+  > EOF
+  $ cat >> $HGRCPATH <<EOF
+  > [extensions]
+  > debugsleep = $TESTTMP/debugsleep.py
+  > EOF
+  $ newchg debugsleep > run_1
+  $ filteredchg debugsleep > run_2
+  $ filteredchg debugsleep > run_3
+  $ filteredchg debugsleep > run_4
+FIXME: Run 4 should not be >3x Run 1's number of samples.
+  $ "$PYTHON" <<EOF
+  > r1 = int(open("run_1", "r").read().split()[-1])
+  > r4 = int(open("run_4", "r").read().split()[-1])
+  > print("Run 1: %d samples\nRun 4: %d samples\nRun 4 > 3 * Run 1: %s" %
+  >       (r1, r4, r4 > (r1 * 3)))
+  > EOF
+  Run 1: * samples (glob)
+  Run 4: * samples (glob)
+  Run 4 > 3 * Run 1: True
+(Disabling with --no-profile on the commandline still works, but isn't permanent)
+  $ newchg log -r . --no-profile
+  $ filteredchg log -r .
+  Sample count: * (glob)
+  $ filteredchg log -r . --no-profile
+  $ filteredchg log -r .
+  Sample count: * (glob)
diff --git a/mercurial/dispatch.py b/mercurial/dispatch.py
--- a/mercurial/dispatch.py
+++ b/mercurial/dispatch.py
@@ -1063,6 +1063,16 @@ 
     if req.earlyoptions[b'profile']:
         for ui_ in uis:
             ui_.setconfig(b'profiling', b'enabled', b'true', b'--profile')
+    elif req.earlyoptions[b'profile'] is False:
+        # Check for it being set already, so that we don't pollute the config
+        # with this when using chg in the very common case that it's not
+        # enabled.
+        if lui.configbool(b'profiling', b'enabled'):
+            # Only do this on lui so that `chg foo` with a user config setting
+            # profiling.enabled=1 still shows profiling information (chg will
+            # specify `--no-profile` when `hg serve` is starting up, we don't
+            # want that to propagate to every later invocation).
+            lui.setconfig(b'profiling', b'enabled', b'false', b'--no-profile')
 
     profile = lui.configbool(b'profiling', b'enabled')
     with profiling.profile(lui, enabled=profile) as profiler: