diff --git a/src/codesnap/htmlconverter.py b/src/codesnap/htmlconverter.py index 3c99e4ba..225e58c8 100644 --- a/src/codesnap/htmlconverter.py +++ b/src/codesnap/htmlconverter.py @@ -7,39 +7,6 @@ from string import Template -colors = ['#e6194B', '#3cb44b', '#ffe119', '#4363d8', '#f58231', '#42d4f4', - '#f032e6', '#bfef45', '#fabed4', '#469990', '#dcbeff', '#9A6324', - '#fffac8', '#aaffc3', '#808000', '#ffd8b1'] - - -def string_to_color(s): - return colors[hash(s) % len(colors)] - - -def snap_tree_node_html(node, parent_entry, parent_exit): - parent_duration = parent_exit - parent_entry - left = (node.t_entry - parent_entry) / parent_duration - width = (node.t_exit - node.t_entry) / parent_duration - ret = '
'.\ - format(left*100, width*100) - ret += '
{}
'.\ - format(string_to_color(node.function_name), html.escape(node.function_name)) - for child in node.children: - ret += child.html(node.t_entry, node.t_exit) - ret += '
' - - return ret - - -def snap_tree_root_node_html(node): - ret = '
' - for child in node.children: - ret += child.html(node.t_entry, node.t_exit) - ret += '
' - - return ret - - def generate_html_report_from_snap_tree(tree): sub = {} diff --git a/src/codesnap/snaptree.py b/src/codesnap/snaptree.py index 9f96e982..dc0750ec 100644 --- a/src/codesnap/snaptree.py +++ b/src/codesnap/snaptree.py @@ -1,7 +1,7 @@ # Licensed under the Apache License: http://www.apache.org/licenses/LICENSE-2.0 # For details: https://github.com/gaogaotiantian/codesnap/blob/master/NOTICE.txt -from .htmlconverter import snap_tree_node_html, snap_tree_root_node_html, generate_html_report_from_snap_tree +from .htmlconverter import generate_html_report_from_snap_tree class SnapTreeNode: @@ -13,15 +13,6 @@ def __init__(self, parent, name, t_entry, t_exit): self.exited = False self.children = [] - def html(self, parent_entry=None, parent_exit=None): - if parent_entry is None and parent_exit is None: - # This is root - return snap_tree_root_node_html(self) - elif self.exited: - return snap_tree_node_html(self, parent_entry, parent_exit) - else: - return "" - def json_object(self): stack = [self] ret = [] diff --git a/tests/test_cmdline.py b/tests/test_cmdline.py new file mode 100644 index 00000000..83a610ac --- /dev/null +++ b/tests/test_cmdline.py @@ -0,0 +1,40 @@ +import unittest +import subprocess +import os + +file_content = \ +""" +def fib(n): + if n < 2: + return 1 + return fib(n-1) + fib(n-2) +fib(5) +""" +class TestCommandLineBasic(unittest.TestCase): + def build_script(self): + with open("cmdline_test.py", "w") as f: + f.write(file_content) + + def cleanup(self, output_file="result.html"): + os.remove("cmdline_test.py") + os.remove(output_file) + + def template(self, cmd_list, expected_output_file="result.html", success=True): + self.build_script() + result = subprocess.run(cmd_list) + self.assertTrue(success ^ (result.returncode != 0)) + self.assertTrue(os.path.exists(expected_output_file)) + self.cleanup(output_file=expected_output_file) + + def test_run(self): + self.template(["python", "-m", "codesnap", "cmdline_test.py"]) + + def test_outputfile(self): + self.template(["python", "-m", "codesnap", "-o", "result.html", "cmdline_test.py"]) + self.template(["python", "-m", "codesnap", "-o", "result.json", "cmdline_test.py"], expected_output_file="result.json") + self.template(["python", "-m", "codesnap", "--output_file", "result.html", "cmdline_test.py"]) + self.template(["python", "-m", "codesnap", "--output_file", "result.json", "cmdline_test.py"], expected_output_file="result.json") + + def test_tracer(self): + self.template(["python", "-m", "codesnap", "--tracer", "c", "cmdline_test.py"]) + self.template(["python", "-m", "codesnap", "--tracer", "python", "cmdline_test.py"]) \ No newline at end of file diff --git a/tests/test_performance.py b/tests/test_performance.py index 80855906..7ce67121 100644 --- a/tests/test_performance.py +++ b/tests/test_performance.py @@ -12,8 +12,8 @@ def do_one_function(self, func): func() origin_end = time.perf_counter() - # With codesnap - snap = CodeSnap() + # With codesnap + python tracer + snap = CodeSnap("python") snap.start() instrumented_start = time.perf_counter() func() diff --git a/tests/test_regression.py b/tests/test_regression.py index de9912be..9d9f56ee 100644 --- a/tests/test_regression.py +++ b/tests/test_regression.py @@ -11,3 +11,11 @@ def test_datetime(self): snap.stop() snap.parse() snap.generate_json() + + snap = codesnap.CodeSnap(tracer="python") + snap.start() + from datetime import timedelta + timedelta(hours=5) + snap.stop() + snap.parse() + snap.generate_json() \ No newline at end of file