def run(pattern="", exit_at_failure=True): """ Run the TiMemory unit tests Args: pattern (str): a regex string for matching the name of the tests to run an empty string implies run all tests """ import timemory manager = timemory.manager() test_names = ['timemory', 'array', 'nested', 'simple'] names = [] try: import re pat = re.compile(pattern) for t in test_names: mat = pat.match(t) if mat is not None: names.append(t) except: names = test_names if len(names) == 0: print('Warning! Pattern "{}" had no matches, running full suite: {}'. format(pattern, test_names)) names = test_names print('\n--> TiMemory has MPI support: {}\n'.format( timemory.has_mpi_support())) print('\n--> Running tests...\n') _fail = 0 _call = 0 _tot = timemory.timer('Total test time') _tot.start() for i in names: manager.clear() _f = '{}_test'.format(i) _file = os.path.join(__this_path, '{}.py'.format(_f)) t = timemory.timer('{}'.format(_f.upper())) t.start() print('\n--> [ Running {} ] ...\n'.format(_f)) try: _call += 1 sp.check_call([sys.executable, _file], cwd=os.getcwd()) except: _fail += 1 print('\n>>>>>>> Test [ {} ] failed! <<<<<<<\n'.format(_f)) t.stop() print('\n\n>>>>>>> {} <<<<<<<\n\n'.format(t)) print('\n\n============== PASSING TESTS: {}/{} =============='.format( _call - _fail, _call)) print('============== FAILING TESTS: {}/{} ==============\n\n'.format( _fail, _call)) _tot.stop() print('\n\n>>>>>>> {} <<<<<<<\n\n'.format(_tot)) if _fail > 0 and exit_at_failure: sys.exit(_fail)
def time_fibonacci(n): atimer = timemory.auto_timer('({})@{}'.format( n, timemory.FILE(use_dirname=True))) key = ('fibonacci(%i)' % n) timer = timemory.timer(key) timer.start() fibonacci(n) timer.stop()
def run_test(): timemory.enable_signal_detection([ signals.sys_signal.Hangup, signals.sys_signal.Interrupt, signals.sys_signal.FPE, signals.sys_signal.Abort ]) array_size = 8000000 t = timemory.timer("Total time") t.start() print('') parser = argparse.ArgumentParser() parser.add_argument("-n", "--nfib", help="Number of fibonacci calculations", default=15, type=int) parser.add_argument("-s", "--size", help="Size of array allocations", default=array_size, type=int) args = options.add_arguments_and_parse(parser) array_size = args.size timemory.settings.output_path = "test_output" timemory.settings.output_prefix = "nested_report" rss = timemory.rss_usage() rss.record() try: main(args.nfib) #print ('Timing manager size: {}'.format(timemory.size())) #tman = timemory.manager() #_jsonf = os.path.join(options.output_dir, 'nested_output.json') #_fname = tman.serialize(_jsonf) #_data = timemory.plotting.read(tman.json()) #_data.title = timemory.FILE(noquotes=True) #_data.filename = _fname #plotting.plot(data = [_data], files = [_jsonf], output_dir=options.output_dir) except Exception as e: exc_type, exc_value, exc_traceback = sys.exc_info() traceback.print_exception(exc_type, exc_value, exc_traceback, limit=5) print('Exception - {}'.format(e)) t.stop() print("RSS usage at initialization: {}".format(rss)) print("{}\n".format( timemory.rss_usage(record=True, prefix="RSS usage at finalization"))) timemory.disable_signal_detection() print('"{}" testing finished'.format(__file__))
def test_2_timing(self): print('\n\n--> Testing function: "{}"...\n\n'.format(timemory.FUNC())) self.manager.clear() freport = options.set_report("timing_report.out") fserial = options.set_serial("timing_report.json") def time_fibonacci(n): atimer = timemory.auto_timer('({})@{}'.format( n, timemory.FILE(use_dirname=True))) key = ('fibonacci(%i)' % n) print('key = {}'.format(key)) timer = timemory.timer(key) timer.start() fibonacci(n) timer.stop() self.manager.clear() t = timemory.timer("tmanager_test") t.start() for i in [20, 25, 30]: # python is too slow with these values that run in a couple # seconds in C/C++ time_fibonacci(i - 3) time_fibonacci(i - 2) time_fibonacci(i - 1) time_fibonacci(i + 0) self.manager.merge() self.manager.report() plotting.plot(files=[fserial], output_dir=self.output_dir) print("{}".format(self.manager)) self.assertEqual(self.manager.size(), 13) #for i in range(0, self.manager.size()): # _t = self.manager.at(i) # self.assertFalse(_t.real_elapsed() < 0.0) # self.assertFalse(_t.user_elapsed() < 0.0) timemory.toggle(True) t.stop() print('{}'.format(t))
def __enter__(self, *args, **kwargs): """ Context manager """ import timemory _file = timemory.FILE(3) _line = timemory.LINE(2) _func = timemory.FUNC(2) self.determine_signature(is_decorator=False, is_context_manager=True) _key = '' _args = self.arg_string(args, kwargs) if self.signature == context.blank: _key = '{}{}'.format(self.key, _args) elif self.signature == context.basic: _key = '{}/{}/{}'.format(_func, self.key, _args) elif self.signature == context.full: _key = '{}/{}:{}/{}{}'.format( _func, _file, _line, self.key, _args) _key = _key.strip('/') self._self_obj = timemory.timer(_key) self._self_obj.start()
def function_wrapper(*args, **kwargs): self.parse_wrapped(func, args, kwargs) self.determine_signature(is_decorator=True, is_context_manager=False) _func = func.__name__ _key = '' _args = self.arg_string(args, kwargs) if self.signature == context.blank: _key = '{}{}'.format(self.key, _args) elif self.signature == context.basic: _key = '{}/{}/{}'.format(_func, self.key, _args) elif self.signature == context.full: _key = '{}/{}:{}/{}{}'.format( _func, _file, _line, self.key, _args) _key = _key.strip('/') t = timemory.timer(_key) t.start() ret = func(*args, **kwargs) t.stop() t.report() return ret
def measure(name, _time = 1): t = timemory.timer(name) t.start() time.sleep(_time) t.stop() print('\n{}\n'.format(t))
def test_8_format(self): print('\n\n--> Testing function: "{}"...\n\n'.format(timemory.FUNC())) self.manager.clear() t1 = timemory.timer("format_test") t2 = timemory.timer("format_test") u1 = timemory.rss_usage("format_test") u2 = timemory.rss_usage("format_test") # Python 2.7 doesn't like timemory.format.{timer,rss} without instance if sys.version_info[0] > 2: timer_rss_fmt = timemory.format.rss() timer_rss_fmt.set_format("%C, %M, %c, %m") timer_rss_fmt.set_precision(0) timer_rss_fmt.set_unit(timemory.units.kilobyte) default_timer_fmt = timemory.format.timer.get_default() timemory.format.timer.set_default_format( "[%T - %A] : %w, %u, %s, %t, %p%, x%l, %R") timemory.format.timer.set_default_rss_format("%C, %M, %c, %m") timemory.format.timer.set_default_rss_format(timer_rss_fmt) timemory.format.timer.set_default_unit(timemory.units.msec) timemory.format.timer.set_default_precision(1) default_rss_fmt = timemory.format.rss.get_default() timemory.format.rss.set_default_format("[ c, p %A ] : %C, %M") timemory.format.rss.set_default_unit(timemory.units.kilobyte) timemory.format.rss.set_default_precision(3) t1 = timemory.timer("format_test") t2 = timemory.timer("format_test") u1 = timemory.rss_usage("format_test") u2 = timemory.rss_usage("format_test") t2.set_format(t2.get_format().copy_from(default_timer_fmt)) u2.set_format(u2.get_format().copy_from(default_rss_fmt)) else: timer_rss_fmt = timemory.format.rss() timer_rss_fmt.set_format("%C, %M, %c, %m") timer_rss_fmt.set_precision(0) timer_rss_fmt.set_unit(timemory.units.kilobyte) timer_format = timemory.format.timer() default_timer_fmt = timer_format.get_default() timer_format.set_default_format( "[%T - %A] : %w, %u, %s, %t, %p%, x%l, %R") timer_format.set_default_rss_format("%C, %M, %c, %m") timer_format.set_default_rss_format(timer_rss_fmt) timer_format.set_default_unit(timemory.units.msec) timer_format.set_default_precision(1) rss_format = timemory.format.rss() default_rss_fmt = rss_format.get_default() rss_format.set_default_format("[ c, p %A ] : %C, %M") rss_format.set_default_unit(timemory.units.kilobyte) rss_format.set_default_precision(3) t1 = timemory.timer("format_test") t2 = timemory.timer("format_test") u1 = timemory.rss_usage("format_test") u2 = timemory.rss_usage("format_test") t2.set_format(t2.get_format().copy_from(default_timer_fmt)) u2.set_format(u2.get_format().copy_from(default_rss_fmt)) freport = options.set_report("timing_format.out") fserial = options.set_serial("timing_format.json") def time_fibonacci(n): atimer = timemory.auto_timer('({})@{}'.format( n, timemory.FILE(use_dirname=True))) key = ('fibonacci(%i)' % n) timer = timemory.timer(key) timer.start() fibonacci(n) timer.stop() self.manager.clear() t1.start() t2.start() for i in [39, 35, 39]: # python is too slow with these values that run in a couple # seconds in C/C++ n = i - 12 time_fibonacci(n - 2) time_fibonacci(n - 1) time_fibonacci(n) time_fibonacci(n + 1) self.manager.report() plotting.plot(files=[fserial], output_dir=self.output_dir) self.assertEqual(self.manager.size(), 9) #for i in range(0, self.manager.size()): # _t = self.manager.at(i) # self.assertFalse(_t.real_elapsed() < 0.0) # self.assertFalse(_t.user_elapsed() < 0.0) timemory.toggle(True) self.manager.clear() u1.record() u2.record() print('\n') print('[memory] {}'.format(u1)) print('[memory] {}'.format(u2)) t1.stop() t2.stop() print('\n') print('[timing] {}'.format(t1)) print('[timing] {}'.format(t2)) print('\n')