Skip to content

Commit 696579e

Browse files
committed
Skip the decorator if an environment variable is set.
This should be a minor speedup and gives cleaner back traces. This can't be done with a flag since imports happen before flags. PiperOrigin-RevId: 213631051
1 parent ee86f00 commit 696579e

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

pysc2/lib/stopwatch.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
from collections import defaultdict
2121
import functools
2222
import math
23+
import os
2324
import sys
2425
import threading
2526
import time
@@ -190,6 +191,9 @@ def random_func_name():
190191
If a name is passed, returns this as a decorator, otherwise returns the
191192
decorated function.
192193
"""
194+
if os.environ.get("SC2_NO_STOPWATCH"):
195+
return name_or_func if callable(name_or_func) else lambda func: func
196+
193197
def decorator(name, func):
194198
@functools.wraps(func)
195199
def _stopwatch(*args, **kwargs):
@@ -236,6 +240,7 @@ def merge(self, other):
236240

237241
@staticmethod
238242
def parse(s):
243+
"""Parse the output below to create a new StopWatch."""
239244
stopwatch = StopWatch()
240245
for line in s.splitlines():
241246
if line.strip():

pysc2/lib/stopwatch_test.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
from __future__ import division
1919
from __future__ import print_function
2020

21+
import os
22+
2123
from absl.testing import absltest
2224
from future.builtins import range # pylint: disable=redefined-builtin
2325

@@ -103,6 +105,18 @@ def testDivideZero(self):
103105
# Just make sure this doesn't have a divide by 0 for when the total is 0.
104106
self.assertIn("zero", str(sw))
105107

108+
@mock.patch.dict(os.environ, {"SC2_NO_STOPWATCH": "1"})
109+
def testDecoratorDisabled(self):
110+
sw = stopwatch.StopWatch()
111+
self.assertEqual(round, sw.decorate(round))
112+
self.assertEqual(round, sw.decorate("name")(round))
113+
114+
@mock.patch.dict(os.environ, {"SC2_NO_STOPWATCH": ""})
115+
def testDecoratorEnabled(self):
116+
sw = stopwatch.StopWatch()
117+
self.assertNotEqual(round, sw.decorate(round))
118+
self.assertNotEqual(round, sw.decorate("name")(round))
119+
106120
def testSpeed(self):
107121
count = 1000
108122

0 commit comments

Comments
 (0)