def testResultsDoesntReset(self): sw = stopwatch.StopWatch() sw.start() self.time.sleep(1) sw.start('a') self.time.sleep(1) sw.stop('a') sw.stop() res1 = sw.results(verbose=True) res2 = sw.results(verbose=True) self.assertListEqual(res1, res2)
def testTimerValue(self): sw = stopwatch.StopWatch() self.assertAlmostEqual(0, sw.timervalue('a'), 2) sw.start('a') self.assertAlmostEqual(0, sw.timervalue('a'), 2) self.time.sleep(1) self.assertAlmostEqual(1, sw.timervalue('a'), 2) sw.stop('a') self.assertAlmostEqual(1, sw.timervalue('a'), 2) sw.start('a') self.time.sleep(1) self.assertAlmostEqual(2, sw.timervalue('a'), 2) sw.stop('a') self.assertAlmostEqual(2, sw.timervalue('a'), 2)
def testSeveralTimes(self): sw = stopwatch.StopWatch() sw.start() sw.start('a') sw.start('b') self.time.sleep(1) sw.stop('b') sw.stop('a') sw.stop() results = sw.results(verbose=1) self.assertListEqual([r[0] for r in results], ['a', 'b', 'overhead', 'total']) # Make sure overhead is positive self.assertEqual(results[2][1] > 0, 1)
def testNoStopOthers(self): sw = stopwatch.StopWatch() sw.start() sw.start('a') sw.start('b', stop_others=0) self.time.sleep(1) sw.stop('b') sw.stop('a') sw.stop() #overhead should be negative, because we ran two timers simultaneously #It is possible that this could fail in outlandish circumstances. #If this is a problem in practice, increase the value of the call to #time.sleep until it passes consistently. #Or, consider finding a platform where the two calls sw.start() and #sw.start('a') happen within 1 second. results = sw.results(verbose=1) self.assertEqual(results[2][1] < 0, 1)
def testResults(self): sw = stopwatch.StopWatch() sw.start() sw.stop() results = sw.results() self.assertListEqual([r[0] for r in results], ['total']) results = sw.results(verbose=1) self.assertListEqual([r[0] for r in results], ['overhead', 'total']) # test tally part of results. sw.start('ron') sw.stop('ron') sw.start('ron') sw.stop('ron') results = sw.results() results = sw.results(verbose=1) for r in results: if r[0] == 'ron': assert r[2] == 2
def testResultsDoesntCrashWhenUnstopped(self): sw = stopwatch.StopWatch() sw.start() sw.results()
def testStopNonExistentTimer(self): sw = stopwatch.StopWatch() self.assertRaises(RuntimeError, sw.stop) self.assertRaises(RuntimeError, sw.stop, 'foo')