
from __future__ import print_function
import time
import unittest

from autosklearn.util import StopWatch


class Test(unittest.TestCase):
    _multiprocess_can_split_ = True

    def test_stopwatch_overhead(self):
        # CPU overhead
        start = time.clock()
        watch = StopWatch()
        for i in range(1, 1000):
            watch.start_task('task_%d' % i)
            watch.stop_task('task_%d' % i)
        stop = time.clock()
        dur = stop - start
        cpu_overhead = dur - watch.cpu_sum()
        self.assertLess(cpu_overhead, 1.5)

        # Wall Overhead
        start = time.time()
        watch = StopWatch()
        for i in range(1, 1000):
            watch.start_task('task_%d' % i)
            watch.stop_task('task_%d' % i)
        stop = time.time()
        dur = stop - start
        wall_overhead = dur - watch.wall_sum()

        self.assertLess(wall_overhead, 2)
        self.assertLess(cpu_overhead, 2*wall_overhead)


if __name__ == '__main__':
    # import sys;sys.argv = ['', 'Test.testName']
    unittest.main()


from __future__ import print_function
import time
import unittest
import unittest.mock

from autosklearn.util import StopWatch


class Test(unittest.TestCase):
    _multiprocess_can_split_ = True

    def test_stopwatch_overhead(self):

        # Wall Overhead
        start = time.time()
        cpu_start = time.clock()
        watch = StopWatch()
        for i in range(1, 1000):
            watch.start_task('task_%d' % i)
            watch.stop_task('task_%d' % i)
        cpu_stop = time.clock()
        stop = time.time()
        dur = stop - start
        cpu_dur = cpu_stop - cpu_start
        cpu_overhead = cpu_dur - watch.cpu_sum()
        wall_overhead = dur - watch.wall_sum()

        self.assertLess(cpu_overhead, 1)
        self.assertLess(wall_overhead, 1)
        self.assertLess(watch.cpu_sum(), 2 * watch.wall_sum())


if __name__ == '__main__':
    # import sys;sys.argv = ['', 'Test.testName']
    unittest.main()

TEST potentially fix brittle stopwatch test
TEST potentially fix brittle stopwatch test

  
  from __future__ import print_function
  import time
  import unittest
+ import unittest.mock
  
  from autosklearn.util import StopWatch
  
  
  class Test(unittest.TestCase):
      _multiprocess_can_split_ = True
  
      def test_stopwatch_overhead(self):
+ 
-         # CPU overhead
+         # Wall Overhead
+         start = time.time()
-         start = time.clock()
+         cpu_start = time.clock()
          watch = StopWatch()
          for i in range(1, 1000):
              watch.start_task('task_%d' % i)
              watch.stop_task('task_%d' % i)
-         stop = time.clock()
+         cpu_stop = time.clock()
-         dur = stop - start
-         cpu_overhead = dur - watch.cpu_sum()
-         self.assertLess(cpu_overhead, 1.5)
- 
-         # Wall Overhead
-         start = time.time()
-         watch = StopWatch()
-         for i in range(1, 1000):
-             watch.start_task('task_%d' % i)
-             watch.stop_task('task_%d' % i)
          stop = time.time()
          dur = stop - start
+         cpu_dur = cpu_stop - cpu_start
+         cpu_overhead = cpu_dur - watch.cpu_sum()
          wall_overhead = dur - watch.wall_sum()
  
+         self.assertLess(cpu_overhead, 1)
-         self.assertLess(wall_overhead, 2)
+         self.assertLess(wall_overhead, 1)
-         self.assertLess(cpu_overhead, 2*wall_overhead)
+         self.assertLess(watch.cpu_sum(), 2 * watch.wall_sum())
  
  
  if __name__ == '__main__':
      # import sys;sys.argv = ['', 'Test.testName']
      unittest.main()
  
TEST potentially fix brittle stopwatch test
## Code Before:

from __future__ import print_function
import time
import unittest

from autosklearn.util import StopWatch


class Test(unittest.TestCase):
    _multiprocess_can_split_ = True

    def test_stopwatch_overhead(self):
        # CPU overhead
        start = time.clock()
        watch = StopWatch()
        for i in range(1, 1000):
            watch.start_task('task_%d' % i)
            watch.stop_task('task_%d' % i)
        stop = time.clock()
        dur = stop - start
        cpu_overhead = dur - watch.cpu_sum()
        self.assertLess(cpu_overhead, 1.5)

        # Wall Overhead
        start = time.time()
        watch = StopWatch()
        for i in range(1, 1000):
            watch.start_task('task_%d' % i)
            watch.stop_task('task_%d' % i)
        stop = time.time()
        dur = stop - start
        wall_overhead = dur - watch.wall_sum()

        self.assertLess(wall_overhead, 2)
        self.assertLess(cpu_overhead, 2*wall_overhead)


if __name__ == '__main__':
    # import sys;sys.argv = ['', 'Test.testName']
    unittest.main()

## Instruction:
TEST potentially fix brittle stopwatch test
## Code After:

from __future__ import print_function
import time
import unittest
import unittest.mock

from autosklearn.util import StopWatch


class Test(unittest.TestCase):
    _multiprocess_can_split_ = True

    def test_stopwatch_overhead(self):

        # Wall Overhead
        start = time.time()
        cpu_start = time.clock()
        watch = StopWatch()
        for i in range(1, 1000):
            watch.start_task('task_%d' % i)
            watch.stop_task('task_%d' % i)
        cpu_stop = time.clock()
        stop = time.time()
        dur = stop - start
        cpu_dur = cpu_stop - cpu_start
        cpu_overhead = cpu_dur - watch.cpu_sum()
        wall_overhead = dur - watch.wall_sum()

        self.assertLess(cpu_overhead, 1)
        self.assertLess(wall_overhead, 1)
        self.assertLess(watch.cpu_sum(), 2 * watch.wall_sum())


if __name__ == '__main__':
    # import sys;sys.argv = ['', 'Test.testName']
    unittest.main()
