示例#1
0
 def test_too_many_entry(self):
     json_path = os.path.join(os.path.dirname(__file__), "data", "multithread.json")
     with open(json_path) as f:
         rb = ReportBuilder(f, verbose=1)
     rb.entry_number_threshold = 20
     # Coverage only
     rb.generate_json()
示例#2
0
 def test_too_many_entry(self):
     json_path = os.path.join(os.path.dirname(__file__), "data",
                              "multithread.json")
     with open(json_path) as f:
         rb = ReportBuilder(json.loads(f.read()), verbose=1)
     rb.entry_number_threshold = 20
     # Coverage only
     with io.StringIO() as s:
         rb.save(s)
示例#3
0
 def test_file(self):
     json_path = os.path.join(os.path.dirname(__file__), "data", "multithread.json")
     with open(json_path) as f:
         rb = ReportBuilder(f, verbose=0)
     rb.combine_json()
     result1 = rb.generate_json()
     rb.combine_json()
     result2 = rb.generate_json()
     self.assertEqual(result1, result2)
示例#4
0
    def giltracer(self, line, cell, local_ns):
        temp_dir = tempfile.mkdtemp()
        perf_path = os.path.join(temp_dir, 'perf.data')
        viz_path = os.path.join(temp_dir, 'viztracer.json')
        gil_path = os.path.join(temp_dir, 'giltracer.json')
        out_path = 'giltracer.html'
        code = self.shell.transform_cell(cell)
        with PerfRecordGIL(perf_path, gil_path) as gt:
            with viztracer.VizTracer(output_file=viz_path):
                exec(code, local_ns, local_ns)
        gt.post_process()
        builder = ReportBuilder([viz_path, gil_path])
        builder.save(output_file=out_path)

        download = HTML(
            f'''<a href="{out_path}" download>Download {out_path}</a>''')
        view = HTML(
            f'''<a href="{out_path}" target="_blank" rel="noopener noreferrer">Open {out_path} in new tab</a> (might not work due to security issue)'''
        )
        display(download, view)
示例#5
0
    def test_invalid(self):
        with self.assertRaises(TypeError):
            _ = ReportBuilder(123123)

        with self.assertRaises(TypeError):
            _ = ReportBuilder([123])

        with self.assertRaises(ValueError):
            rb = ReportBuilder([])
            rb.save()
示例#6
0
 def test_file(self):
     json_path = os.path.join(os.path.dirname(__file__), "data",
                              "multithread.json")
     with open(json_path) as f:
         rb = ReportBuilder(json.loads(f.read()), verbose=0)
     with io.StringIO() as s:
         rb.save(s)
         result1 = s.getvalue()
     with io.StringIO() as s:
         rb.save(s)
         result2 = s.getvalue()
     self.assertEqual(result1, result2)
示例#7
0
 def test_invalid_json(self):
     invalid_json_path = os.path.join(os.path.dirname(__file__), "data", "fib.py")
     with open(invalid_json_path) as f:
         with self.assertRaises(Exception):
             ReportBuilder(f, verbose=1)
示例#8
0
 def test_invalid(self):
     with self.assertRaises(TypeError):
         _ = ReportBuilder(123123)
示例#9
0
def main(argv=sys.argv):
    parser = argparse.ArgumentParser(
        argv[0],
        formatter_class=argparse.RawDescriptionHelpFormatter,
        usage=usage)
    parser.add_argument('--module', '-m')
    parser.add_argument(
        '--import',
        dest="import_",
        help=
        "Comma seperated list of modules to import before tracing (cleans up tracing output)"
    )
    parser.add_argument('--verbose', '-v', action='count', default=1)
    parser.add_argument('--quiet', '-q', action='count', default=0)
    parser.add_argument('--output',
                        '-o',
                        dest="output",
                        default='giltracer.html',
                        help="Output filename (default %(default)s)")
    parser.add_argument(
        '--state-detect',
        help=
        "Use perf sched events to detect if a process is sleeping due to the GIL (default: %(default)s)",
        default=False,
        action='store_true')
    parser.add_argument('--no-state-detect',
                        dest="state_detect",
                        action='store_false')
    parser.add_argument(
        '--gil-detect',
        help=
        "Use uprobes to detect who has the GIL (read README.md) (default: %(default)s)",
        default=True,
        action='store_true')
    parser.add_argument('--no-gil-detect',
                        dest="gil_detect",
                        action='store_false')

    parser.add_argument('args', nargs=argparse.REMAINDER)
    args = parser.parse_args(argv[1:])
    verbose = args.verbose - args.quiet

    if args.import_:
        for module in args.import_.split(','):
            if verbose >= 2:
                print(f'importing {module}')
            __import__(module)

    perf1 = PerfRecordSched(verbose=verbose) if args.state_detect else None
    perf2 = PerfRecordGIL(verbose=verbose) if args.gil_detect else None
    vt = viztracer.VizTracer(output_file="viztracer.json", verbose=verbose)

    # pass on the rest of the arguments
    sys.argv = args.args
    if args.module:
        module = runpy.run_module(args.module)
    else:
        module = runpy.run_path(sys.argv[0])

    if perf1:
        perf1.start()
    if perf2:
        perf2.start()

    try:
        vt.start()
        module['main'](args.args)
    finally:
        vt.stop()
        if perf1:
            perf1.stop()
        if perf2:
            perf2.stop()
        vt.save('viztracer.json')
        if perf1:
            perf1.post_process()
        if perf2:
            perf2.post_process()

    files = ['viztracer.json']
    if perf1:
        files.append('schedtracer.json')
    if perf2:
        files.append('giltracer.json')
    builder = ReportBuilder(files, verbose=verbose)
    builder.save(output_file=args.output)