Skip to content

Commit

Permalink
remove unused code, add commandline tests
Browse files Browse the repository at this point in the history
  • Loading branch information
gaogaotiantian committed Aug 11, 2020
1 parent 90d8e69 commit fc263ce
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 45 deletions.
33 changes: 0 additions & 33 deletions src/codesnap/htmlconverter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '<div style="left:{}%;width:{}%;" class="func-container">'.\
format(left*100, width*100)
ret += '<div style="background-color:{};" class="func-block">{}</div>'.\
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 += '</div>'

return ret


def snap_tree_root_node_html(node):
ret = '<div id="root" style="width:98vw;margin-left:1vw">'
for child in node.children:
ret += child.html(node.t_entry, node.t_exit)
ret += '</div>'

return ret


def generate_html_report_from_snap_tree(tree):
sub = {}

Expand Down
11 changes: 1 addition & 10 deletions src/codesnap/snaptree.py
Original file line number Diff line number Diff line change
@@ -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:
Expand All @@ -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 = []
Expand Down
40 changes: 40 additions & 0 deletions tests/test_cmdline.py
Original file line number Diff line number Diff line change
@@ -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"])
4 changes: 2 additions & 2 deletions tests/test_performance.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
8 changes: 8 additions & 0 deletions tests/test_regression.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

0 comments on commit fc263ce

Please sign in to comment.