Example #1
0
    def test_6_toggle(self):
        print('\n\n--> Testing function: "{}"...\n\n'.format(timemory.FUNC()))

        self.manager.clear()

        timemory.toggle(True)
        if True:
            autotimer = timemory.auto_timer("on")
            fibonacci(27)
            del autotimer
        self.assertEqual(self.manager.size(), 2)

        timemory.toggle(False)
        if True:
            autotimer = timemory.auto_timer("off")
            fibonacci(27)
            del autotimer
        self.assertEqual(self.manager.size(), 2)

        timemory.toggle(True)
        if True:
            autotimer_on = timemory.auto_timer("on")
            timemory.toggle(False)
            autotimer_off = timemory.auto_timer("off")
            fibonacci(27)
            del autotimer_off
            del autotimer_on
        self.assertEqual(self.manager.size(), 3)

        freport = timemory.options.set_report("timing_toggle.out")
        fserial = timemory.options.set_serial("timing_toggle.json")
        self.manager.report(ign_cutoff=True)
        plotting.plot(files=[fserial], output_dir=self.output_dir)
Example #2
0
        def auto_timer_with_args(nc=2):
            # construct context-manager with args
            with timemory.util.auto_timer('{}({})'.format(
                    timemory.FUNC(), ['test', 'context'])):
                time.sleep(nc)

                with timemory.util.auto_timer('{}({})'.format(
                        timemory.FUNC(), ['test', 'context']),
                                              report_at_exit=True):
                    ret = np.ones(shape=[500, 500], dtype=np.float64)
                    for i in [2.0, 3.5, 8.7]:
                        n = i * np.ones(shape=[500, 500], dtype=np.float64)
                        ret += n
                        del n
                # recursive
                if nc > 1:
                    auto_timer_with_args(nc=1)
Example #3
0
    def test_3_decorator(self):
        print('\n\n--> Testing function: "{}"...\n\n'.format(timemory.FUNC()))

        timemory.toggle(True)
        self.manager.clear()

        @auto_timer()
        def test_func_glob():
            time.sleep(1)

            @auto_timer()
            def test_func_1():
                ret = np.ones(shape=[2500, 2500], dtype=np.float64)
                time.sleep(1)

            @auto_timer()
            def test_func_2(n):
                test_func_1()
                time.sleep(n)

            test_func_1()
            test_func_2(2)

        test_func_glob()

        freport = timemory.options.set_report("timing_decorator.out")
        fserial = timemory.options.set_serial("timing_decorator.json")
        self.manager.report(ign_cutoff=True)
        plotting.plot(files=[fserial], output_dir=self.output_dir)

        self.assertEqual(timemory.size(), 5)

        @timer()
        def test_func_timer():
            time.sleep(1)

            @rss_usage()
            def test_func_rss():
                ret = np.ones(shape=[5000, 5000], dtype=np.float64)
                return None

            print('')
            ret = test_func_rss()
            print('')
            time.sleep(1)
            return None

        test_func_timer()
Example #4
0
def main(nfib):
    timemory.set_max_depth(10)
    print('')
    print('main: file() {}'.format(timemory.FILE()))
    print('main: line() {}'.format(timemory.LINE()))
    print('main: line() {}'.format(timemory.LINE()))
    print('main: func() {}'.format(timemory.FUNC()))
    test()
    print('creating manager...')
    tman = timemory.manager()
    print('creating Fibonacci object...')
    fib = Fibonacci(int(nfib))
    print('calculating fibonacci...')
    ret = fib.calculate()
    print('completed...')

    timemory.report()
Example #5
0
    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))
Example #6
0
    def test_1_rss_validity(self):

        import numpy as np

        print('\n\n--> Testing function: "{}"...\n\n'.format(timemory.FUNC()))

        rss_init = timemory.rss_usage(record=True)
        rss_post = timemory.rss_usage(record=False)

        print('\t(A) RSS post: {}'.format(rss_post))
        print('\t(A) RSS init: {}'.format(rss_init))

        # should be 8 MB
        nsize = 1048576
        arr1 = np.ones(shape=[nsize], dtype=np.uint64, order='C')

        #for i in range(nsize):
        #    arr1[i] = i

        rss_post.record()

        print('\t(B) RSS post: {}'.format(rss_post))
        print('\t(B) RSS init: {}'.format(rss_init))

        rss_post -= rss_init

        print('\t(C) RSS post: {}'.format(rss_post))
        print('\t(C) RSS init: {}'.format(rss_init))

        # in kB
        rss_corr = 8192
        # real memory in kilobytes
        rss_real = rss_post.current(timemory.units.kilobyte)

        # compute diff
        rss_diff = rss_real - rss_corr
        print('\tRSS real:  {} kB'.format(rss_real))
        print('\tRSS ideal: {} kB'.format(rss_corr))
        print('\tRSS diff:  {} kB'.format(rss_diff))

        # allow some variability
        self.assertTrue(abs(rss_diff) < 300)
Example #7
0
    def test_5_pointer(self):
        print('\n\n--> Testing function: "{}"...\n\n'.format(timemory.FUNC()))

        nval = 4

        def set_pointer_max(nmax):
            self.manager.set_max_depth(4)
            return self.manager.get_max_depth()

        def get_pointer_max():
            return timemory.manager().get_max_depth()

        ndef = get_pointer_max()
        nnew = set_pointer_max(nval)
        nchk = get_pointer_max()

        self.assertEqual(nval, nchk)

        set_pointer_max(ndef)

        self.assertEqual(ndef, get_pointer_max())
