# Path: pysc2/lib/stopwatch.py
# class Stat(object):
# class StopWatchContext(object):
# class TracingStopWatchContext(StopWatchContext):
# class FakeStopWatchContext(object):
# class StopWatch(object):
#   def __init__(self):
#   def reset(self):
#   def add(self, val):
#   def avg(self):
#   def dev(self):
#   def merge(self, other):
#   def build(summation, average, standard_deviation, minimum, maximum, number):
#   def parse(s):
#   def __str__(self):
#   def __init__(self, stopwatch, name):
#   def __enter__(self):
#   def __exit__(self, unused_exception_type, unused_exc_value, unused_traceback):
#   def __enter__(self):
#   def __exit__(self, *args, **kwargs):
#   def _log(self, s):
#   def __enter__(self):
#   def __exit__(self, unused_exception_type, unused_exc_value, unused_traceback):
#   def __init__(self, enabled=True, trace=False):
#   def disable(self):
#   def enable(self):
#   def trace(self):
#   def custom(self, factory):
#   def __call__(self, name):
#   def decorate(self, name_or_func):
#     def decorator(name, func):
#       def _stopwatch(*args, **kwargs):
#   def push(self, name):
#   def pop(self):
#   def cur_stack(self):
#   def clear(self):
#   def add(self, name, duration):
#   def __getitem__(self, name):
#   def times(self):
#   def merge(self, other):
#   def parse(s):
#   def str(self, threshold=0.1):
#   def __str__(self):


import os
import mock
from absl.testing import absltest
from future.builtins import range  # pylint: disable=redefined-builtin
from pysc2.lib import stopwatch
    # The names should be in sorted order.
    names = [l.split(None)[0] for l in out.splitlines()[1:]]
    self.assertEqual(names, ["five", "four", "one", "two", "two.three"])

    one_line = out.splitlines()[3].split(None)
    self.assertLess(one_line[5], one_line[6])  # min < max
    self.assertEqual(one_line[7], "2")  # num
    # Can't test the rest since they'll be flaky.

    # Allow a few small rounding errors for the round trip.
    round_trip = str(stopwatch.StopWatch.parse(out))
    self.assertLess(ham_dist(out, round_trip), 15,
                    "%s != %s" % (out, round_trip))

  def testDivideZero(self):
    sw = stopwatch.StopWatch()
    with sw("zero"):
      pass

    # Just make sure this doesn't have a divide by 0 for when the total is 0.
    self.assertIn("zero", str(sw))

  @mock.patch.dict(os.environ, {"SC2_NO_STOPWATCH": "1"})
  def testDecoratorDisabled(self):
    sw = stopwatch.StopWatch()
    self.assertEqual(round, sw.decorate(round))
    self.assertEqual(round, sw.decorate("name")(round))

  @mock.patch.dict(os.environ, {"SC2_NO_STOPWATCH": ""})
  def testDecoratorEnabled(self):

# Path: pysc2/lib/stopwatch.py
# class Stat(object):
# class StopWatchContext(object):
# class TracingStopWatchContext(StopWatchContext):
# class FakeStopWatchContext(object):
# class StopWatch(object):
#   def __init__(self):
#   def reset(self):
#   def add(self, val):
#   def avg(self):
#   def dev(self):
#   def merge(self, other):
#   def build(summation, average, standard_deviation, minimum, maximum, number):
#   def parse(s):
#   def __str__(self):
#   def __init__(self, stopwatch, name):
#   def __enter__(self):
#   def __exit__(self, unused_exception_type, unused_exc_value, unused_traceback):
#   def __enter__(self):
#   def __exit__(self, *args, **kwargs):
#   def _log(self, s):
#   def __enter__(self):
#   def __exit__(self, unused_exception_type, unused_exc_value, unused_traceback):
#   def __init__(self, enabled=True, trace=False):
#   def disable(self):
#   def enable(self):
#   def trace(self):
#   def custom(self, factory):
#   def __call__(self, name):
#   def decorate(self, name_or_func):
#     def decorator(name, func):
#       def _stopwatch(*args, **kwargs):
#   def push(self, name):
#   def pop(self):
#   def cur_stack(self):
#   def clear(self):
#   def add(self, name, duration):
#   def __getitem__(self, name):
#   def times(self):
#   def merge(self, other):
#   def parse(s):
#   def str(self, threshold=0.1):
#   def __str__(self):


# Path: pysc2/lib/stopwatch_test.py
import os
import mock
from absl.testing import absltest
from future.builtins import range  # pylint: disable=redefined-builtin
from pysc2.lib import stopwatch

    # The names should be in sorted order.
    names = [l.split(None)[0] for l in out.splitlines()[1:]]
    self.assertEqual(names, ["five", "four", "one", "two", "two.three"])

    one_line = out.splitlines()[3].split(None)
    self.assertLess(one_line[5], one_line[6])  # min < max
    self.assertEqual(one_line[7], "2")  # num
    # Can't test the rest since they'll be flaky.

    # Allow a few small rounding errors for the round trip.
    round_trip = str(stopwatch.StopWatch.parse(out))
    self.assertLess(ham_dist(out, round_trip), 15,
                    "%s != %s" % (out, round_trip))

  def testDivideZero(self):
    sw = stopwatch.StopWatch()
    with sw("zero"):
      pass

    # Just make sure this doesn't have a divide by 0 for when the total is 0.
    self.assertIn("zero", str(sw))

  @mock.patch.dict(os.environ, {"SC2_NO_STOPWATCH": "1"})
  def testDecoratorDisabled(self):
    sw = stopwatch.StopWatch()
    self.assertEqual(round, sw.decorate(round))
    self.assertEqual(round, sw.decorate("name")(round))

  @mock.patch.dict(os.environ, {"SC2_NO_STOPWATCH": ""})
  def testDecoratorEnabled(self):

    sw = stopwatch.StopWatch()