-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add tracer code and basic raw json report
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
Showing
9 changed files
with
421 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,7 +14,6 @@ | |
# under the License. | ||
|
||
import mock | ||
|
||
import testtools | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
Oops, something went wrong.