def test_time_offset(): time_before_offset = qcore.utime() with TimeOffset(qcore.HOUR): time_during_offset = qcore.utime() time_after_offset = qcore.utime() assert_eq(time_before_offset, time_after_offset, tolerance=qcore.MINUTE) assert_ne(time_before_offset, time_during_offset, tolerance=qcore.MINUTE) assert_le(time_after_offset, time_during_offset)
def test_collect_perf_stats(): sleep_time = 50000 # 50ms @asynq() def func(depth): if depth == 0: result((yield ExternalCacheBatchItem(mc._batch, 'get', 'test'))) return time.sleep(float(sleep_time) / SECOND) yield func.asynq(depth - 1), func.asynq(depth - 1) debug.options.COLLECT_PERF_STATS = True debug.options.KEEP_DEPENDENCIES = True profiler.reset() reset_caches() depth = 3 func(depth) profiled_result = profiler.flush() batches = 1 async_tasks = 2**(depth + 1) - 1 num_leaf_tasks = 2**depth assert_eq(async_tasks + batches, len(profiled_result)) deps_to_tasks = collections.defaultdict(list) for task in profiled_result: num_deps = len(task['dependencies']) deps_to_tasks[num_deps].append(task) assert_unordered_list_eq([1, 2, num_leaf_tasks], deps_to_tasks.keys()) # leaf tasks assert_eq(num_leaf_tasks, len(deps_to_tasks[1])) for task in deps_to_tasks[1]: assert_ge(sleep_time, task['time_taken']) # one batch (with all the batch items as dependencies) assert_eq(1, len(deps_to_tasks[num_leaf_tasks])) # non-leaf async tasks in the tree (1 + 2 + 3 = 7) assert_eq(7, len(deps_to_tasks[2])) for task in deps_to_tasks[2]: assert_le(sleep_time, task['time_taken']) # When COLLET_PERF_STATS is False, we shouldn't profile anything. debug.options.COLLECT_PERF_STATS = False profiler.reset() reset_caches() func(2) profiled_result = profiler.flush() assert_eq(0, len(profiled_result))
def test_collect_perf_stats(): sleep_time = 50000 # 50ms @asynq() def func(depth): if depth == 0: result((yield ExternalCacheBatchItem(mc._batch, 'get', 'test'))) return time.sleep(float(sleep_time) / SECOND) yield func.asynq(depth - 1), func.asynq(depth - 1) debug.options.COLLECT_PERF_STATS = True profiler.reset() reset_caches() depth = 3 func(depth) profiled_result = profiler.flush() assert_eq(2**(depth + 1) - 1, len(profiled_result)) for stats in profiled_result: num_deps = stats['num_deps'] num_dependencies = len(stats['dependencies']) time_taken = stats['time_taken'] # It has one ExternalCacheBatchItem, and no dependent AsyncTasks. if num_deps == 1: assert_eq(0, num_dependencies) assert_ge(sleep_time, time_taken) # It has two dependent AsyncTasks, and is sleeping for sleep_time (50ms). elif num_deps == 2: assert_eq(2, num_dependencies) assert_le(sleep_time, time_taken) assert_ge(sleep_time * 2, time_taken) else: assert False # When COLLET_PERF_STATS is False, we shouldn't profile anything. debug.options.COLLECT_PERF_STATS = False profiler.reset() reset_caches() func(2) profiled_result = profiler.flush() assert_eq(0, len(profiled_result))
def test_assert_ordering(): # ints assert_gt(2, 1) with AssertRaises(AssertionError): assert_gt(1, 1) with AssertRaises(AssertionError): assert_gt(0, 1) assert_ge(2, 1) assert_ge(1, 1) with AssertRaises(AssertionError): assert_ge(0, 1) with AssertRaises(AssertionError): assert_lt(2, 1) with AssertRaises(AssertionError): assert_lt(1, 1) assert_lt(0, 1) with AssertRaises(AssertionError): assert_le(2, 1) assert_le(1, 1) assert_lt(0, 1) # floats (tolerance isn't supported) assert_gt(2.5, 1.5) with AssertRaises(AssertionError): assert_gt(1.5, 1.5) with AssertRaises(AssertionError): assert_gt(0.5, 1.5) assert_ge(2.5, 1.5) assert_ge(1.5, 1.5) with AssertRaises(AssertionError): assert_ge(0.5, 1.5) with AssertRaises(AssertionError): assert_lt(2.5, 1.5) with AssertRaises(AssertionError): assert_lt(1.5, 1.5) assert_lt(0.5, 1.5) with AssertRaises(AssertionError): assert_le(2.5, 1.5) assert_le(1.5, 1.5) assert_lt(0.5, 1.5) # strings assert_gt("c", "b") with AssertRaises(AssertionError): assert_gt("b", "b") with AssertRaises(AssertionError): assert_gt("a", "b") assert_ge("c", "b") assert_ge("b", "b") with AssertRaises(AssertionError): assert_ge("a", "b") with AssertRaises(AssertionError): assert_lt("c", "b") with AssertRaises(AssertionError): assert_lt("b", "b") assert_lt("a", "b") with AssertRaises(AssertionError): assert_le("c", "b") assert_le("b", "b") assert_lt("a", "b")