def test_sanity(self): lock1 = threading.RLock() lock2 = threading.RLock() def sleep_and_inc(lock,sleep_time): with synchronize_using(lock): time.sleep(sleep_time) lock.sync_test_counter = getattr(lock,'sync_test_counter',0) + 1 sleep_time = 0.5 n = 4 s = Stopwatch() lst_threads = [] for lock in [lock1,lock2]: for _ in xrange(n): t = threading.Thread(target=sleep_and_inc,args=[lock,sleep_time]) lst_threads.append(t) t.start() # wait for all threads, then check results for t in lst_threads: t.join() duration = s.duration() self.assertEqual(lock1.sync_test_counter,n) self.assertEqual(lock2.sync_test_counter,n) ideal_time = n*sleep_time self.assert_(ideal_time*0.9 < duration < ideal_time*1.1, "duration=%s, ideal=%s" % (duration,ideal_time))
def test_n_threads(self): sleep_time = 0.1 class Sleeper(object): def __init__(self): self._lock = threading.RLock() # used by @synchronized decorator self.i = 0 self.thread_ids = set() @synchronized def _critical_section(self): self.thread_ids.add(id(threading.currentThread())) self.i += 1 def sleep(self): time.sleep(sleep_time) self._critical_section() n_threads = 5 n_calls = 20 ao = AO.AO(Sleeper(), n_threads) ao.start() self.aos_to_stop.append(ao) s = Stopwatch() futures = [ao.sleep() for i in xrange(n_calls)] for f in futures: f.get() duration = s.duration() expected = sleep_time * n_calls / float(n_threads) self.assert_(0.9 * expected < duration < 1.2 * expected, "duration=%s, expected=%s" % (duration, expected)) self.assertEqual(ao.obj.i, n_calls) self.assertEqual(len(ao.obj.thread_ids), n_threads) self.failIf(id(threading.currentThread()) in ao.obj.thread_ids)
def test_no_sync(self): stopwatch = Stopwatch() threading.Thread(target=self.s.add,args=[10]).start() time.sleep(0.05) # make sure other thread gets head start val = self.s.add(5) self.assertEqual(val,0) duration = stopwatch.duration() self.assert_(duration < 1.2*self.s.sleep_time, 'calls took too long. duration=%s' % duration)
def test_normal(self): stopwatch = Stopwatch() threading.Thread(target=self.s.sync_add,args=[10]).start() time.sleep(0.05) # make sure other thread gets head start (and the mutex) val = self.s.sync_add(5) self.assertEqual(val,10) duration = stopwatch.duration() self.assert_(duration > 1.9*self.s.sleep_time, 'calls completed too quickly. duration=%s' % duration)
def test_timeout(self): # preset future - should return within timeout f = future.Future.preset(3) self.assertEqual(f.get(100),3) # unset future - should raise exception timeout = 1 # timeout in seconds f = future.Future() stopwatch = Stopwatch() stopwatch.start() self.assertRaises(future.FutureTimeoutException,f.get,timeout*1000) duration = stopwatch.duration() self.assert_(0.9*timeout < duration < 1.3*timeout, 'duration=%s' % duration)
def test_sanity(self): s = Stopwatch() time.sleep(0.5) duration = s.duration() self.assert_(0.45 < duration < 0.55, 'duration=%s' % duration)
from stopwatch import Stopwatch class firstn(object): def __init__(self, n): self.n = n self.num = 0 # self.num, self.nums = 0, [] def __iter__(self): return self def next(self): if self.num < self.n: cur, self.num = self.num, self.num+1 return cur else: raise StopIteration() s = Stopwatch() sum_of_first_n = sum(firstn(10000000)) duration = s.duration() print "sum:",sum_of_first_n print "duration:",duration