#!/usr/bin/env python
#
# Run and visualize a benchmark test.

import argparse
import subprocess
import cProfile
from benchmarks import likelihood


def main(benchmark):
    class_name, method_name = benchmark.split('.')
    test = getattr(likelihood, class_name)
    m = test()
    m.setup()
    sub_test = getattr(m, method_name)

    pr = cProfile.Profile()
    pr.enable()
    sub_test()
    pr.disable()
    pr.dump_stats('.profile')

    subprocess.Popen(['snakeviz', '.profile'])


if __name__ == '__main__':
    parser = argparse.ArgumentParser('Run and visualize a benchmark test.')
    parser.add_argument('-b', '--benchmark',
                        default='TimeLikelihood.time_likelihood',
                        help="benchmark to run (classname.methodname)")
    args = parser.parse_args()
    main(args.benchmark)
