示例#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)
示例#2
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()
示例#3
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))
示例#4
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)
示例#5
0
def try_plot():
    try:
        parser = argparse.ArgumentParser()
        parser.add_argument("-f",
                            "--files",
                            nargs='*',
                            help="File input",
                            type=str)
        parser.add_argument("-d",
                            "--display",
                            required=False,
                            action='store_true',
                            help="Display plot",
                            dest='display_plot')
        parser.add_argument("-t",
                            "--titles",
                            nargs='*',
                            help="Plot titles",
                            type=str)
        parser.add_argument('-c',
                            "--combine",
                            required=False,
                            action='store_true',
                            help="Combined data into a single plot")
        parser.add_argument('-o',
                            '--output-dir',
                            help="Output directory",
                            type=str,
                            required=False)
        parser.add_argument('-e',
                            '--echo-dart',
                            help="echo Dart measurement for CDash",
                            required=False,
                            action='store_true')
        parser.add_argument(
            '--min-percent',
            required=False,
            type=float,
            help="Exclude plotting below this percentage of maximum")
        parser.add_argument('--img-dpi',
                            help="Image dots per sq inch",
                            required=False,
                            type=int)
        parser.add_argument('--img-size',
                            help="Image dimensions",
                            nargs=2,
                            required=False,
                            type=int)
        parser.add_argument('--img-type',
                            help="Image type",
                            required=False,
                            type=str)
        parser.add_argument(
            '--plot-max',
            help="Plot the maximums from a set of inputs to <filename>",
            required=False,
            type=str,
            dest='plot_max')
        parser.add_argument('--log-x',
                            help="Plot X-axis in a log scale",
                            action='store_true')
        parser.add_argument('--font-size',
                            help="Font size of y-axis labels",
                            type=int)

        parser.set_defaults(display_plot=False)
        parser.set_defaults(combine=False)
        parser.set_defaults(output_dir=".")
        parser.set_defaults(echo_dart=False)
        parser.set_defaults(min_percent=plot_parameters.min_percent)
        parser.set_defaults(img_dpi=plot_parameters.img_dpi)
        parser.set_defaults(img_size=[
            plot_parameters.img_size['w'], plot_parameters.img_size['h']
        ])
        parser.set_defaults(img_type=plot_parameters.img_type)
        parser.set_defaults(plot_max="")
        parser.set_defaults(font_size=plot_parameters.font_size)

        args = parser.parse_args()

        do_plot_max = True if len(args.plot_max) > 0 else False

        # print('Files: {}'.format(args.files))
        # print('Titles: {}'.format(args.titles))

        params = _plotting.plot_parameters(min_percent=args.min_percent,
                                           img_dpi=args.img_dpi,
                                           img_size={
                                               'w': args.img_size[0],
                                               'h': args.img_size[1]
                                           },
                                           img_type=args.img_type,
                                           log_xaxis=args.log_x,
                                           font_size=args.font_size)

        if do_plot_max:
            if len(args.titles) != 1:
                raise Exception("Error must provide one title")
        else:
            if len(args.titles) != 1 and len(args.titles) != len(args.files):
                raise Exception(
                    "Error must provide one title or a title for each file")

        data = {}
        for i in range(len(args.files)):
            f = open(args.files[i], "r")
            _jdata = json.load(f)
            _ranks = _jdata["timemory"]["ranks"]

            nranks = len(_ranks)
            for j in range(nranks):
                _json = _ranks[j]
                _data = _plotting.read(_json)
                _rtag = '' if nranks == 1 else '_{}'.format(j)
                _rtitle = '' if nranks == 1 else ' (MPI rank: {})'.format(j)

                _data.filename = args.files[i].replace('.json', _rtag)
                if len(args.titles) == 1:
                    _data.title = args.titles[0] + _rtitle
                else:
                    _data.title = args.titles[i] + _rtitle
                _data.plot_params = params
                _data.mpi_size = nranks
                # print('### --> Processing "{}" from "{}"...'.format(_data.title,
                #                                                    args.files[i]))
                if not j in data.keys():
                    data[j] = [_data]
                else:
                    data[j] += [_data]

        _pargs = {
            'plot_params': params,
            'display': args.display_plot,
            'output_dir': args.output_dir,
            'echo_dart': args.echo_dart
        }

        if do_plot_max:
            _pargs['combine'] = args.combine

        for _rank, _data in data.items():
            if do_plot_max:
                _plotting.plot_maximums(args.plot_max, args.titles[0], _data,
                                        **_pargs)
            else:
                _plotting.plot(data=_data, **_pargs)

    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))
        sys.exit(1)

    # print('Done - {}'.format(sys.argv[0]))
    sys.exit(0)
示例#6
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')
示例#7
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)