Skip to content

Commit

Permalink
Add tracer code and basic raw json report
Browse files Browse the repository at this point in the history
This patch includes:

  * Add main module that can be called by "profimp"
    If no args it will display help message otherwise
    it will do it's job

  * Add tracer code that knows how to trace imports

  * Add reporter that knows hot to show traces in json

  * Cover everything by unit tests

  * Improve a bit Readme
  • Loading branch information
boris-42 committed Apr 2, 2015
1 parent f3307ac commit 713f749
Show file tree
Hide file tree
Showing 9 changed files with 421 additions and 13 deletions.
26 changes: 23 additions & 3 deletions README.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,26 @@
==============================
Profimp - python import tracer
==============================
===============================
Profimp - python imports tracer
===============================


Profimp allows you to trace imports of your code.

This lib should be used to simplify optimization of imports in your code.
At least you will find what consumes the most part of time and do the
right decisions.

Syntax:

.. code-block::
profimp [import_module_line]
Samples:

.. code-block::
profimp "import re"
or
profimp "from somemoudle import something"
23 changes: 21 additions & 2 deletions doc/source/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,27 @@
under the License.


Profimp - python import tracer
==============================
Profimp - python imports tracer
===============================

Profimp allows you to trace imports of your code.

This lib should be used to simplify optimization of imports in your code.
At least you will find what consumes the most part of time and do the
right decisions.

Syntax:

.. code-block::
profimp [import_module_line]
Samples:

.. code-block::
profimp "import re"
or
profimp "from somemoudle import something"
47 changes: 46 additions & 1 deletion profimp/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,54 @@
# License for the specific language governing permissions and limitations
# under the License.

from __future__ import print_function

import sys

from profimp import reports
from profimp import tracer


HELP_MESSAGE = """
Profimp allows you to trace imports of your code.
This lib should be used to simplify optimization of imports in your code.
At least you will find what consumes the most part of time and do the
right decisions.
Syntax:
profimp [import_module_line]
Samples:
profimp "import re"
or
profimp "from somemoudle import something"
"""


def print_help():
print(HELP_MESSAGE)


def trace_module(import_line):
root_pt = tracer.init_stack()
with tracer.patch_import():
exec(import_line)
return root_pt


def main():
return "Hello world"
if len(sys.argv) == 1:
print_help()
elif len(sys.argv) == 2:
report = reports.to_json(trace_module(sys.argv[1]))
sys.stdout.write(report)
else:
print_help()
raise SystemExit("Wrong input arguments: %s" % sys.argv)


if __name__ == "__main__":
main()
9 changes: 3 additions & 6 deletions tests/unit/test_noop.py → profimp/reports.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,8 @@
# License for the specific language governing permissions and limitations
# under the License.

from tests.unit import test
import json


class NoopTestCase(test.TestCase):
"""Test case base class for all unit tests."""

def test_noop(self):
self.assertEqual(4, 2 + 2)
def to_json(results):
return json.dumps(results.to_dict(), indent=2)
98 changes: 98 additions & 0 deletions profimp/tracer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
# Copyright 2015: Boris Pavlovic
# All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.

import contextlib
import time

from six.moves import builtins


class TracePoint(object):

def __init__(self, import_line, level=0):
self.started_at = 0
self.finished_at = 0
self.import_line = import_line
self.level = level
self.children = []

def __enter__(self):
self.start()
return self

def __exit__(self, etype, value, traceback):
self.stop()

def to_dict(self):

result = {
"started_at": self.started_at,
"finished_at": self.finished_at,
"duration": (self.finished_at - self.started_at) * 1000,
"import_line": self.import_line,
"level": self.level,
"children": []
}

for child in self.children:
result["children"].append(child.to_dict())

return result

def start(self):
self.started_at = time.time()

def stop(self):
self.finished_at = time.time()

def add_child(self, child):
self.children.append(child)
child.level = self.level + 1


TRACE_STACK = []


def init_stack():
global TRACE_STACK
TRACE_STACK = [TracePoint("root", 0)]
return TRACE_STACK[0]


@contextlib.contextmanager
def patch_import():
old_import = builtins.__import__
builtins.__import__ = _traceit(builtins.__import__)
yield
builtins.__import__ = old_import


def _traceit(f):
def w(*args, **kwargs):
import_line = ""

if len(args) > 3 and args[3]:
import_line = "from %s import %s" % (args[0], ", ".join(args[3]))
else:
import_line = "import %s" % args[0]

with TracePoint(import_line) as trace_pt:
TRACE_STACK[-1].add_child(trace_pt)
TRACE_STACK.append(trace_pt)
try:
return f(*args, **kwargs)
finally:
TRACE_STACK.pop()
return w
1 change: 0 additions & 1 deletion tests/unit/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
# under the License.

import mock

import testtools


Expand Down
60 changes: 60 additions & 0 deletions tests/unit/test_main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# Copyright 2015: Boris Pavlovic
# All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.

import mock

from profimp import main
from tests.unit import test


class MainTestCase(test.TestCase):

@mock.patch("profimp.main.print", create=True)
def test_print_help(self, mock_print):
main.print_help()
mock_print.assert_called_once_with(main.HELP_MESSAGE)

def test_trace_module(self):
root_pt = main.trace_module("import re")

self.assertEqual(1, len(root_pt.children))
self.assertEqual("import re", root_pt.children[0].import_line)

@mock.patch("profimp.main.print_help")
@mock.patch("profimp.main.sys")
def test_main_too_few_args(self, mock_sys, mock_print_help):
mock_sys.argv = ["profimp"]
main.main()
mock_print_help.assert_called_once_with()

@mock.patch("profimp.main.reports")
@mock.patch("profimp.main.trace_module")
@mock.patch("profimp.main.sys")
def test_main(self, mock_sys, mock_trace_module, mock_reports):
mock_sys.argv = ["profimp", "import re"]

mock_trace_module.return_value
main.main()

mock_trace_module.assert_called_once_with("import re")
mock_reports.to_json.assert_called_once_with(
mock_trace_module.return_value)

@mock.patch("profimp.main.print_help")
@mock.patch("profimp.main.sys")
def test_main_with_too_many_args(self, mock_sys, mock_print_help):
mock_sys.argv = ["profimp", "module_one", "something else"]
self.assertRaises(SystemExit, main.main)
mock_print_help.assert_called_once_with()
32 changes: 32 additions & 0 deletions tests/unit/test_reports.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Copyright 2015: Boris Pavlovic
# All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.

import json

import mock

from profimp import reports
from tests.unit import test


class ReportsTestCase(test.TestCase):

def test_to_json(self):

results = mock.MagicMock()
results.to_dict.return_value = {"a": 1, "b": 20}

self.assertEqual(json.dumps(results.to_dict.return_value, indent=2),
reports.to_json(results))
Loading

0 comments on commit 713f749

Please sign in to comment.