Example #8
0
    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.component_decorator(self.components, _key)
Example #9
0
    def test_4_max_depth(self):
        print('\n\n--> Testing function: "{}"...\n\n'.format(timemory.FUNC()))

        timemory.toggle(True)
        self.manager.clear()

        def create_timer(n):
            autotimer = timemory.auto_timer('{}'.format(n))
            fibonacci(30)
            if n < 8:
                create_timer(n + 1)

        ntimers = 4
        timemory.set_max_depth(ntimers)

        create_timer(0)

        freport = timemory.options.set_report("timing_depth.out")
        fserial = timemory.options.set_serial("timing_depth.json")
        self.manager.report()
        plotting.plot(files=[fserial], output_dir=self.output_dir)

        self.assertEqual(self.manager.size(), ntimers)
Example #10
0
def test():
    print('test: func() {}'.format(timemory.FUNC()))
    print('test: func(2) {}'.format(timemory.FUNC(2)))
Example #11
0
    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')
Example #12
0
    def test_7_context_manager(self):
        print('\n\n--> Testing function: "{}"...\n\n'.format(timemory.FUNC()))

        timemory.toggle(True)
        self.manager.clear()

        #----------------------------------------------------------------------#
        # timer test
        with timemory.util.timer():
            time.sleep(1)

            ret = np.ones(shape=[500, 500], dtype=np.float64)
            for i in [2.0, 3.5, 8.7]:
                n = i * np.ones(shape=[500, 500], dtype=np.float64)
                ret += n
                del n

        #----------------------------------------------------------------------#
        # timer test with args
        with timemory.util.timer('{}({})'.format(timemory.FUNC(),
                                                 ['test', 'context'])):
            time.sleep(1)

            ret = np.ones(shape=[500, 500], dtype=np.float64)
            for i in [2.0, 3.5, 8.7]:
                n = i * np.ones(shape=[500, 500], dtype=np.float64)
                ret += n
                del n

        #----------------------------------------------------------------------#
        # auto timer test
        with timemory.util.auto_timer(report_at_exit=True):
            time.sleep(1)

            ret = np.ones(shape=[500, 500], dtype=np.float64)
            for i in [2.0, 3.5, 8.7]:
                n = i * np.ones(shape=[500, 500], dtype=np.float64)
                ret += n
                del n

        #----------------------------------------------------------------------#
        # failure test
        def fail_func():
            return [0, 1, 2, 3]

        # timer exit test
        try:
            with timemory.util.timer():
                a, b, c = fail_func()
        except Exception as e:
            pass

        # auto timer exit test
        try:
            with timemory.util.auto_timer():
                a, b, c = fail_func()
        except Exception as e:
            pass

        # rss exit test
        try:
            with timemory.util.rss_usage():
                a, b, c = fail_func()
        except Exception as e:
            pass

        #----------------------------------------------------------------------#
        # auto timer test with args
        @timemory.util.auto_timer()
        def auto_timer_with_args(nc=2):
            # construct context-manager with args
            with timemory.util.auto_timer('{}({})'.format(
                    timemory.FUNC(), ['test', 'context'])):
                time.sleep(nc)

                with timemory.util.auto_timer('{}({})'.format(
                        timemory.FUNC(), ['test', 'context']),
                                              report_at_exit=True):
                    ret = np.ones(shape=[500, 500], dtype=np.float64)
                    for i in [2.0, 3.5, 8.7]:
                        n = i * np.ones(shape=[500, 500], dtype=np.float64)
                        ret += n
                        del n
                # recursive
                if nc > 1:
                    auto_timer_with_args(nc=1)

        auto_timer_with_args()

        print('\n\n{}\n\n'.format(self.manager))

        #----------------------------------------------------------------------#
        # rss test
        with timemory.util.rss_usage():
            time.sleep(1)

            ret = np.ones(shape=[500, 500], dtype=np.float64)
            for i in [2.0, 3.5, 8.7]:
                n = i * np.ones(shape=[500, 500], dtype=np.float64)
                ret += n
                del n

        #----------------------------------------------------------------------#
        # rss test with args
        with timemory.util.rss_usage('{}({})'.format(timemory.FUNC(),
                                                     ['test', 'context'])):
            time.sleep(1)

            ret = np.ones(shape=[500, 500], dtype=np.float64)
            for i in [2.0, 3.5, 8.7]:
                n = i * np.ones(shape=[500, 500], dtype=np.float64)
                ret += n
                del n

        print("\nNormal report\n")
        freport = timemory.options.set_report(
            "timing_context_manager_normal.out")
        fserial = timemory.options.set_serial(
            "timing_context_manager_normal.json")

        # self.assertFalse(self.manager.get_self_cost())
        self.manager.report(ign_cutoff=True)
        plotting.plot(files=[fserial], output_dir=self.output_dir)

        # print("\nSelf report\n")
        # freport = timemory.options.set_report("timing_context_manager_self.out")
        # fserial = timemory.options.set_serial("timing_context_manager_self.json")

        # self.manager.set_self_cost(True)
        self.assertTrue(self.manager.get_self_cost())
        self.manager.report(ign_cutoff=True)
        plotting.plot(files=[fserial], output_dir=self.output_dir)