Beispiel #1
0
    def do_one_function(self, func):
        # the original speed
        with Timer() as t:
            func()
            origin = t.get_time()

        # With viztracer + python tracer
        tracer = VizTracer("python", verbose=0)
        tracer.start()
        with Timer() as t:
            func()
            instrumented = t.get_time()
        tracer.stop()
        with Timer() as t:
            entries1 = tracer.parse()
            instrumented_parse = t.get_time()
        with Timer() as t:
            tracer.generate_json()
            instrumented_json = t.get_time()
        tracer.clear()

        # With viztracer + c tracer
        tracer = VizTracer("c", verbose=0, ignore_c_function=True)
        tracer.start()
        with Timer() as t:
            func()
            instrumented_c = t.get_time()
        tracer.stop()
        with Timer() as t:
            entries2 = tracer.parse()
            instrumented_c_parse = t.get_time()
        with Timer() as t:
            tracer.generate_json()
            instrumented_c_json = t.get_time()
        tracer.clear()

        # With cProfiler
        pr = cProfile.Profile()
        pr.enable()
        with Timer() as t:
            func()
            cprofile = t.get_time()
        pr.disable()

        def time_str(name, origin, instrumented):
            return "{:.9f}({:.2f})[{}] ".format(instrumented,
                                                instrumented / origin, name)

        print("{:10}({}, {}):".format(func.__name__, entries1, entries2))
        print(time_str("origin", origin, origin))
        print(
            time_str("py", origin, instrumented) +
            time_str("parse", origin, instrumented_parse) +
            time_str("json", origin, instrumented_json))
        print(
            time_str("c", origin, instrumented_c) +
            time_str("parse", origin, instrumented_c_parse) +
            time_str("json", origin, instrumented_c_json))
        print(time_str("cProfile", origin, cprofile))
Beispiel #2
0
    def do_one_function(self, func):
        # the original speed
        with Timer() as t:
            func()
            origin = t.get_time()

        # With viztracer + c tracer + vdb
        tracer = VizTracer(verbose=0, vdb=True)
        tracer.start()
        with Timer() as t:
            func()
            instrumented_c_vdb = t.get_time()
        tracer.stop()
        with Timer() as t:
            tracer.parse()
            instrumented_c_vdb_parse = t.get_time()
        with Timer() as t:
            tracer.save(output_file="tmp.json")
            instrumented_c_vdb_json = t.get_time()
        os.remove("tmp.json")
        tracer.clear()

        # With viztracer + c tracer
        tracer = VizTracer(verbose=0)
        tracer.start()
        with Timer() as t:
            func()
            instrumented_c = t.get_time()
        tracer.stop()
        with Timer() as t:
            tracer.parse()
            instrumented_c_parse = t.get_time()
        with Timer() as t:
            tracer.generate_json(allow_binary=True)
            instrumented_c_json = t.get_time()
        tracer.clear()

        # With cProfiler
        pr = cProfile.Profile()
        pr.enable()
        with Timer() as t:
            func()
            cprofile = t.get_time()
        pr.disable()

        def time_str(name, origin, instrumented):
            return "{:.9f}({:.2f})[{}] ".format(instrumented,
                                                instrumented / origin, name)

        print(time_str("origin", origin, origin))
        print(
            time_str("c+vdb", origin, instrumented_c_vdb) +
            time_str("parse", origin, instrumented_c_vdb_parse) +
            time_str("json", origin, instrumented_c_vdb_json))
        print(
            time_str("c", origin, instrumented_c) +
            time_str("parse", origin, instrumented_c_parse) +
            time_str("json", origin, instrumented_c_json))
        print(time_str("cProfile", origin, cprofile))
Beispiel #3
0
 def test_exception(self):
     tracer = VizTracer()
     tracer.start()
     counter = VizCounter(tracer, "name")
     with self.assertRaises(Exception) as _:
         counter.a = ""
     with self.assertRaises(Exception) as _:
         counter.b = {}
     with self.assertRaises(Exception) as _:
         counter.c = []
     tracer.stop()
     tracer.clear()